2013-09-28

【Delphi】DBGrid 標題欄顯示多列文字/標題換行(折行)

資料來源 ----
http://www.swissdelphicenter.ch/en/showcode.php?id=1653
http://delphi.ktop.com.tw/board.php?cid=30&fid=66&tid=38801

  1. type
  2. TAccessDBGrid = class(TDBGrid);
  3.  
  4. ...
  5. ...
  6.  

再於 DBGrid 的 onDrawColumnCell 事件內寫入下述程式碼:


  1. procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  2. var
  3. S1, S2: String;
  4. begin
  5. with TAccessDBGrid(DBGrid1) do
  6. begin
  7. // 指定標題欄高度
  8. RowHeights[0] := 32;
  9.  
  10. Canvas.Brush.Style := bsClear;
  11.  
  12. // Column Titles
  13. case Column.Index of
  14. 0: begin
  15. Column.Title.Caption := '';
  16. S1 := 'Row 1';
  17. S2 := 'Row 2';
  18. end;
  19. 1: begin
  20. Column.Title.Caption := '';
  21. S1 := 'Row 3';
  22. S2 := 'Row 4';
  23. end;
  24. end;
  25.  
  26. // write title:
  27. Canvas.TextOut(Rect.Left+2, 2, S1);
  28. Canvas.TextOut(Rect.Left+2, 16, S2);
  29. end;
  30. end;

沒有留言:

張貼留言