如何得知某个property是否属于某个控件?(100分)

  • 主题发起人 主题发起人 melice
  • 开始时间 开始时间
M

melice

Unregistered / Unconfirmed
GUEST, unregistred user!
我希望得知某一个控件是否有某个published的property,但是使用
fieldaddress的时候,返回的参数总是nil。。。
例如我想知道控件A有没有caption属性,该怎么处理?
 
使用 TypInfo 单元中 GetPropInfo(Acomp, PropName)即可
属性 PropName 存在于 Acomp 中,则返回不为 nil;反之则返回 nil
但 broland 不推荐使用该函数。
 
function PropType(Instance: TObject; const PropName: string): TTypeKind;
begin
Result := PropType(Instance.ClassType, PropName);
end;

function PropType(AClass: TClass; const PropName: string): TTypeKind;
begin
Result := FindPropInfo(AClass, PropName)^.PropType^^.Kind;
end;


implementation
uses TypInfo;
//////请注意引用这个单元,这个单元在Delphi6的/Program Files/Borland/Delphi6/Source/Rtl/Common中,

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var PT:TTypeKind;
begin
try
PT:=PropType(Button1,'aa');
ShowMessage(IntToStr(ORD(PT)));
Except
On E:Exception do
ShowMessage(E.Message);
end;
end;

end.
 
如果我要对这个属性进行赋值呢?
 
procedure SetBoolPropValue(Acomp: TComponent; PropName: string; Value: boolean);
// uses TypInfo
var
p: PPropInfo;
begin
p := GetPropInfo(Acomp, PropName);
if p <> nil then
if p^.PropType^.Kind = tkEnumeration then
SetOrdProp(Acomp, p, integer(Value));
end;
只要将 Value 的类型改为你需要的类型
 
procedure SetInt64Prop(Instance: TObject; const PropName: string;
const Value: Int64);
begin
SetInt64Prop(Instance, FindPropInfo(Instance, PropName), Value);
end;


procedure SetOrdProp(Instance: TObject; const PropName: string;
Value: Longint);
begin
SetOrdProp(Instance, FindPropInfo(Instance, PropName), Value);
end;

procedure SetEnumProp(Instance: TObject; const PropName: string;
const Value: string);
begin
SetEnumProp(Instance, FindPropInfo(Instance, PropName), Value);
end;


procedure SetSetProp(Instance: TObject; const PropName: string;
const Value: string);
begin
SetSetProp(Instance, FindPropInfo(Instance, PropName), Value);
end;


procedure SetObjectProp(Instance: TObject; const PropName: string;
Value: TObject);
begin
SetObjectProp(Instance, FindPropInfo(Instance, PropName), Value);
end;

procedure SetStrProp(Instance: TObject; const PropName: string;
const Value: string);
begin
SetStrProp(Instance, FindPropInfo(Instance, PropName), Value);
end;

procedure SetWideStrProp(Instance: TObject; const PropName: string;
const Value: WideString);
begin
SetStrProp(Instance, FindPropInfo(Instance, PropName), Value);
end;

procedure SetFloatProp(Instance: TObject; const PropName: string;
const Value: Extended);
begin
SetFloatProp(Instance, FindPropInfo(Instance, PropName), Value);
end;

procedure SetVariantProp(Instance: TObject; const PropName: string;
const Value: Variant);
begin
SetVariantProp(Instance, FindPropInfo(Instance, PropName), Value);
end;

procedure SetMethodProp(Instance: TObject; const PropName: string;
const Value: TMethod);
begin
SetMethodProp(Instance, FindPropInfo(Instance, PropName), Value);
end;
 
多人接受答案了。
 
后退
顶部