设为首页 加入收藏

TOP

dephi RTI (Runtime Type Information)获取运行时的控件信息
2023-08-06 07:49:28 】 浏览:237
Tags:dephi RTI Runtime Type Information 时的控

var
Edit: TComponent;
begin
Edit := FindComponent("Edit1");
If Edit is TEdit then
TEdit(Edit).Text := '你好 Delphi7';
end;

 

 

RTTI(RunTime Type Information): 运行时类型信息, 就是在程序运行后也能得到类型(譬如 TButton 类)的信息.

 

这在早期主要用于 IDE 设计时, 譬如把一个 Button 放到窗体后, 此时我们的程序虽然没有运行, 但在 Delphi 的 IDE 编辑环境中, 这个 Button 已经是在运行状态(要不然IDE怎么才能显示我们要求的TButton呢); 此时我们对 Button 的属性等信息的设置都是通过 RTTI 技术实现的.

 

但在 Delphi 2007 之前, 能够获取 RTTI 信息是有限的.

Delphi 2009 增加了 ObjAuto 单元、Delphi 2010 增加的 RTTI 单元, 这都可以让程序在运行时对类型有更多掌控(副作用是最后生成的程序越来越大).?

-------------------------------------例子1----------------------------------------

Uses Rtti, TypInfo;

procedure TForm1.Button3Click(Sender: TObject);

begin

ShowMessage(GetEnumName(TypeInfo(TFormStyle), Ord(FormStyle)));

end;

主窗体上放2个Label,3个Button,然后

procedure TForm1.Button1Click(Sender: TObject);

var

i: integer;

PropInfo: PPropInfo;

begin

for I := 0 to ComponentCount-1 do

begin

PropInfo:=GetPropInfo(Components[i].ClassInfo, 'Color');

if PropInfo <> nil then

SetOrdProp(Components[i], PropInfo, clBlue);

end;

end;


-----------------------------------
Delphi的RTTI(许多参考链接)
https://blog.51cto.com/u_15127692/4693281

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇mormot2 笔记(二) Http服务的简单.. 下一篇AI智能问答助手 AI智能批量文章生..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目