本文主要是介绍回答网友的一个Delphi问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
网友想在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;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, Grids, DBGrids, DB, ExtCtrls, ADODB;typeTForm1 = 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);privatedg_bmp:Tbitmap;public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState);
varDG: TDBGrid;Source: TRect;
beginDG:= 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);
varcol_cnt,row_cnt,i,k ,w,h:integer;
begindg_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 doADODataSet1.FieldDefs.Add('F'+inttostr(i),ftString,36);ADODataSet1.CreateDataSet();ADODataSet1.DisableControls;for i:=1 to row_cnt dobeginADODataSet1.Append();for k:=1 to col_cnt doADODataSet1.FieldByName('F'+inttostr(k)).asstring:=format('行%d 列%d',[i,k]);ADODataSet1.post();end;ADODataSet1.First;ADODataSet1.EnableControls;
end;procedure TForm1.FormDestroy(Sender: TObject);
beginfreeandnil(dg_bmp);
end;end.
这篇关于回答网友的一个Delphi问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!