如何在 TDrawGrid 的单元格中显示图片 - 回复 "巅枫" 的问题
如何在 TDrawGrid 的单元格中显示图片 - 回复 "巅枫" 的问题
涂孟超 发表于3年前
如何在 TDrawGrid 的单元格中显示图片 - 回复 "巅枫" 的问题
  • 发表于 3年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

问题来源: http://www.cnblogs.com/del/archive/2008/03/06/1094289.html#2023057

在 C:\Temp\ 下准备 1.bmp、2.bmp、3.bmp 三个图片, 然后:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;

type
  TForm1 = class(TForm)
    DrawGrid1: TDrawGrid;
    procedure FormCreate(Sender: TObject);
    procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  bits: array[0..2] of TBitmap;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  ChDir('c:\Temp\');
  for i := Low(bits) to High(bits) do
  begin
    bits[i] := TBitmap.Create;
    bits[i].LoadFromFile(IntToStr(i+1) + '.bmp');
  end;

  DrawGrid1.FixedRows := 0;
  DrawGrid1.FixedCols := 0;
  DrawGrid1.RowCount := 1;
  DrawGrid1.ColCount := 3;
  DrawGrid1.DefaultRowHeight := bits[0].Height;
  DrawGrid1.DefaultColWidth := bits[0].Width;
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  r: TRect;
begin
  if (ARow = 0) and (ACol >= Low(bits)) and (ACol <= High(bits)) then
  begin
    r := Bounds(0, 0, bits[ACol].Width, bits[ACol].Height);
    //r := Bounds(0, 0, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
    TDrawGrid(Sender).Canvas.CopyRect(Rect, bits[ACol].Canvas, r);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := Low(bits) to High(bits) do bits[i].Free;
end;

end.

共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: