Friday, June 18, 2004

Same friend, only one reading this blog asked for more...

I was going to say skin a cat but that would make him winch, so I am saying it any way because it is a saying...
The second way to get all Winforms in your app...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


procedure TWinForm.Button4_Click(sender: System.Object; e: System.EventArgs);
const
AllBindingFlags : BindingFlags = BindingFlags.Instance or BindingFlags.Static or
BindingFlags.Public or BindingFlags.NonPublic or
BindingFlags.FlattenHierarchy;
var
MSCorlibModule : Module;
HandleBucketType : System.Type;
hashBucketField : FieldInfo;
hashBuckets : System.Array;
HandleBucket : TObject;
HandleField : FieldInfo;
WinHandle : IntPtr;
FoundControl : Control;
i : integer;
begin
MSCorlibModule := Self.GetType.BaseType.Module;
if not Assigned(MSCorlibModule) then Exit;

hashBucketField := Typeof(NativeWindow).GetField('hashBuckets',AllBindingFlags);
//if there are no hash buckets we might as well end it here.
if not Assigned(hashBucketField) then Exit;
hashBuckets := System.Array(hashBucketField.GetValue(nil));
if not Assigned(hashBuckets) then Exit;

HandleBucketType := MSCorlibModule.GetType('System.Windows.Forms.NativeWindow+HandleBucket');
if not Assigned(HandleBucketType) then Exit;

HandleField := HandleBucketType.GetField('handle',AllBindingFlags);
if not Assigned(HandleField) then Exit;

for i := 0 to hashBuckets.Length - 1 do
begin
HandleBucket := hashBuckets.GetValue(i);
if Assigned(HandleBucket) then
begin
WinHandle := IntPtr(HandleField.GetValue(HandleBucket));
if (WinHandle.ToInt32 <> 0) then
begin
FoundControl := Control.FromHandle(WinHandle);
if FoundControl is System.Windows.Forms.Form then
MessageBox.Show(FoundControl.Text);
end;
end;
end;
end;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Delphi code again...

1 Comments:

Blogger Hallvard Vassbotn said...

Hi Roy,

You gotta write larger and narrower (or wrappable) code... :)

22 June 2004 at 10:43  

Post a Comment

<< Home