加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何绘制TStringGrid的背景

发布时间:2020-12-15 09:51:00 所属栏目:大数据 来源:网络整理
导读:我使用OnDrawCell事件自定义绘制Delphi TStringGrid. 单元格覆盖的区域没有问题,但如何绘制最右边的列右下方和最后一行的背景? (编辑) 绘画不是真的有必要,我只想设置用于背景的颜色. 我正在使用XE2并调查VCL样式. 即使在默认绘图中,在stringgrid中设置Colo
我使用OnDrawCell事件自定义绘制Delphi TStringGrid.
单元格覆盖的区域没有问题,但如何绘制最右边的列右下方和最后一行的背景?

(编辑)
绘画不是真的有必要,我只想设置用于背景的颜色.
我正在使用XE2并调查VCL样式.
即使在默认绘图中,在stringgrid中设置Colors,接缝也完全没有效果.

TIA

解决方法

这是我在谷歌找到的一些代码(它不是来自我,我找不到作者的名字,也许它来自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,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,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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读