当我们在扩展一个 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[, ] := 'blue';
StringGrid1.CellsColor[, ] := c; c.TextColor := clred;
c.BackGroundColor := clgreen;
StringGrid1.cells[, ] := 'red';
StringGrid1.CellsColor[, ] := c; c.TextColor := clgray;
c.BackGroundColor := clnavy; StringGrid1.cells[, ] := 'yellow';
StringGrid1.CellsColor[, ] := c; end; procedure TForm1.Button2Click(Sender: TObject);
var
c: TCellColor;
begin c.TextColor := clred;
c.BackGroundColor := clolive; StringGrid1.cells[, ] := 'blue';
StringGrid1.CellsColor[, ] := c; c.TextColor := clblue; c.BackGroundColor := clMaroon;
StringGrid1.cells[, ] := 'red';
StringGrid1.CellsColor[, ] := c; c.TextColor := clgray;
c.BackGroundColor := clLime;
StringGrid1.cells[, ] := 'yellow';
StringGrid1.CellsColor[, ] := c; end; end.

unit1.pas

以下为TStringGrid扩展功能的代码

unit uColorGrid;

interface

uses
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids; type TCellColor = record
TextColor: TColor; // 格子文字的颜色
BackGroundColor: TColor; // 格子背景的颜色
ColorChanged: boolean;
end; TCellColorArr = array of array of TCellColor; TStringGrid = class(Vcl.Grids.TStringGrid)
private
FCellColorArr: TCellColorArr; // 记录单元格颜色
function GetCellsColor(ACol, ARow: integer): TCellColor;
procedure SetCellsColor(ACol, ARow: integer; const Value: TCellColor);
procedure InitCellsColor(ACol, ARow: integer);
protected
procedure SizeChanged(OldColCount, OldRowCount: Longint); override; // 在此过程中调整颜色记录数组的大小
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
public
constructor Create(AOwner: TComponent); override;
property CellsColor[ACol, ARow: integer]: TCellColor read GetCellsColor write SetCellsColor;
end; implementation constructor TStringGrid.Create(AOwner: TComponent);
begin
inherited;
InitCellsColor(ColCount, RowCount);
end; procedure TStringGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
var
CC: TCellColor;
oldBrush: Tbrush;
oldPen: tpen;
oldFont: tfont;
begin
CC := FCellColorArr[ARow, ACol];
if CC.ColorChanged then
begin
oldBrush := self.Canvas.Brush;
oldPen := self.Canvas.Pen;
oldFont := self.Canvas.Font;
Canvas.Font.Color := CC.TextColor; // 文字颜色
Canvas.Brush.Color := CC.BackGroundColor; // 背景色
inherited;
Canvas.Brush := oldBrush;
Canvas.Pen := oldPen;
Canvas.Font := oldFont;
end
else
inherited;
end; function TStringGrid.GetCellsColor(ACol, ARow: integer): TCellColor;
begin
result := FCellColorArr[ARow, ACol];
end; procedure TStringGrid.InitCellsColor(ACol, ARow: integer);
var
i, j: integer;
begin
setlength(FCellColorArr, ARow);
for i := to ARow - do
begin
setlength(FCellColorArr[i], ACol);
for j := to ACol - do
begin
FCellColorArr[i, j].ColorChanged := false; //初始化
end;
end; end; procedure TStringGrid.SetCellsColor(ACol, ARow: integer; const Value: TCellColor);
begin
FCellColorArr[ARow, ACol] := Value;
FCellColorArr[ARow, ACol].ColorChanged := true;
end; procedure TStringGrid.SizeChanged(OldColCount, OldRowCount: Longint);
begin
inherited;
InitCellsColor(ColCount, RowCount);
end; end.

uColorGrid.pas

本文示例代码下载

效果图

参考文章:https://www.cnblogs.com/delphi7456/p/5349619.html

