delphi - 如何使用自定义颜色填充字符串网格的单元格?

标签 delphi delphi-xe8 tstringgrid

我正在尝试编写自定义日期选择器(日历)。日期将显示在字符串网格上。我正在尝试使用自定义颜色填充单击的单元格并使所选单元格文本变为粗体。

这是我的代码:

    type
      TStringGrid = Class(Vcl.Grids.TStringGrid)
      private
        FHideFocusRect: Boolean;
      protected
         Procedure Paint;override;
      public
         Property HideFocusRect:Boolean Read FHideFocusRect Write FHideFocusRect;
      End;


    TfrmNepaliCalendar = class(TForm)
    ...
    ...
    ...
    end;


    procedure TfrmNepaliCalendar.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
       if gdSelected in State then begin
        StringGrid.Canvas.Brush.Color := $00940A4B;
        StringGrid.Canvas.FillRect(Rect);

        StringGrid.Canvas.Font.Style := [fsBold];
        StringGrid.Canvas.Font.Color := clHighlightText;
        StringGrid.Canvas.TextOut(Rect.Left + 3, Rect.Top + 5, StringGrid.Cells[ACol,ARow]);

        StringGrid.HideFocusRect := True;
      end;
    end;


{ TStringGrid }

procedure TStringGrid.Paint;
var
  LRect: TRect;
begin
  inherited;
  if HideFocusRect then begin
    LRect := CellRect(Col,Row);
    if DrawingStyle = gdsThemed then InflateRect(LRect,-1,-1);

    DrawFocusrect(Canvas.Handle,LRect)
  end;
end;

我得到的输出:

When clicked on cell containing no text

When clicked, the background is clipped from left

问题#1:我需要隐藏作为所选单元格边框出现的不需要的矩形

问题#2:避免单元格背景剪切

最佳答案

在 OnDrawCell 过程中在 FillRect 之前添加

Rect.Left := Rect.Left-4;

似乎有效。

<小时/>

另一种选择

即使使用您的绘画程序插件,上述内容也不能完全解决焦点问题。有时,单元格边框内会出现一条白线。

但以下是一种替代方案,可以解决您的两个问题。它需要更多的编码,但不需要那么多。另一方面,不需要子类化 TStringGrid,也不需要 Rect 调整

基础是禁用默认绘制,因此设置 grids 属性DefaultDrawing := false; 然后添加到OnDrawCell事件中:

procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if gdFixed in State then
  begin
    StringGrid.Canvas.Brush.Color := clGradientInactiveCaption;
    StringGrid.Canvas.Font.Style := [];
    StringGrid.Canvas.Font.Color := clBlack;
  end
  else
  if gdSelected in State then
  begin
    StringGrid.Canvas.Brush.Color := $00940A4B;
    StringGrid.Canvas.Font.Style := [fsBold];
    StringGrid.Canvas.Font.Color := clHighlightText;
  end
  else
  begin
    StringGrid.Canvas.Brush.Color := $00FFFFFF;
    StringGrid.Canvas.Font.Style := [];
    StringGrid.Canvas.Font.Color := clWindowText;
  end;

  StringGrid.Canvas.FillRect(Rect);
  StringGrid.Canvas.TextOut(Rect.Left + 3, Rect.Top + 5, StringGrid.Cells[ACol,ARow]);
end;

禁用默认绘图后,网格将绘制网格框和网格线,但将所有其他绘图留给程序员。需要注意的是,如果需要,您必须自己添加精美的主题图画。 通过上面的编码,我得到这个结果:

Sample grid

关于delphi - 如何使用自定义颜色填充字符串网格的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120729/

相关文章:

delphi - 如何从Windows获取用于存储缓存缩略图的永久目录?

delphi - 在哪里可以找到 "ESC/POS"Epson 条码测试程序?

delphi - Firemonkey:在带有 BorderStyle None 的表单中使用 TWebBrowser 时不显示

image - Delphi StringGrid 背景图片

delphi - IdHttpServer 表单标题未更新

delphi - 如何在 C++ Builder 中使用 Delphi Inc() 和 Dec() 过程

json - Delphi Rest.JSON JsonToObject 仅适用于 f 变量

delphi - 如何适本地制作RAD Studio格式的案例陈述?

C++ 构建器 TstringGrid 从特定单元格获取字符串

delphi - TStringGrid 单元格的范围检查是否有效?