在ERP實現排程的模塊中,我們希望能直觀展現個機台每天的排單情況,一直苦惱Delphi沒有合適的控件,沒辦法,先自己動手。
效果圖:
繪制日歷關鍵代碼
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處理一下
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;
源碼下載
從小到大排列,用個簡單的冒泡排序法就好了。
procedure TForm1.Button1Click(Sender: TObject);
var
i,j:Integer;
tmp:Double;
begin
for i:=0 to StringGrid1.RowCount-2 do
begin
if StringGrid1.Cells[0,i]='' then continue;
for j:=i to StringGrid1.RowCount -2 do
begin
if StrToFloat(StringGrid1.Cells[0,j]) > StrToFloat(StringGrid1.Cells[0,j+1]) then
begin
tmp:=StrToFloat(StringGrid1.Cells[0,j+1]);
StringGrid1.Cells[0,j+1] := StringGrid1.Cells[0,j];
StringGrid1.Cells[0,j]:=FloatToStr(tmp);
end;
end;
end;
end;
又是閣下,插入這樣寫。
procedure TForm1.Button1Click(Sender: TObject);
begin
StringGrid1.RowCount := StringGrid1.RowCount +1;
try
StrToFloat(Edit1.Text);
StringGrid1.Rows[StringGrid1.RowCount -1].Append(Edit1.Text);
except
ShowMessage('輸入的不是數字!');
end;
end;
排序用冒泡排序,早上有說過。