delphi VCL组件同名继承的更多相关文章

  1. CSDN论坛 > Delphi > VCL组件开发及应用 DBLookupComboBox用法

    (1)DataSource属性    该属性用于连接要编辑数据的主表数据源(2)DataField属性    该属性用于指定要编辑的数据字段名(3)ListSource属性    .    该属性用于 ...

  2. Delphi的组件读写机制

    Delphi的组件读写机制(一) 一.流式对象(Stream)和读写对象(Filer)的介绍在面向对象程序设计中,对象式数据管理占有很重要的地位.在Delphi中,对对象式数据管理的支持方式是其一大特 ...

  3. Delphi 第三方组件

    TMS Component Pack v7.0.0.0 TMS Component Pack 版本为Delphi和C++ Builder提供了超过350个VCL组件,用以创建功能丰富的.现代的和原生W ...

  4. Delphi xe7组件和控件的安装方法

    暂时我所遇到的所有控件安装方法大体与下面两种相同. 若有不同大家提出来,一起想办法解决. .dproj格式的组件安装方法: raise组件 安装详细步骤如下: 一.设置搜索路径1. 将本包中的文件连同 ...

  5. Blazor入门笔记(2)-分部类组件与组件的继承

    1.前言 本文接自Blazor的组件(1)-从0构建一个组件 2.分部类组件 Razor组件你可理解为就是一个类名与文件名相同的类,因此,可以新建一个同名的partial类,将组件中@code里面的代 ...

  6. Hibernate缓存、组件、继承映射

    Hibernate缓存.组件.继承映射 三种状态: 临时状态:不受session管理,没有提交到数据库:没有执行sql之前,new对象的时候: 持久化状态:受session管理,提交到数据库:正在执行 ...

  7. Z Order of Controls in Delphi VCL

    Get and set the Z Order of controls at runtime in Delphi VCL. If you are looking for a FireMonkey so ...

  8. 与 QWidget 有关的 Qt 可视化组件的继承关系图

    与 QWidget 有关的 Qt 可视化组件的继承关系图

  9. Delphi IdHttp组件+IdHttpServer组件实现文件下载服务

     http://blog.csdn.net/xxkku521/article/details/16864759 Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 2013- ...

随机推荐

  1. UVA 12716 GCD XOR (异或)

    题意:求出[1,n]中满足gcd(a,b)=a xor b,且1<=a<=b<=n的对数 题解:首先a xor b = c,则a xor c = b,而b是a的约数,则可以使用素数筛 ...

  2. vc 判断当前用户是否在管理员组以及是否SYSTEM权限运行

    BOOL IsUserInAdminGroup() //判断是否在管理员组 { BOOL fInAdminGroup = FALSE; HANDLE hToken = NULL; HANDLE hTo ...

  3. jquery01-简介+语法+选择器+事件

    jQuery是一个JavaScript函数库,是一个轻量级的"写的少,做的多"的JavaScript库,包含以下功能: HTML 元素选取 HTML 元素操作 CSS 操作 HTM ...

  4. File类之在指定目录中查找文件

    package IoDemo; import java.io.File; /** * @Title:FileDemo2 * @Description:在指定的目录中查找文件 * @author Cra ...

  5. QT 使用QTcpServer QTcpSocket 建立TCP服务器端 和 客户端

    1.  如图客户端连接server后,server发送"hello tcp" 给客户端 2. 实例代码 -----------------------------------  s ...

  6. Rem是什么,牛逼的Vue,Epub竟然可以实现阅读器功能。太牛了。

    rem 相当于根元素font-size值的倍数. 1rem=根元素font-size 2rem=根元素font-size * 2 // 1rem = fontSize px // 1px = (1/f ...

  7. 0.00-050613_Makefile

    # Makefile for the simple example kernel. AS86 =as86 -0 -a LD86 =ld86 -0 AS =gas LD =gld LDFLAGS =-s ...

  8. Hibernate中使用Spring Data JPA

    一.配置文件的方式 1.pom.xml中引入相关依赖 <properties> <project.build.sourceEncoding>UTF-8</project. ...

  9. Get UTI (uniform type identifier) and ContentType

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  10. mac上获取手机的uuid

    把手机连上mac 终端中输入: system_profiler SPUSBDataType | grep "Serial Number:.*" 修改用 | sed s#" ...