Выставить значения свойств
Автор: Xavier Pacheco
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Panel1Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses TypInfo;
{$R *.DFM}
procedure SetIntegerPropertyIfExists(AComp: TComponent; APropName: string;
AValue: Integer);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkInteger then
SetOrdProp(AComp, PropInfo, AValue);
end;
end;
procedure SetObjectPropertyIfExists(AComponent: TComponent; APropName: string;
AValue: TObject);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComponent.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkClass then
SetObjectProp(AComponent, PropInfo, AValue);
end;
end;
procedure SetBooleanPropertyIfExists(AComp: TComponent; APropName: string;
AValue: Boolean);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkEnumeration then
SetOrdProp(AComp, PropInfo, Integer(AValue));
end;
end;
procedure SetStringPropertyIfExists(AComp: TComponent; APropName: string;
AValue: string);
var
PropInfo: PPropInfo;
TK: TTypeKind;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
TK := PropInfo^.PropType^.Kind;
if (TK = tkString) or (TK = tkLString) or (TK = tkWString) then
SetStrProp(AComp, PropInfo, AValue);
end;
end;
procedure SetMethodPropertyIfExists(AComp: TComponent; APropName: string;
AMethod: TMethod);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkMethod then
SetMethodProp(AComp, PropInfo, AMethod);
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
SetStringPropertyIfExists(Button1, 'Caption', 'Yahoo');
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
SetIntegerPropertyIfExists(Button2, 'Width', 50);
end;
procedure TMainForm.Button3Click(Sender: TObject);
begin
SetBooleanPropertyIfExists(Button3, 'Enabled', False);
end;
procedure TMainForm.Button4Click(Sender: TObject);
var
F: TFont;
begin
F := TFont.Create;
F.Name := 'Arial';
F.Size := 24;
F.Color := clRed;
SetObjectPropertyIfExists(Panel1, 'Font', F);
end;
procedure TMainForm.Button5Click(Sender: TObject);
begin
SetMethodPropertyIfExists(Button5, 'OnClick',
GetMethodProp(Panel1, 'OnClick'));
end;
procedure TMainForm.Panel1Click(Sender: TObject);
begin
ShowMessage(Button5.Caption);
end;
end.
|