在一个程序中控制另外一个程序的button ,要向其发送 鼠标单击事件。第一次发送的时候 总是失败,而第二次发送的时候就能成功。为什么呢?代码如下: CWnd *pWnd = FindWindow(_T("#32770"),"Test");
if( pWnd )
 {
     CButton *pChild = (CButton *)pWnd->GetDlgItem(dwID);
    pChild->SendMessage(WM_LBUTTONDOWN,0,0);
    pChild->SendMessage(WM_LBUTTONUP,0,0);
}发送消息没有任何效果。
但是如果改为连续发两次,就能成功为什么? CWnd *pWnd = FindWindow(_T("#32770"),"Test");
if( pWnd )
 {
     CButton *pChild = (CButton *)pWnd->GetDlgItem(dwID);
    pChild->SendMessage(WM_LBUTTONDOWN,0,0);
    pChild->SendMessage(WM_LBUTTONUP,0,0);
   pChild->SendMessage(WM_LBUTTONDOWN,0,0);
    pChild->SendMessage(WM_LBUTTONUP,0,0);}

解决方案 »

  1.   

    我尝试过设置焦点
     pWnd->SetFocus()  pChild->SetFocus() 都试过。没有用
      

  2.   

    用SETACTIVEWINDOW,SETFOCUS是获得输入焦点
      

  3.   

    CWnd *pWnd = FindWindow(_T("#32770"),"Test");
     
    if( pWnd )
     {
         CButton *pChild = (CButton *)pWnd->GetDlgItem(dwID);
        pChild->SendMessage(WM_LBUTTONDOWN,0,0);
        pChild->SendMessage(WM_LBUTTONUP,0,0);
    }  i该如何修改
    我SetActiveWindow SetFocus都试过了不行。。
      

  4.   

    CWnd *pWnd = FindWindow(_T("#32770"),"Test");
     
    if( pWnd )
     {
         CButton *pChild = (CButton *)pWnd->GetDlgItem(dwID);
        pChild->PostMessage(WM_LBUTTONDOWN,0,0);
        pChild->PostMessage(WM_LBUTTONUP,0,0);
    }
    现在看看了!
      

  5.   

    如果你是主线程中启动的子线程的话,那么在CreatProcess后,最好加上一句Sleep(500).
      

  6.   

    对按扭发送WM_LBUTTONDOWN并不能触发按扭的点击事件,WM_LBUTTONDOWN是按扭被按下后发送的,所以不能满足你的要求,你需要对按扭的父窗口发送WM_NOTIFY消息
      

  7.   

    BM_CLICK Message--------------------------------------------------------------------------------An application sends a BM_CLICK message to simulate the user clicking a button. This message causes the button to receive the WM_LBUTTONDOWN and WM_LBUTTONUP messages, and the button's parent window to receive a BN_CLICKED notification message.Syntax
    To send this message, call the SendMessage function as follows. 
    lResult = SendMessage(      // returns LRESULT in lResult     (HWND) hWndControl,      // handle to destination control     (UINT) BM_CLICK,      // message ID     (WPARAM) wParam,      // = 0; not used, must be zero    (LPARAM) lParam      // = 0; not used, must be zero );