Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: LivedKrad on February 26, 2006, 02:41 PM

Title: [Solved] Enabling a button
Post by: LivedKrad on February 26, 2006, 02:41 PM
Is it possible to enable a button on another program's form? I was told that the API EnableWindow() could do this, but one of the parameters is obviously a handle. I don't know how to grab the handle of a button on a remote form. Can anyone assist?
Title: Re: Enabling a button
Post by: K on February 26, 2006, 02:44 PM
Use FindWindow  (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindow.asp)to find the top level window.  Use FindWindowEx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindowex.asp)to enumerate through the child windows until you have found the button.  You will need to know some information about the windows, which you can find with Spy++ (which comes with Visual Studio).
Title: Re: Enabling a button
Post by: Warrior on February 26, 2006, 07:20 PM
Wouldn't you need Spy++ everytime you opened the application?
Title: Re: Enabling a button
Post by: K on February 26, 2006, 07:31 PM
No.  You just need to know the window text and/or class name of the main window, and the text and/or class name of the child window.  If there is more than one child with the same class at the same level in the window heirarchy, then you just need to call FindWindowEx() with the appropriate arguments to enumerate the siblings and find the correct one.

For example, the following code will find the "Defragment" button in the Windows XP Disk Defragmenter:


Dim mmcmainframe As Long, mdiclient As Long, mmcchildfrm As Long
Dim mmcviewwindow As Long, mmcocxviewwindow As Long, atlaxwinex As Long
Dim atlffa As Long, button As Long
mmcmainframe = FindWindow("mmcmainframe", vbNullString)
mdiclient = FindWindowEx(mmcmainframe, 0&, "mdiclient", vbNullString)
mmcchildfrm = FindWindowEx(mdiclient, 0&, "mmcchildfrm", vbNullString)
mmcviewwindow = FindWindowEx(mmcchildfrm, 0&, "mmcviewwindow", vbNullString)
mmcocxviewwindow = FindWindowEx(mmcviewwindow, 0&, "mmcocxviewwindow", vbNullString)
atlaxwinex = FindWindowEx(mmcocxviewwindow, 0&, "atlaxwinex", vbNullString)
atlffa = FindWindowEx(atlaxwinex, 0&, "atl:000007ff7236a190", vbNullString)
button = FindWindowEx(atlffa, 0&, "button", vbNullString)
button = FindWindowEx(atlffa, button, "button", vbNullString)


And as I should have mentioned before, there is this (http://www.patorjk.com/progapispy.htm) nifty application (which I just used to create that code!) which will generate the code for you.
Title: Re: Enabling a button
Post by: topaz on February 26, 2006, 07:52 PM
Quote from: K on February 26, 2006, 07:31 PM
No. You just need to know the window text and/or class name of the main window, and the text and/or class name of the child window. If there is more than one child with the same class at the same level in the window heirarchy, then you just need to call FindWindowEx() with the appropriate arguments to enumerate the siblings and find the correct one.

For example, the following code will find the "Defragment" button in the Windows XP Disk Defragmenter:


Dim mmcmainframe As Long, mdiclient As Long, mmcchildfrm As Long
Dim mmcviewwindow As Long, mmcocxviewwindow As Long, atlaxwinex As Long
Dim atlffa As Long, button As Long
mmcmainframe = FindWindow("mmcmainframe", vbNullString)
mdiclient = FindWindowEx(mmcmainframe, 0&, "mdiclient", vbNullString)
mmcchildfrm = FindWindowEx(mdiclient, 0&, "mmcchildfrm", vbNullString)
mmcviewwindow = FindWindowEx(mmcchildfrm, 0&, "mmcviewwindow", vbNullString)
mmcocxviewwindow = FindWindowEx(mmcviewwindow, 0&, "mmcocxviewwindow", vbNullString)
atlaxwinex = FindWindowEx(mmcocxviewwindow, 0&, "atlaxwinex", vbNullString)
atlffa = FindWindowEx(atlaxwinex, 0&, "atl:000007ff7236a190", vbNullString)
button = FindWindowEx(atlffa, 0&, "button", vbNullString)
button = FindWindowEx(atlffa, button, "button", vbNullString)


And as I should have mentioned before, there is this (http://www.patorjk.com/progapispy.htm) nifty application (which I just used to create that code!) which will generate the code for you.

WOW! Thanks!
Title: Re: [Solved] Enabling a button
Post by: LivedKrad on February 26, 2006, 08:21 PM
Solved.