通过实例看VCL组件开发全过程(三)

news/2024/7/4 0:51:57 标签: delphi, function, class, tools, image, string
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">  
class="article-detail" id="divDetail">

class="MsoNormal">三、添加组件图标、注册组件的属性类别:

class="MsoNormal">在前面的文章中我们已经完成了组件的基本功能的开发。但是遗憾的是一但你安装了组件包,你会发现组件显示在class="tags" href="/tags/DELPHI.html" title=delphi>delphi组件页中的图标并不能清楚的说明我们组件的功能(由于我们的组件继承自TcustomLabel,图标是一个默认的class="tags" href="/tags/DELPHI.html" title=delphi>delphiVCL的图标,如果组件继承自其它已经出现在组件面板中的组件,图标还会和已有组件一样!)。显然一个好的组件特别是一个要发布的商业化组件需要一个有自己特色的目标,下面我们便来完成这一工作:

class="MsoNormal">打开class="tags" href="/tags/DELPHI.html" title=delphi>delphi自带的Image EditorToolsàImage Editor),新建一个组件资源(fileànewàComponent Resource File (.dcr)),在弹出的窗口中右键单击new新建一个bitmap位图资源调整好位图的大小(我们用24*24)和色深后确定,双击建立好的位图名字还是做图(做图工具的使用基本和windows自带的画图程序差不多,这里略过),完成后我们需要为位图文件另取一个名字(右键点击bitmap),因为class="tags" href="/tags/DELPHI.html" title=delphi>delphi强制要求这个位图的名字要和组件的名字一样,并且要全部大写,这里我们就取为:TCLOCK。最后保存这个资源文件到我们的组件包(dpk文件)目录,命名为ClockDcr.dcr。最后在Clock的代码中的interface部分加入一个编译器开关:{$R ClockDcr.dcr}然后重新编译更新组件(还记得怎么更新吗?),这时的组件图标已经变成我们刚才做的位图了!

class="MsoNormal">接下来我们将为我们开发的组件的属性进行分类并介绍一个组件开发中重要的特性:属性类别。

class="MsoNormal">为了让我们组件的一些和时钟有关的属性注册成一个新的类别把它们和label的属性分开开来,让组件用户能够更容易的发现组件的新特性,我们继承了属性类别的基类TpropertyCategory(在class="tags" href="/tags/DELPHI.html" title=delphi>delphi5中这需要引用单元DsgnIntf,不过应该特别注意在class="tags" href="/tags/DELPHI.html" title=delphi>delphi7中已经没有了这个基类,也没有这个单元文件,注册新的属性类别可以通过直接使用RegisterPropertyInCategory这种简单的办法完成,在下面的代码中会在相应的地方同时给出两种方法并说明他们的不同。)并覆盖它的两个类方法,最后在Register过程中用RegisterPropertyInCategory(在class="tags" href="/tags/DELPHI.html" title=delphi>delphi5中在DsgnIntf单元,在class="tags" href="/tags/DELPHI.html" title=delphi>delphi7中在DesignIntf单元,注意:class="tags" href="/tags/DELPHI.html" title=delphi>delphi的一些单元并没有被安装,包括我们这里指出的这两个单元和将要在后文中指出的单元,这些单元属于class="tags" href="/tags/DELPHI.html" title=delphi>delphiopen tools api是用来方便我们,特别是组件开发者用来扩展class="tags" href="/tags/DELPHI.html" title=delphi>delphi。如果你的class="tags" href="/tags/DELPHI.html" title=delphi>delphi没有这些单元,请将class="tags" href="/tags/DELPHI.html" title=delphi>delphi安装目录下的source文件夹里ToolsAPI文件夹中的pas文件拷贝到lib目录下,在你第一个需要用到这些单元的程序编译时class="tags" href="/tags/DELPHI.html" title=delphi>delphi会自动编译这些单元)方法注册属性类别。我们把以下的部分代码补充进我们开发的组件的原代码中:

class="MsoNormal"> 

class="MsoNormal">uses

class="MsoNormal"> DesignIntf;//class="tags" href="/tags/DELPHI.html" title=delphi>delphi7//class="tags" href="/tags/DELPHI.html" title=delphi>delphi5DsgnIntf

class="MsoNormal"> 

class="MsoNormal">///这部分代码如果是class="tags" href="/tags/DELPHI.html" title=delphi>delphi7就不需要了///

class="MsoNormal">type

class="MsoNormal"> TClockGategory=class(TpropertyCategory)//建立一个新的属性类别

class="MsoNormal">  Class class="tags" href="/tags/FUNCTION.html" title=function>function Name:string;override;//属性类别的名称

class="MsoNormal">  Class class="tags" href="/tags/FUNCTION.html" title=function>function Description:string;override;//属性类别的描述

class="MsoNormal"> End;

class="MsoNormal">……

