delphi VCL组件同名继承
当我们在扩展一个 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组件同名继承的更多相关文章
- CSDN论坛 > Delphi > VCL组件开发及应用 DBLookupComboBox用法
(1)DataSource属性 该属性用于连接要编辑数据的主表数据源(2)DataField属性 该属性用于指定要编辑的数据字段名(3)ListSource属性 . 该属性用于 ...
- Delphi的组件读写机制
Delphi的组件读写机制(一) 一.流式对象(Stream)和读写对象(Filer)的介绍在面向对象程序设计中,对象式数据管理占有很重要的地位.在Delphi中,对对象式数据管理的支持方式是其一大特 ...
- Delphi 第三方组件
TMS Component Pack v7.0.0.0 TMS Component Pack 版本为Delphi和C++ Builder提供了超过350个VCL组件,用以创建功能丰富的.现代的和原生W ...
- Delphi xe7组件和控件的安装方法
暂时我所遇到的所有控件安装方法大体与下面两种相同. 若有不同大家提出来,一起想办法解决. .dproj格式的组件安装方法: raise组件 安装详细步骤如下: 一.设置搜索路径1. 将本包中的文件连同 ...
- Blazor入门笔记(2)-分部类组件与组件的继承
1.前言 本文接自Blazor的组件(1)-从0构建一个组件 2.分部类组件 Razor组件你可理解为就是一个类名与文件名相同的类,因此,可以新建一个同名的partial类,将组件中@code里面的代 ...
- Hibernate缓存、组件、继承映射
Hibernate缓存.组件.继承映射 三种状态: 临时状态:不受session管理,没有提交到数据库:没有执行sql之前,new对象的时候: 持久化状态:受session管理,提交到数据库:正在执行 ...
- 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 ...
- 与 QWidget 有关的 Qt 可视化组件的继承关系图
与 QWidget 有关的 Qt 可视化组件的继承关系图
- Delphi IdHttp组件+IdHttpServer组件实现文件下载服务
http://blog.csdn.net/xxkku521/article/details/16864759 Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 2013- ...
随机推荐
- 【转】jQuery对象与DOM对象之间的转换方法
刚开始学习jquery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 什么是jQuery对 ...
- 域名解析中TTL是什么意思
在做域名解析的时候都会看到一个叫“TTL”的值,一般都有一个默认的值,不过不同注册商默认的值也会不一样,常见的是3600和7200这两个值. 另外ping的时候也可以看到“TTL=XXX”的字样,(如 ...
- localstorage存储对象
之前在客户端存储数据一直用的是cookie,由于有大小等限制,随着html5时代的到来,现在大多数用的是localstorage存储数据: 例如: localStorage.setItem(" ...
- php:PHP解析xml的4种方法
XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML, XMLReader, DOMDocument. ...
- hzau 1202 GCD(矩阵快速幂)
1202: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 201 Solved: 31[Submit][Status][Web Board] ...
- Https---SSL协议
ssl协议的起源和历史我就不再多说了,就是那个Netscape 网景公司开发的,它的作用主要是提供了一种安全传输方式,我们知道网上有很多的时候需要我们去输入用户名和密码,那么假设我们自己的电脑防病毒还 ...
- JDK所有版本
http://www.oracle.com/technetwork/java/archive-139210.html
- 用urliso把linux刻录U盘失败无数次。 用unetbootin试试可以启动的。
我用的是ubuntu 16.04 lts 画面挺好用的.
- Servlet细节处理
1.Servlet的线程安全问题 尽管servlet的工作效率高,但是线程也是不安全的,因为一个类型的Servlet只有一个实例对象,那么就有可能会出现一个Servlet同时处理多个请求 解决方案:所 ...
- git,npm,bower设置代理地址
我們先假設代理伺服器的位址為: http://10.0.0.1:8080 設定 Git 使用代理伺服器 輸入兩行指令即可設定完畢: git config --global https.proxy ht ...