有关RTTI,请问如何从一个控件中通过一个事件名称得到该事件的类型信息PTypeInfo(有一定难度)?

dejoy 2007-11-25 10:20:50
有关RTTI,请问如何从一个控件中通过一个事件名称得到该事件的类型信息PTypeInfo(有一定难度)?
在此虚心请教各位达人。
比如给定TButton,及名称'OnClick',如何得到OnClick对应的事件的PTypeInfo?
函数定义如下:
function GetEventTypeInfo(ComponentClass:TPersistentClass; EventName: string):PTypeInfo;
begin
//??? 这里如何实现?
end;
因为只知道一个类名和一个事件名,而不知道具体的事件类型,所以不能直接使用TypeInfo(TNotifyEvent)这样的方式直接取得事件的类型信息,而要通过类和事件名来取得,事件是published的。
...全文
140 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
oushengfen 2007-11-29
  • 打赏
  • 举报
回复
呵呵,高手,不错
dejoy 2007-11-28
  • 打赏
  • 举报
回复
我简单的解释一下用途。
取得PTypeInfo有什么用呢=>>
当然有用,在delphi7中的DesignEditors.pas中有一个函数:
procedure RegisterPropertyEditor(PropertyType: PTypeInfo; ComponentClass: TClass;
const PropertyName: string; EditorClass: TPropertyEditorClass);
其中第一个参数PropertyType需要传入的就是一个PTypeInfo类型的。简单的解释下用途,
我要用GetEventTypeInfo动态的取得一个类(不是类的实例)的published的事件的PTypeInfo,如果该类存在这个事件,就传到RegisterPropertyEditor动态注册一个该事件的PropertyEditor。
比如GetEventTypeInfo(TButton,'OnClickXXX'),GetEventTypeInfo(TButton,'OnClick')等等,其中两个参数都是动态的,未知的,如果返回的不是Nil,那就说明这个类有这个事件,我就把它给注册了~~
luxuewei5214 2007-11-26
  • 打赏
  • 举报
回复
不懂,up
sdzeng 2007-11-26
  • 打赏
  • 举报
回复
就是这样用的

再进一步就可以动态创建、设置属性方法
http://blogs.codegear.com/abauer/2007/06/14/36043
king_xing 2007-11-26
  • 打赏
  • 举报
回复
TypInfo.GetTypeData()也可以获得很多有用的东西!
etomahawk 2007-11-26
  • 打赏
  • 举报
回复
function GetEventTypeInfo(ComponentClass:TPersistentClass; EventName: string):PTypeInfo;
var
p:PPropInfo;
begin
p:= GetPropInfo(ComponentClass,EventName,tkMethods);
if p <> nil then
result := p^.PropType^;
end;

-------------------------------------------------
嗯,我就是这样用的。
yi10000 2007-11-26
  • 打赏
  • 举报
回复
帮顶...
dejoy 2007-11-25
  • 打赏
  • 举报
回复
通过分析Typinfo.pas,初步实现了如下代码,希望有达人能进一步。
function GetEventTypeInfo(ComponentClass:TPersistentClass; EventName: string):PTypeInfo;
var
p:PPropInfo;
begin
p:= GetPropInfo(ComponentClass,EventName,tkMethods);
if p <> nil then
result := p^.PropType^;
end;
dejoy 2007-11-25
  • 打赏
  • 举报
回复
调用形式:GetEventTypeInfo(TButton,'OnClick');

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