We help IT Professionals succeed at work.

Button Controls

Zainal062797 asked
Last Modified: 2013-12-03
I am trying to emulate mouse clicks sent to a an off-shilf software.
The software displays a dialog box which has a number of control buttons. I am trying to emulate this operation by sending a PostMessage() to these button controls. However, the problem is that I cannot find out the handles of these buttons. The button I am interested in is the "OK" button. I used the following:
FindWindowEx (hParent, NULL, NULL, "OK")
But it cannot find it!
Please advise.

Thank you.
Watch Question

The most obvious possibility is that the button isn't a direct child of the parent you are specifying, it's a child of a child.

Another possibility is that the O in "OK" is underlined, so you'd have to search for "&OK".

Hope this helps!
Unlock this solution and get a sample of our free trial.
(No credit card required)

What you should do is to use Spy++ or a similar utility to obtain the class name of the button and its caption.  (It is possible that the caption is not "OK")  Then you can specify this information to FindWindowEx();


I Used Spy++ ans found that the button is a direct child of the window.
The Okay is not underlined.
I used the class name as well for the 3rd parameter, which is "Button".. And it is still not working.


Did you check the captiion in Spy++?  The 4th parameter must match the caption.

What part is not working?  Is FindWindowEx() not returning the handle or is the PostMessage() not working?


Yes... The Caption is "OK" - no underscore.
FIndWindowEx is not working. It doesn't return a handle. It returns NULL.


Is the window handle you are passing in the first parameter correct?  It must be a handle to the window that is the dirrect parent window of the button window.  It should not be one of your application's windows and it should not be a grandparent or more distant ancestor of the button.  Use Spy++ to confirm that you are specifying the correct window handle.


The parent handle Is correct 100%. It is a direct parent. I could find its siblings using the same function but changing the text. However, the siblings I can get a handle to are all groub boxes. The okay button (and cancel) are stand alone child windows....  I am wondering if this has to do with how Windows  handles and processes windows controls?!


Does Spy++ show the okay button as being a direct child of the parent, or as a child of a group box?  A control is just a window, same as any other window.


I solved the promlem. I had to use the "Button" class naem as well as the "OK" text. With others windows, I could get the handle by specifying the text only!

The question now is setting the dialog box item to the desired option.
I can set the radio button to the desired option by passing it  a:
PostMessage (hWnd, WM_SYSKEYDown, Letter, NULL) ;  
But does this actually update the variable associated with this radio button or it just merely checks the radio button, but internally, the variable still holds the old option? Do I have to send additional messges to set the internal variable?

Please Advice.

That was the first thing I told you!  I said the 3rd parameter, the window class, must be specified.

If that makes the radio button appear to be marked that is enough.  You probably will need to send a SYSKEYDOWN and a SYSKEYUP.


Thank you very much.
Just to let you know that I did what you told me exactly. As you told me in the beggening , I used the "button" class name nad the window text "OK". The reason it didn't work and later it did is beacuse I realized the timing problem that is caused by Windows messages. See the problem tyurned out to be that the window I was trying to get ist handle was not yet created when the FindWinex() was invoked. This is because the program I am trying to control creats a number of child windows.

I just thought I would share this experience with you.

Thanks again.
Aiman. :)

Thanks, the feedback always helps.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.