gpt4 book ai didi

delphi - 如何绘制 TStringGrid 的背景

转载 作者:行者123 更新时间:2023-12-03 15:02:10 25 4
gpt4 key购买 nike

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

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

TIA

最佳答案

这是我通过google找到的一些代码(它不是我写的,我找不到作者的名字,也许它以某种方式来自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;

关于delphi - 如何绘制 TStringGrid 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8442007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com