Hi all,

I’m doing some automation for a nightly deployment of some software. My program should:
Open a program
Send shortcut key press to the program
Push a button
And close the program again.

So far I can open the program, bring it to front, send shortcut keys, but I can't figure out how to push a button in that window?
Are buttons windows? If so, I guess I have to enumerate child windows to push the button?
How do I send the push? sendmessage? What message type?

Any help would be appreciated,

- Henning

Recommended Answers

All 11 Replies

Sure, buttons are windows. In Windows, everything's a window except those things that aren't windows!

You could use SendMessage to the hWnd of the button with the BN_CLICKED notification - I believe - in the LOWRD of wParam. Please check out that part. There are macros to position various 16 bit quantities in either the low or hi word of wParam.

Here's some additional info...

The MAKELONG macro creates an unsigned 32-bit value by concatenating two given 
16-bit values. 

DWORD MAKELONG
(
  WORD wLow,  // low-order word of long value
  WORD wHigh  // high-order word of long value
);
 
Parameters

wLow     Specifies the low-order word of the new long value. 
wHigh    Specifies the high-order word of the new long value.

BN_CLICKED  The BN_CLICKED notification message is sent when the user clicks a 
            button. The parent window of the button receives this notification 
            message through the WM_COMMAND message. 

BN_CLICKED
 
idButton = (int) LOWORD(wParam);    // identifier of button 
hwndButton = (HWND) lParam;         // handle to button

So you would need...

SendMessage(hParent, WM_COMMAND, MAKELONG(idButton, BN_CLICKED), hButton);

where hParent is the parent or container control of button in question and idButton
is the identifier of button from CreateWindow().  If you have either the hWnd of
button or control id you can get the other from either GetCtrlID() or GetDlgItem().
The parent of a control can be gotten from GetParent().

Also check out BN_CLICK message.

Here's some additional info...

Don't copy MSDN doc.
And it's not the right method (events)

commented: You just.. don't... stop. -2
commented: William...is...right :) -6

Don't copy MSDN doc.
And it's not the right method (events)

*Yawn*

Once again, instead of saying what not to do or what's wrong in his program, tell him how to do it right, then we all might actually think you know something for once. Otherwise, just go away, your "help" is needed here.

Thanks a lot. :)

I'm still having a few minor problems tho'.

I need to send a ctrl+g

SendMessage ( hndDesigner, WM_KEYDOWN, 17, 1) ;            
	SendMessage ( hndDesigner, WM_KEYDOWN, 71, 1) ;            
	SendMessage ( hndDesigner, WM_KEYUP, 71,  0xC0000001 ) ;    
	SendMessage ( hndDesigner, WM_KEYUP, 17, 0xC0000001 ) ;

17 = control
71 = 'g'

Doesn't work?
I uses a spy so I'm sure that I have the right handle, but nothing happens :/

Any ideas?

Thanks,
- Henning

*Yawn*

Once again, instead of saying what not to do or what's wrong in his program, tell him how to do it right, then we all might actually think you know something for once. Otherwise, just go away, your "help" is needed here.

or *Isn't, should I say :P

Thanks a lot. :)

I'm still having a few minor problems tho'.

I need to send a ctrl+g

SendMessage ( hndDesigner, WM_KEYDOWN, 17, 1) ;            
	SendMessage ( hndDesigner, WM_KEYDOWN, 71, 1) ;            
	SendMessage ( hndDesigner, WM_KEYUP, 71,  0xC0000001 ) ;    
	SendMessage ( hndDesigner, WM_KEYUP, 17, 0xC0000001 ) ;

17 = control
71 = 'g'

Doesn't work?
I uses a spy so I'm sure that I have the right handle, but nothing happens :/

Any ideas?

Thanks,
- Henning

Instead of sending a message to press a key, try actually simulating a keypress or mouse click to achieve the same result, research mouse_event and keybd_event.

For example, the code to send CTRL-G is this:

keybd_event(VK_CONTROL, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event(71, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event(71, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

Hope this helps.

Hi William,

Using the keyboard event worked first time around ;)
Thanks a lot..

Now I just have to figure out how to get a button state (active/inactive).
As I read at msdn, the button sends a message to the parrent window when i changes state, but I cant see how I can get the state :/
GetWindowInfo doesn't give me information about the state: dwWindowStatus allways zero.
I have googled and googled, but found nothing?

You know of any, for me unknown, apicall that would do the trick..

Thanks,
-Henning

You mean you want to know if the button is enabled or disabled?
If so that's easy, here's what i came up with after a couple of minutes of playing around with the windows api.

bool enabled = !(bool)(GetWindowLong( button, GWL_STYLE ) & WS_DISABLED);

Hope this helps.

commented: Very nice +36
commented: I agree on the fact that you're a WinAPI God :) +21

You are a winapi God..

I was searching for window state, information and the likes. I would never have thought about GetWindowLong..

Thanks, yet again
- Henning

commented: Thankyou :) glad i helped. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.