Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the handle of a form 2

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Ok, is it ossible using findwindow to get the handle of a form
eg i ahve 3 forms in an app.
Main is the one i want to find the handle for from another app as i have hooked main and need it to receive a message.

I can find the handle for the app, but it is not the same as the handle for main

Any idea?

}...the bane of my life!
 
cool will check it out
This has nothing to do with link name od the form?

If i hook findwindow(vbnullstring,app.title)

I can send a message to it

if i hook me.hwnd it gives me a different handle!!

}...the bane of my life!
 
That's because they are different windows. Every VB top-level form (i.e. not MDIChildren) is actually owned1 by a toplevel, 0 x 0 pixel window to which all messages and events are initially sent, whose caption is the application title3.

>This has nothing to do with link name od the form?

Well possibly. FindWindow will work quite happily in locating VB forms. But it isn't very flexible. Given the search criteria of class name and window caption, it just finds the first matching window in the window list2. If your form's caption is the same as your app title, and you are only searching on window caption then there's no guarantee that you'll get the right handle back.

So you need to either

a) be more specific in the search. If you want a VB form (or MDI parent) set the class name to ThunderRT6FormDC (or ThunderRT6MDIForm) for compiled apps and ThunderFormDC (or ThunderMDIForm) for the IDE. Of course, if you have two VB apps running with forms with the same name, then you'll still have problems

b) ensure your form has a unique name

1. Having an owner is subtly different from having a parent, in that the owned windows can still be a top-level window (and thus be found by FindWindow). If it has a parent, then it can only be a child window.

2. A combination of EnumWindows and the function BobRodes mentioned, EnumChildWindows, will allow you to walk this window list (which is also visible in the Spy++ application from the Visual Studio Tools menu)

3. This window has a class of ThunderRT6Main at runtime, and ThunderMain in the IDE

 
That is fab. The app is basically sending a message to a previous instance so by that i think i need to rename the new instance, find the handle of the old instnce then walk throught the window list untill i get the handle for main.

Does that sound about right?

}...the bane of my life!
 
Sounds right.

(Still think you ought to try out the COM method though... ;-) )
 
It was down to me not renameing the caption of the new instance so that the message is sent to the previous instance not the new instance!! (i thought i just had to change app.title :( )
Cheers guys.

Oh star for the enumchilds thing as i have written an app to list all windows open with their class name and thier caption / title ...very handy :)

}...the bane of my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top