3

I need some help. I wrote my questions at the end and will first explain what exactly my code is supposed to do:

I am making a program that will communicate with other programs. What I need my software to do is be able to click on a button on another program, and I believe the appropriate code to do this would be:

SendMessage(hWnd, Msg, wParam, lParam);

with

Msg = B_Click
wParam = 0;
lParam = 0;

however I am not sure how to get the hWnd which is the handle of a specific button on a specific window of another program that is running at the same time. I have read somewhere that I could possibly do the following:

HWND buttonHandle = FindWindowEx(hwndParent, hwndChildAfter, lpszClass, lpszWindow);

where:

HWND hwndParent = A handle to the parent window whose child windows are to be searched
HWND hwndChildAfter = if its null child windows are of the parent window are looked through
LPCTSTR lpszClass = (NOT SURE WHAT THIS IS)
LPCTSTR lpszWindow = (NOT SURE WHAT THIS IS)

I have a few issues with the FindWindowEX() function however.

Question 1: The window I am looking at has various buttons, so how would the function know which one of the 3 i am looking at?

Question 2: What are the lpszClass and lpszWindow variables and how might I get them?

Question 3: Is this even the right approach? If it's not, please point me to the right direction!

1 Answer 1

0

You don't need the handle of the button, you need the handle of its parent window. The button sends BN_CLICKED to its parent window. Look up the button's ID with spy++. Then use EnumChildWindows of the parent to look at all the child windows. For each child use GetWindowLong with GWL_ID to check its ID.

6
  • Oh, well I don't have spy++ cause I don't have visual studio... I use codeblocks instead. I downloaded a program called winspy 1.7 recently that gets me handles but it seems to only get me window handles. Any way I can get spy++ without getting visual studio, or do u know if winspy1.7 can get button handles too? Apr 19, 2014 at 18:50
  • Hi dude, I just got spy++ and have found the window which contains the button I wish to click, however there are 3 buttons in that window... and I have been looking everywhere in the "Window Properties" to find the ID of the button but haven't found it. How do I go about finding the button's ID? Apr 20, 2014 at 17:14
  • In spy++ use Find Window, grab the "finder tool" with mouse and drop it on the button you are interested in. Then click OK and you will see the "Control ID" appear in Window Properties. Apr 20, 2014 at 18:45
  • This does not work though. The finder tool only clicks on windows, not on the button inside a window, I have tried Apr 21, 2014 at 23:00
  • A button is a window. If the finder tool does not draw a rectangle around the button then it is not a real button control and does not have an HWND. It is just custom painting designed to look like a button. (Toolbars, among other things, do that.) In that case you can try and use Spy++ to find out what message is sent to the parent window when clicked. You might be able to simulate the same message. Apr 21, 2014 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.