观察DBGrids单元内的源码,发现原本的定义是:
procedure DrawDataCell(const Rect: TRect; Field: TField;
State: TGridDrawState); dynamic;
既然是动态的,那就可以重载。于是写新代码,添加一个函数:
unit MyDBGrid;
interface
uses
SysUtils, Classes, Controls, Grids, DBGrids, Types;
type
TMyDBGrid = class(TDBGrid)
private
{ Private declarations }
protected
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState); override;//这里,注意要override
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
uses Graphics;
procedure Register;
begin
RegisterComponents('Samples', [TMyDBGrid]);
end;
//对应的函数体:
procedure TMyDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState);
begin
Canvas.Brush.Color:=clRed;
Canvas.FillRect(Rect);//把整个cell涂成红色。you can do your own operations here
inherited;
//这句话表示调用父类里面的DrawColumnCell。多数重载的函数都会在最先或最后调用
//父类里面的对应函数,因为父类里的函数可能会提供一些你需要的操作。当然,如果
//你觉得你的操作已经足够,也可以不要掉用父类里的函数。看需要而定。
end;
end.
因为TRect没有定义,add uses Types;因为clRed没有定义,add uses graphic.
Done