其他分享
首页 > 其他分享> > 如何在 TDrawGrid 的单元格中显示图片 - 回复 "巅枫" 的问题

如何在 TDrawGrid 的单元格中显示图片 - 回复 "巅枫" 的问题

作者:互联网

如何在 TDrawGrid 的单元格中显示图片 - 回复 "巅枫" 的问题
问题来源: 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.


posted on 2011-02-09 11:12  万一  阅读(5145)  评论(5)  编辑  收藏

标签:Sender,巅枫,单元格,TForm1,TObject,DrawGrid1,TDrawGrid,bits,procedure
来源: https://blog.51cto.com/u_14617575/2795052