TDBGrid <- TCustomDBGrid <- TCustomGrid
上面是TDBGrid的继承关系,其画格子的部分是在TCustomGrid中的Paint中解决的。
你可以将TDBGrid的声明完全抄下来,加上一个BkImage属性,重写Paint事件:
TMyDBGrid = class(TCustomDBGrid)
private
fBKImage: TBitmap;
procedure SetBKImage(value: TBitmap);
protected
procedure Paint; Override;
public
...
published
constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
property BkImage: TBitmap read fBKImage write SetBKImage;
...
end;
具体代码参考TCustomGrid.Paint的方法, SetBKImage后要调用Invalidate.
constructor TMyDBGrid.Create;
begin
inherited Create(AOwner);
fBKImage := TBitmap.create;
end;
destructor TMyDBGrid.Destroy;
begin
fBKImage.Free;
inherited;
end;
procedure TMyDBGrid.SetBKImage(Value: TBitmap);
begin
fBKImage.Assign(Value);
invalidate;
end;
procedure TMyDBGrid.Paint;
var DrawRect: TRect;
begin
if Assigned(fBKImage) then
begin
DrawRect := 计算背景大小;
canvas.StretchDraw(DrawRect, fBKImage); //从fBKImage中复制图像到Grid的Canvas上;
end;
inherited;
end;