在你发送BM_CLICK之前,必须先激活对方的窗口 HWND messagehwnd = ::FindWindow(NULL,"提示信息"); if(messagehwnd) { HWND messagechildhwnd = ::FindWindowEx(messagehwnd,0,"Button","确定"); if(messagechildhwnd) { SetActiveWindow(messagehwnd); SendMessage(messagechildhwnd, BM_CLICK, 0, 0); } } 这点在MSDN里是由解释的: If the button is in a dialog box and the dialog box is not active, the BM_CLICK message might fail. To ensure success in this situation, call the SetActiveWindow function to activate the dialog box before sending the BM_CLICK message to the button.
SendMessage(messagechildhwnd, BM_CLICK, 0, 0);
另外调试一下程序,看看FindWindow、FindWindowEx的返回值是不是NULL。
HWND messagehwnd = ::FindWindow(NULL,"提示信息");
if(messagehwnd)
{
HWND messagechildhwnd = ::FindWindowEx(messagehwnd,0,"Button","确定");
if(messagechildhwnd)
{
SetActiveWindow(messagehwnd);
SendMessage(messagechildhwnd, BM_CLICK, 0, 0);
}
}
这点在MSDN里是由解释的:
If the button is in a dialog box and the dialog box is not active, the BM_CLICK message might fail. To ensure success in this situation, call the SetActiveWindow function to activate the dialog box before sending the BM_CLICK message to the button.