当我们在扩展一个 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. JAVA8新特性——接口定义增强

    JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ 接口定义增强 在JDK1.8以前,接口是定义的: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法 ...

  2. mysql数据简单去重

    我有一个 foo 表,定义了如下几个字段:id / a / b,其中 id 是主键,a,b 原本应该具有唯一性, 但因为程序 bug 导致 a,b 内容有重复,现在我要在 a,b 上加唯一索引,请问如 ...

  3. SQL Server 性能优化之RML Utilities:快速入门(Quick Start)(1)

      SQL Server 性能优化之RML Utilities:快速入门(Quick Start)(1) 安装Quick Start工具 RML(Replay Markup Language)是MS ...

  4. 数据库排序规则的冲突(理解collate Chinese_PRC_CI_AS)

    之前碰到了数据库排序规则冲突问题,即百度或者 Google 的老话题: “ 无法解决 equal to 操作中‘ sql_latin1_general_cp1_ci_as ’和‘ chinese_pr ...

  5. jsp:tld标签

    z注意每个uri地址要保持统一 1.创建MytagPrinta.java文件 package cn.tag; import java.io.IOException; import javax.serv ...

  6. Xcode 查找 TODO 清单

    在 Xcode 中按 Shift+Command+F,显示在项目中查找窗口,选择按正则表达式查找(Find > Regular Expression): TODO:  //\s*\bTODO\s ...

  7. 【VS外接程序】利用T4模板生成模块代码

    引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...

  8. 教你们在cmd里运行打开游戏,效率很快的。喜欢吧?

    第一步安装好的游戏 环境变量 变量值:把刚才复制好粘贴在里面,前面不用删除. 喜欢吧?这招非常好用.

  9. RecycleView出现折叠效果--第三方开源--SectionedExpandableGridRecyclerView

    下载地址:https://github.com/ddwhan0123/SectionedExpandableGridRecyclerView/archive/master.zip 具体见源码

  10. vue mint ui 手册文档对于墙的恐惧

    http://www.cnblogs.com/smallteeth/p/6901610.html npm 安装 推荐使用 npm 的方式安装,它能更好地和 webpack 打包工具配合使用. npm ...