首页  编辑  

如何判断一个控件(TControl)是否是动态创建出来的?

Tags: /超级猛料/Friends.网友专栏/zswang/   Date Created:

如何判断一个控件(TControl)是否是动态创建出来的?

function IsDynamicControl(mControl: TControl): Boolean;

var

 vHandle: THandle;

 vResourceName: PChar;

 vResourceStream: TResourceStream;

 vMemoryStream: TMemoryStream;

 I: Integer;

begin

 Result := True;

 if not Assigned(mControl) then Exit; //nil 算是动态的吧~~

 if mControl.Name = '' then Exit; //Name为空是动态的!~~

 if not Assigned(mControl.Owner) then Exit; //没有Owner是动态的!~~

 vResourceName := PChar(string(mControl.Owner.ClassName));

 vHandle := FindResource(HInstance, vResourceName, RT_RCDATA);

 if vHandle = 0 then Exit;

 vResourceStream :=

   TResourceStream.Create(HInstance, PChar(vResourceName), RT_RCDATA);

 vMemoryStream := TMemoryStream.Create;

 with TStringList.Create do try

   ObjectBinaryToText(vResourceStream, vMemoryStream);

   vMemoryStream.Position := 0;

   LoadFromStream(vMemoryStream);

   vHandle := 0;

   while Pos(#32#32, Text) > 1 do

     Text := StringReplace(Text, #32#32, #32, [rfReplaceAll]);

   for I := 0 to Count - 1 do

     if Pos('object ' + mControl.Name + ':', Strings[I]) in [1, 2] then begin

       vHandle := I + 1;

       Break;

     end;

   if vHandle = 0 then Exit;

 finally

   Free;

   vResourceStream.Free;

   vMemoryStream.Free;

 end;

 Result := False;

end; { IsDynamicControl }