class="MsoNormal">Class class="tags" href="/tags/FUNCTION.html" title=function>function TClockGategory .Name:string;

class="MsoNormal">Begin

class="MsoNormal"> Result:=’ClockPro’;

class="MsoNormal">End;

class="MsoNormal"> 

class="MsoNormal">Class class="tags" href="/tags/FUNCTION.html" title=function>function TClockGategory . Description:string;

class="MsoNormal">Begin

class="MsoNormal"> Result:=’Our Component Clock Description’;

class="MsoNormal">End;

class="MsoNormal">

class="MsoNormal">接下来我们要做的就是修改register过程:

class="MsoNormal"> 

class="MsoNormal">procedure Register;

class="MsoNormal">begin

class="MsoNormal">  RegisterComponents('ClockAndTime', [TClock]);

class="MsoNormal">  这是class="tags" href="/tags/DELPHI.html" title=delphi>delphi7的代码/

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'State');

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'Active');

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'BeginTime');

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'WakeTime');

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'AllowWake');

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'OnWakeUp');

class="MsoNormal">  RegisterPropertyInCategory('ClockPro',TClock,'OnTimeUp');

class="MsoNormal">  //

class="MsoNormal">  ///这是class="tags" href="/tags/DELPHI.html" title=delphi>delphi5的代码/

class="MsoNormal">  {

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'State');

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'Active');

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'BeginTime');

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'WakeTime');

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'AllowWake');

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'OnWakeUp');

class="MsoNormal">   RegisterPropertyInCategory(TClockGategory,TClock,'OnTimeUp');

class="MsoNormal">  }

class="MsoNormal"> 

class="MsoNormal">end;

class="MsoNormal">重新编译后,做一个测试程序,这时只要组件使用者右键单击Object Inspector选择ArrangeàBy Category就可以看到属性已经被清楚的分类了,如下图:

class="MsoNormal">

class="MsoNormal">然而,应该清楚的是属性类别绝对不能被滥用,因为过多的使用该技术会使组件使用者为了找到某一个属性变的更加麻烦和摸不着头脑。

class="MsoNormal">在接下来的文章里,我们将继续研究两个很有用的组件特性。


http://www.niftyadmin.cn/n/1413561.html

相关文章

非常可乐(搜索)

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫…

Java 枚举类详解

1. 枚举类定义 在某些情况下,一个类的对象是有限而且固定的,比如季节类,它只有4个对象,这种实例有限而且固定的类,在Java里被称为枚举类。 2. 早期实现枚举的方式 public static final int SEASON_SPRING 1; public s…

通过实例看VCL组件开发全过程(四)

四、组件属性编辑器和组件编辑器:通过上面的努力我们的组件似乎已经比较完美了,可我们也忽略了一些重要的细节和一些有趣的事情,这一篇我们将研究两个很有用的组件特性: 在之前开发组件核心功能时我们曾设置了两个属性BeginTime和…

J2ObjC 1.0 发布,将 Java 转换为 Objective-C

J2ObjC 是一个Google开发的开源工具,用于将Java代码转换为Objective-C代码。其目的是为了能在iOS平台上重用Android平台、web服务器端的Java代码。服务器端代码的转换由 GWT 完成。J2ObjC并不转换UI代码,这部分需要针对不同平台分别开发。 我们在2012年发…

Delphi自定义部件开发(1)

Delphi除了支持使用可视化部件所见即所得地建立应用程序外,还支持为开发应用而设计自己的部件。在本章中将阐述如何为Delphi应用程序编写部件。这一章将达到两个目的:● 教你如何自定义部件● 使你的部件成为Delphi环境的有机组合部分19.1 Delphi部件原理…

jquery获取节点的时候获取包含自己在内的HTML标签

jquery获取某个标签的html()方法的时候总是只能获取内部的 如果获取包含自身的HTML代码呢&#xff1f; 用.prop("outerHTML")方法获取 <div id"my_id"><p>hello word&#xff01;</p></div> <script> var html$("#my_i…

行为型模式-策略模式(一)

今天就说一说设计模式中的策略模式&#xff0c;从名字来讲&#xff0c;意思就是&#xff0c;对应不同的情况&#xff0c;就有一种解决问题的办法&#xff0c;不同的情况&#xff0c;就有不同的应对方法&#xff0c;这就是策略模式&#xff0c;非常的智能化。 也可以参考菜鸟 …

Mathematically Hard(小于n且与n互质的数的个数==欧拉函数,欧拉表)

题意&#xff1a; 先说值吧&#xff0c;函数是求n之前的数与n互质的数的个数&#xff0c;术语&#xff1a;欧拉值。欧拉公式上图也给出来了。那么这道题求的是区间[a,b] 的欧拉值的平方之和。 #include<stdio.h> #include<string.h> #include<iostream> #in…