网友想在grid 中 加一个水印,俺就给他写了个例子。先靠效果:
这个例子 包含下面几步:
1、创建背景
dg_bmp:=Tbitmap.Create;
w:= Image1.Picture.Bitmap.width;
h:= Image1.Picture.Bitmap.height;
dg_bmp.width:= w*2;
dg_bmp.height:= h*2;
dg_bmp.Canvas.Draw(0,0,Image1.Picture.Bitmap);
dg_bmp.Canvas.Draw(w,0,Image1.Picture.Bitmap);
dg_bmp.Canvas.Draw(0,h,Image1.Picture.Bitmap);
dg_bmp.Canvas.Draw(w,h,Image1.Picture.Bitmap);
2、创建测试数据
col_cnt:=5;
row_cnt:=200;
for i:=1 to col_cnt do
ADODataSet1.FieldDefs.Add('F'+inttostr(i),ftString,36);
ADODataSet1.CreateDataSet();
ADODataSet1.DisableControls;
for i:=1 to row_cnt do
begin
ADODataSet1.Append();
for k:=1 to col_cnt do
ADODataSet1.FieldByName('F'+inttostr(k)).asstring:=format('行%d 列%d',[i,k]);
ADODataSet1.post();
end;
ADODataSet1.First;
ADODataSet1.EnableControls;
3、处理重绘事件
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
var
DG: TDBGrid;
Source: TRect;
begin
DG:= TDBGrid(Sender);
Source.Left:=Rect.Left mod (dg_bmp.Width div 2);
Source.Top:=Rect.Top mod (dg_bmp.Height div 2);
Source.Right:=Source.Left+(Rect.Right-Rect.Left);
Source.Bottom:=Source.Top+(Rect.Bottom-Rect.Top);
DG.Canvas.CopyRect(Rect, dg_bmp.Canvas,Source);
DG.Canvas.Brush.Style:=bsClear;
DG.Canvas.font.Color:=clblue;
DG.DefaultDrawDataCell(Rect,Field,State);
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ExtCtrls, ADODB;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Image1: TImage;
DataSource1: TDataSource;
ADODataSet1: TADODataSet;
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
dg_bmp:Tbitmap;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
var
DG: TDBGrid;
Source: TRect;
begin
DG:= TDBGrid(Sender);
Source.Left:=Rect.Left mod (dg_bmp.Width div 2);
Source.Top:=Rect.Top mod (dg_bmp.Height div 2);
Source.Right:=Source.Left+(Rect.Right-Rect.Left);
Source.Bottom:=Source.Top+(Rect.Bottom-Rect.Top);
DG.Canvas.CopyRect(Rect, dg_bmp.Canvas,Source);
DG.Canvas.Brush.Style:=bsClear;
DG.Canvas.font.Color:=clblue;
DG.DefaultDrawDataCell(Rect,Field,State);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
col_cnt,row_cnt,i,k ,w,h:integer;
begin
dg_bmp:=Tbitmap.Create;
w:= Image1.Picture.Bitmap.width;
h:= Image1.Picture.Bitmap.height;
dg_bmp.width:= w*2;
dg_bmp.height:= h*2;
dg_bmp.Canvas.Draw(0,0,Image1.Picture.Bitmap);
dg_bmp.Canvas.Draw(w,0,Image1.Picture.Bitmap);
dg_bmp.Canvas.Draw(0,h,Image1.Picture.Bitmap);
dg_bmp.Canvas.Draw(w,h,Image1.Picture.Bitmap);
col_cnt:=5;
row_cnt:=200;
for i:=1 to col_cnt do
ADODataSet1.FieldDefs.Add('F'+inttostr(i),ftString,36);
ADODataSet1.CreateDataSet();
ADODataSet1.DisableControls;
for i:=1 to row_cnt do
begin
ADODataSet1.Append();
for k:=1 to col_cnt do
ADODataSet1.FieldByName('F'+inttostr(k)).asstring:=format('行%d 列%d',[i,k]);
ADODataSet1.post();
end;
ADODataSet1.First;
ADODataSet1.EnableControls;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
freeandnil(dg_bmp);
end;
end.