我 如何用 SetPropValue函数 为含有子项目的属性赋值?多谢!!!希望大家健康,躲过“非典”劫难!
//uses typinfo
function GetObjectProperty(
const AObject : TObject;
const APropName : string
):TObject;
var
PropInfo:PPropInfo;
begin
Result := nil;
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind = tkClass) then
Result := GetObjectProp(AObject,PropInfo);
end;
function SetIntegerPropertyIfExists(
const AObject : TObject;
const APropName : string;
const AValue : integer
):Boolean;
var
PropInfo:PPropInfo;
begin
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind = tkInteger) then
begin
SetOrdProp(AObject,PropInfo,AValue);
Result:=True;
end else
Result:=False;
end;
//调用
procedure TFrmTest.FormCreate(Sender: TObject);
var
objTemp : TObject;
begin
objTemp := GetObjectProperty(Self,Font);
if Assigned(objTemp) then
SetIntegerPropertyIfExists(objTemp,Size,9);
end;
//属性不能是集合,如TDBGrid.Columns
{our callback function prototype}
function EnumWinProps(hWnd: HWND; pString: PChar; Data: THandle): BOOL; stdcall;
var
Form1: TForm1;
implementation
{these steps will be executed for each property
entry in the windows property list}
function EnumWinProps(hWnd: HWND; pString: PChar; Data: THandle): BOOL;
begin
{add the string and associated value to the list box}
Form1.ListBox1.Items.Add(Format(%s=%d,[pString,Data]));
{continue enumeration}
Result:=TRUE;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
{add a new property to the windows property list}
SetProp(Form1.Handle,PChar(Edit1.Text),StrToInt(Edit2.Text));
{clear the edit boxes}
Edit1.Text:=;
Edit2.Text:=;
{clear the list box}
Form1.ListBox1.Items.Clear;
{list all of the properties associated with the window}
EnumProps(Form1.Handle, @EnumWinProps);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
{clear the list box}
Form1.ListBox1.Items.Clear;
{list all of the properties associated with the window}
EnumProps(Form1.Handle, @EnumWinProps);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
{remove the selected property from the property list}
RemoveProp(Form1.Handle,PChar(Copy(ListBox1.Items[ListBox1.ItemIndex],
0,Pos(=,ListBox1.Items[ListBox1.ItemIndex])-1)));
{clear the list box}
Form1.ListBox1.Items.Clear;
{list all of the properties associated with the window}
EnumProps(Form1.Handle, @EnumWinProps);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
Data: THandle; // this stores the property entry data
begin
{get property entry data associated with the given string}
Data:=GetProp(Form1.Handle,PChar(Edit1.Text));
{if there was a property value returned...}
if (Data<>0) then
{...display it...}
Edit2.Text:=IntToStr(Data)
else
{...otherwise display an error message}
Edit2.Text:=No property found.;
end;
The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl