当前位置:编程学习 > VB >>

鼠标模拟点击的问题

我用VB写了一个简单的程序,就是先启动一个测试程序(这个测试程序就一个窗口,点击窗体就会跳出一个msgbox),然后找到测试程序的句柄,模拟点击他,可是老是不成功。。实在想不出问题在那里?希望高手帮忙看看。。指出错误在哪里?
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202

Private Sub Form_Load()
Dim ph As Long
Shell "C:\Program Files\Internet Explorer\test2.exe", 1
ph = FindWindow(vbNullString, "测试")
ShowWindow ph, 3
PostMessage ph, &H201, 0&, 0&
PostMessage ph, &H202, 0&, 0&
End Sub
--------------------编程问答-------------------- SendMessage Me.hwnd, WM_LBUTTONDOWN, WM_LBUTTONDOWN, WM_LBUTTONDOWN
--------------------编程问答--------------------   PostMessage ph, WM_LBUTTONDOWN, MK_LBUTTON, 0&
  PostMessage ph, WM_LBUTTONUP, 0&, 0&
参数不对
用这个试下 --------------------编程问答-------------------- 如果不行你可以试下  Mouse_Event
x y是你要点的坐标 
 Mouse_Event MOUSEEVENTF_LEFTDOWN,X,Y,0,0
  Mouse_Event MOUSEEVENTF_LEFTUP,X,Y,0,0 --------------------编程问答-------------------- 楼上的两个方法我都没成功。。。 --------------------编程问答-------------------- 推荐用Mouse_Event
你可查阅一下Mouse_Event的用法。 --------------------编程问答-------------------- 不想用Mouse_Event。。Mouse_Event不能点击一个窗口大小为0的窗体吧。 --------------------编程问答-------------------- 希望有点提示。

void CPc_controlDlg::OnButton1() 
{
// TODO: Add your control notification handler code here
hMainWnd=this->m_hWnd;
// SetWindowPos(&wndTopMost,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),SWP_SHOWWINDOW);

/////////
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);

hWnd =::FindWindow(NULL,"2010.txt - 记事本"); 
    if(hWnd != NULL)
{
  menuMain  =  ::GetMenu(hWnd);   /////get the menu handle    
if(menuMain != NULL)
{
//AfxMessageBox("2010.txt - 记事本 ok");

HMENU   menuMain=::GetMenu(hWnd); 
 if(menuMain != NULL)
   {
  // AfxMessageBox("menuMain OK");
   }
   else
   {
     AfxMessageBox("menuMain fail");
   }
     
 HMENU menuSub =  ::GetSubMenu(menuMain, 0);
RECT   rectMenu;//子菜单位置   
 RECT   rectMain;//主菜单位置   
 GetMenuItemRect(hWnd,menuMain,0,&rectMain);//主菜单第1个菜单   
int   x,y;   
x=(rectMain.left+rectMain.right)/2;   
y=(rectMain.top+rectMain.bottom)/2;   
SetCursorPos(x,y);   
//模拟单击   
 
 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);   
 mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); 
  if(menuSub != NULL)
{
 int   x1,y1; 
GetMenuItemRect(hWnd,menuSub,3,&rectMenu);   
x1=(rectMenu.left+rectMenu.right)/2;   
y1=(rectMenu.top+rectMenu.bottom)/2; 
SetCursorPos(x1,y1);   
// mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);   
// mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); 
SetTimer(2,1000,NULL); 
  }
  else
  {
   AfxMessageBox("menuSub fail");
  
  }





}


}
else
{
AfxMessageBox("2010.txt - 记事本 fail1");

}

}

void CPc_controlDlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default

if(nIDEvent==2)
{
KillTimer(2);
 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
  
 SetTimer(3,1000,NULL);
  
}
if(nIDEvent==3)
{
KillTimer(3);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);

}


CDialog::OnTimer(nIDEvent);
}
--------------------编程问答-------------------- ph = FindWindow(vbNullString, "测试") 

你的ph既然已得到了 那就 用 BM_CLICK 就OK啦

Const BM_CLICK = &HF5 '摆在通用区

SendMessage ph, BM_CLICK, ByVal 0&, ByVal 0& --------------------编程问答-------------------- 改用AutoHotKey软件吧
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,