当我们在扩展一个 vcl 组件功能的时候,既想保留IDE中能拖动大小与直接设置属性的功能,又想减少写创建与释放代码和安装扩展后新组件的麻烦,那么本文中的方法,就非常实用了。
以给TStringGrid的单元格加上颜色功能为例,先看如何调用:
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, uColorGrid; type TStringGrid = class(uColorGrid.TStringGrid); // 此句必备! TForm1 = class(TForm) StringGrid1: TStringGrid; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TStringGrid } procedure TForm1.Button1Click(Sender: TObject); var c: TCellColor; begin // 设置颜色要在改变表格的行列数之后 c.TextColor := clblue; c.BackGroundColor := clyellow; StringGrid1.cells[2, 1] := 'blue'; StringGrid1.CellsColor[2, 1] := c; c.TextColor := clred; c.BackGroundColor := clgreen; StringGrid1.cells[3, 1] := 'red'; StringGrid1.CellsColor[3, 1] := c; c.TextColor := clgray; c.BackGroundColor := clnavy; StringGrid1.cells[4, 1] := 'yellow'; StringGrid1.CellsColor[4, 1] := c; end; procedure TForm1.Button2Click(Sender: TObject); var c: TCellColor; begin c.TextColor := clred; c.BackGroundColor := clolive; StringGrid1.cells[2, 1] := 'blue'; StringGrid1.CellsColor[2, 1] := c; c.TextColor := clblue; c.BackGroundColor := clMaroon; StringGrid1.cells[3, 1] := 'red'; StringGrid1.CellsColor[3, 1] := c; c.TextColor := clgray; c.BackGroundColor := clLime; StringGrid1.cells[4, 1] := 'yellow'; StringGrid1.CellsColor[4, 1] := c; end; end.