I need to simulate a click on the OK button of a messagebox of another program. I managed to find the handle of the OK button. Next, I called SetCapture with the handle of OK button followed by sending Message WM_LBUTTONDOWN and then WM_LBUTTONUP. But the OK Button didnt click. Though I found another way for doing this, I'm curious to know why the previousone  didnt work. THe new method i used is this.
I found handle of the Messagebox as well. I sent Message WM_COMMAND to the messagebox window with wParam the Control ID of the OK button. it was 2. and lParam the handle of the OK button. This worked.  Please someone help.
Posted on 2005-11-03 13:51:59 by fornix
Why SetCapture? Maybe that's what's messing it up. I'd say a normal SendMessage(hButton,WM_LBUTTONDOWN,0,0) followed by an SendMessage(hButton,WM_LBUTTONUP,0,0) should do. At least that's how I always did it.

Posted on 2005-11-03 15:04:19 by JimmyClif
I  think you can send a BM_CLICK message to  the OK button of a messagebox of another program.
Posted on 2005-11-04 10:18:23 by purpleendurer
I tried both of them. WM_LBUTTONDOWN followed by WM_LBUTTONUP and BM_CLICK. Both dont work. the button just doesnt get clicked! I think button of messagebox behaves differently.
Posted on 2005-11-05 10:15:33 by fornix
Maybe you ould try the vk_return instead?

Just a wild guess.
Posted on 2005-11-05 11:20:44 by gavin
hey fornix,

i did exactly the same as you want to do - just to click the "ok"-button of a message-box. here's how i did it:

//a proc for enumerating the child-windows of the message-box
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) {
  char buffer[1024];
  GetWindowText(hwnd,buffer,sizeof(buffer)); //get child-window-label
  if(!strcmp(buffer,"OK")) { //is it the "OK"-button?
    hwndButton=hwnd; //copy the handle
    return false; //end enumeration
  return true; //continue enumeration

  hWnd=FindWindow(NULL,"title of your messagebox"); //find the window-handle of the messagebox
  EnumChildWindows(hWnd,&EnumChildProc,0); //find the handle
  if(hwndButton) { //have we got the window?
    dwButtonId=GetWindowLong(hwndButton,GWL_ID); //also get the resource-ID of the button
    PostMessage(hWnd,WM_COMMAND,(BN_CLICKED<<16)|(dwButtonId&0xFFFF),(LPARAM)hwndButton); //simulate a click on the button

maybe it's just the resource-ID you reqire to send too (as usually the programs check the resource-ID of the message to determine which button has been clicked)

greets, hartyl
Posted on 2005-11-06 11:00:57 by hartyl
Thanx hartyl. THe method you told was the one i had to employ at last.
Posted on 2005-11-07 10:39:28 by fornix