【问题标题】:How do I paint the background of TStringGrid如何绘制 TStringGrid 的背景
【发布时间】:2011-12-09 06:46:13
【问题描述】:

我使用 OnDrawCell 事件对 Delphi TStringGrid 进行自定义绘图。 单元格覆盖的区域没有问题,但是如何在最右边一列和最后一行下方绘制背景?

(编辑) 绘画不是真的必要,我只想设置用于背景的颜色。 我正在使用 XE2 并研究 VCL 样式。 即使在默认绘图中,在字符串网格中设置颜色,接缝也完全没有效果。

TIA

【问题讨论】:

  • 好的,我终于找到了。问题是默认为 gdsThemed 的 TStringGrid 的 DrawingStyle 属性。将其设置为 gdsClassic 会使 grids Color 属性生效 - 也适用于背景。问题解决了。感谢 Andreas 提供了一种完全控制背景绘制过程的方法,但这对我的问题来说太过分了。 rgds TheRoadrunner

标签: delphi paint tstringgrid


【解决方案1】:

这是我在谷歌上找到的一些代码(不是我的,我找不到作者的名字,也许它来自 StackExchange 以某种方式......)。它定义了 TStringGrid 的后代并实现了新的背景绘图。 (该示例使用位图,但您可以轻松更改它...)

  type
  TStringGrid = class(Grids.TStringGrid)
  private
    FGraphic: TGraphic;
    FStretched: Boolean;
    function BackgroundVisible(var ClipRect: TRect): Boolean;
    procedure PaintBackground;
  protected
    procedure Paint; override;
    procedure Resize; override;
    procedure TopLeftChanged; override;
  public
    property BackgroundGraphic: TGraphic read FGraphic write FGraphic;
    property BackgroundStretched: Boolean read FStretched write FStretched;
  end;

  TForm1 = class(TForm)
    StringGrid: TStringGrid;
    Image: TImage;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TStringGrid }

function TStringGrid.BackgroundVisible(var ClipRect: TRect): Boolean;
var
  Info: TGridDrawInfo;
  R: TRect;
begin
  CalcDrawInfo(Info);
  SetRect(ClipRect, 0, 0, Info.Horz.GridBoundary, Info.Vert.GridBoundary);
  R := ClientRect;
  Result := (ClipRect.Right < R.Right) or (ClipRect.Bottom < R.Bottom);
end;

procedure TStringGrid.Paint;
begin
  inherited Paint;
  PaintBackground;
end;

procedure TStringGrid.PaintBackground;
var
  R: TRect;
begin
  if (FGraphic <> nil) and BackgroundVisible(R) then
  begin
    with R do
      ExcludeClipRect(Canvas.Handle, Left, Top, Right, Bottom);
    if FStretched then
      Canvas.StretchDraw(ClientRect, FGraphic)
    else
      Canvas.Draw(0, 0, FGraphic);
  end;
end;

procedure TStringGrid.Resize;
begin
  inherited Resize;
  PaintBackground;
end;

procedure TStringGrid.TopLeftChanged;
begin
  inherited TopLeftChanged;
  PaintBackground;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Usage: 
  StringGrid.BackgroundGraphic := Image.Picture.Graphic;
  StringGrid.BackgroundStretched := True;
end;

【讨论】:

  • 它来自 NGLN 在 SO 上的回答。 delphi-stringgrid-with-picture-in-background。它应该与背景中的图片一起使用。
  • 好的,谢谢!使用代码在网格上绘制其他背景应该没有问题。
  • 完全没有问题,但要回答这个问题,您可以详细说明如何绘制背景颜色。
  • @Andreas 您可以自己添加链接。习惯上将您重新迭代的信息归因于其来源。
  • @LU RD:问题已编辑。一开始并没有暗示它只是要绘制的背景颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多