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

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.

Edited 7 Years Ago by Frederick2: n/a

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.

Edited 7 Years Ago by William Hemsworth: n/a

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.

Edited 7 Years Ago by William Hemsworth: n/a

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.

Comments
I agree on the fact that you're a WinAPI God :)
Very nice

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

Comments
Thankyou :) glad i helped.
This question has already been answered. Start a new discussion instead.