当我们在扩展一个 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. 创建SVN仓库的步骤

    今天在客户现场联调,两个开发人员之间的代码想用SVN来管理,所以就临时在本地机器上搭建一个SVN仓库.现将操作步骤记录下来,以便后续查阅. 1.安装SVN服务端Setup-Subversion-1.6 ...

  2. Flume之核心架构深入解析

    我们一起来了解Source.Channel和Sink的全链路过程. 一.Flume架构分析 这个图中核心的组件是: Source,ChannelProcessor,Channel,Sink.他们的关系 ...

  3. Spring初学之spring的事务管理注解

    spring的事务管理,本文的例子是:比如你需要网购一本书,卖书的那一方有库存量以及书的价格,你有账户余额.回想我们在编程中要实现买书这样的功能,由于你的账户表和书的库存量表肯定不是同一张数据库表,所 ...

  4. Windows 摄像头数据

    1. FFmpeg获取DirectShow设备数据(摄像头,录屏) http://blog.csdn.net/leixiaohua1020/article/details/38284961 2.

  5. Thinkpad E440个性化设置:如何/禁用关闭触摸板?

    #如何禁用/关闭触摸版 默认情况下,ThinkPad E440是不支持触摸板的关闭功能,如果要关闭的话,需要去官方下载相应的鼠标驱动 UltraNav. 下载地址:http://think.lenov ...

  6. 智课雅思词汇---十八、前缀peri是什么意思

    智课雅思词汇---十八.前缀peri是什么意思 一.总结 一句话总结:前缀:peri- 表示“周围, 靠近” 词根:-peri- [词根含义]:试验,尝试 [词根来源]:英语experience, e ...

  7. dp3--codevs2598 编辑距离问题

    dp3--codevs2598 编辑距离问题 一.心得 1.字符串相关问题dp的时候从0开始是个陷阱 二.题目 2598 编辑距离问题  时间限制: 1 s  空间限制: 128000 KB  题目等 ...

  8. Windows环境下CGAL的安装

    1 准备工作 下载cmake 下载CGAL安装包 学习如何设置环境变量 安装Qt运行demos. libQGLViewer用来运行 3D CGAL demos. 确定Visual Studio 相应的 ...

  9. CentOS 6.6 中jdk1.6的安装和配置方法

    Linux中JDK1.6的安装和配置方法 一.安装 创建安装目录,在/usr/java下建立安装路径,并将文件考到该路径下: # mkdir /usr/java 1.jdk-6u11-linux-i5 ...

  10. JProfiler连接weblogic

    转 http://blog.csdn.net/xu1314/article/details/7737236