利用Delphi TStringGrid控件實現日曆排程

在ERP實現排程的模塊中,咱們但願能直觀展示個機臺天天的排單狀況,一直苦惱Delphi沒有合適的控件,沒辦法,先本身動手。dom

效果圖:字體

繪製日曆關鍵代碼spa

procedure TForm1.DrawCalender;
var
  iDay, iProcess, days: Integer;
  row,col:Integer;
begin
  //sgCalender.
  days := DaysInAMonth(StrToInt(cbbYear.Text), StrToInt(cbbMonth.Text));
  sgCalender.ColCount := days + 1;
  sgCalender.RowCount := MachineNumber+1;
  sgCalender.RowHeights[0]:= 25;
  sgCalender.ColWidths[0]:= 80;
  
  for iDay := 1 to days do
  begin
    sgCalender.Cells[iDay, 0] := IntToStr(iDay);
  end;
  for iProcess := 1 to MachineNumber do
  begin
    sgCalender.Cells[0, iProcess] := '機臺' + IntToStr(iProcess);
  end;
  for row := 1 to MachineNumber do
  begin
     for col := 1 to days do
     begin
        sgCalender.Cells[col, row] := Format('排單數:%d' + '|' + '完成數:%d'+ '|' + '製成率:%d',[Random(10000),Random(10000),Random(100)]) ;
     end;           
  end;
end;

 

在作的時候,因爲cell內容不能換行,因此須要在DrawCell處理一下code

procedure TForm1.sgCalenderDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  s, item: string;
  d: TStringGrid;
  I,num: Integer;
begin
  d := TStringGrid(sender);
  s := d.Cells[ACol, ARow];
  begin
    d.Canvas.Font.Assign(d.Font); //制定字體
    with d.Canvas do
    begin
      Brush.Color := clWindow; //制定單元格顏色
      if gdFixed in State then     
        Brush.Color := d.FixedColor;

      Font.Color := clWindowText;

      FillRect(Rect);
      with d do
      begin
        num:=0;
//根據'|' 字符換行 if Pos( '|',s) >0 then begin for I := 0 to Length(s) - 1 do begin if s[i] <> '|' then begin item := item + s[i]; end else begin Rect.Top := Rect.Top + num * 30; DrawText(Canvas.Handle, PChar(trim(item)), Length(Trim(item)), Rect, DT_Left or DT_SINGLELINE or DT_VCENTER); item := ''; Inc(num); end; end; if item<>'' then begin Rect.Top := Rect.Top + 30; DrawText(Canvas.Handle, PChar(trim(item)), Length(Trim(item)), Rect, DT_Left or DT_SINGLELINE or DT_VCENTER); end; end else begin // Draw Fixed Row Col DrawText(Canvas.Handle, PChar(s), Length(s), Rect, DT_CENTER or DT_SINGLELINE or DT_VCENTER); end; end; end; end; end;

 源碼下載orm

相關文章
相關標籤/搜索