【发布时间】:2021-07-27 13:29:09
【问题描述】:
我正在创建 TDBGrid 的派生类,并且我想实现一种更好的方法来定义其文本格式,类似于 QuantumGrid 的 GetContentStyle。
问题在于 DBGrid 忽略了我的新事件在其 Canvas 上设置的字体和颜色。
type TSetCellStyle = procedure(const Sender: TObject; const AColumn: TColumn; const ARow: TDataset; const AField: TField; const State: TGridDrawState; var TextFont: TFont; var BackgroundColor: TColor) of object;
TMyDBGrid = class(TDBGrid)
private
FSetCellStyle: TSetCellStyle;
protected
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); override;
published
property OnSetCellstyle: TSetCellStyle read FSetCellStyle write FSetCellStyle;
...
...
implementation
procedure TMyDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
BeginUpdate;
Canvas.Lock; // Prevents other threads from drawing on the canvas.
if Assigned(FSetCellStyle) then begin
var TextFont: TFont;
var BackgroundColor: TColor;
TextFont := Canvas.Font;
BackgroundColor := Canvas.Brush.Color;
FSetCellStyle(Self, Column, Self.DataSource.DataSet, Self.DataSource.DataSet.FindField(Column.FieldName), State, TextFont, BackgroundColor);
Canvas.Font := TextFont;
Canvas.Brush.Color := BackgroundColor;
end;
Canvas.Unlock;
inherited DrawColumnCell(Rect, DataCol, Column, State);
EndUpdate;
end;
这是应用程序如何使用新事件来自定义网格格式的示例:
procedure TFInspira.GridInspiraSetCellStyle(const Sender: TObject; const AColumn: TColumn; const ARow: TDataSet; const AField: TField; const State: TGridDrawState; var TextFont: TFont; var BackgroundColor: TColor);
begin
if (AColumn.FieldName = 'ReferenciaGrup') and (ARow.FieldByName('PrimerDeGrup').AsBoolean) then begin
BackgroundColor := clYellow;
end;
if ARow.FieldByName('Selected').AsBoolean then begin
TextFont.Style := TextFont.Style + [fsItalic];
end;
end;
我可以调试我的网格并看到覆盖的 DrawColumnCell 将某些单元格的画布设置为黄色和斜体,但网格从不显示它们。看起来对 inherit DrawColumnCell 的调用会重置 Canvas 的格式。
如果我无法在 DrawColumnCell 中挂钩我的格式化事件,我在哪里可以这样做?
谢谢。
【问题讨论】:
-
据我了解,如果 Grid 的 DefaultDrawing 设置为 true(这是默认设置),则文本已被绘制。
标签: delphi vcl delphi-10.4-sydney