【问题标题】:I can't set the text formatting on my own derivative of a TDBGrid我无法在自己的 TDBGrid 衍生产品上设置文本格式
【发布时间】: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


【解决方案1】:

我认为您的 DrawColumnCell 只是缺少对 DefaultDrawDataCell 的调用以使网格实际绘制单元格。 F.i.in my answer 给你的另一个 q,

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  AGrid : TDBGrid;
begin
  AGrid := (Sender as TDBGrid);

  if Odd(AGrid.RowBeingDrawn) then begin
     AGrid.Canvas.Brush.Color := clGreen;
  end;
  AGrid.DefaultDrawDataCell(Rect, Column.Field, State);

end;

显然,这会将交替行的单元格涂成绿色。

我使用了其他答案的插入器类,这样我就可以引用添加的 RowBeingDrawn 属性,但类似于上面的代码也可以与标准 TDBGrid 一起使用(前提是其 DefaultDrawing 属性设置为 True) .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多