关于执行别人程序里的弹出菜单项(100分)

  • 主题发起人 主题发起人 plzw
  • 开始时间 开始时间
procedure TForm1.Button4Click(Sender: TObject);<br>var<br>&nbsp; Wnd: HWND;<br>&nbsp; WinPos: TRect;<br>&nbsp; xy: TPoint;<br>begin<br><br>&nbsp; Wnd := FindWindow(nil,'CAXA电子图板文件浏览器');<br>&nbsp; if Wnd &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; SetForegroundWindow(Wnd);<br>&nbsp; &nbsp; GetWindowRect(Wnd,WinPos);<br>&nbsp; &nbsp; xy.x := WinPos.Left + 20;<br>&nbsp; &nbsp; xy.y := WinPos.Top + 45; &nbsp; //(20,45)是工具条上'打开'按钮的相对位置。<br>&nbsp; &nbsp; SetCursorPos(xy.x,xy.y);<br>&nbsp; &nbsp; Mouse_Event(MOUSEEVENTF_LEFTDown,xy.x,xy.y,0,0);<br>&nbsp; &nbsp; Mouse_Event(MOUSEEVENTF_LEFTUP, &nbsp;xy.x,xy.y,0,0);<br><br>&nbsp; &nbsp; sleep(2000);<br>&nbsp; &nbsp; wnd := 0;<br>&nbsp; &nbsp; while &nbsp;wnd = 0 &nbsp;do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Wnd := FindWindow(nil,'打开');<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; cnt := 1;<br>&nbsp; &nbsp; EnumChildWindows(Wnd,@EnumerateChildWindows,0);<br><br>&nbsp; &nbsp; SetForegroundWindow(Wnd);<br>&nbsp; &nbsp; GetWindowRect(Wnd,WinPos);<br>&nbsp; &nbsp; xy.x := WinPos.Left + 380; &nbsp;//(380,200)是'确定'按钮在窗口内的相对位置。<br>&nbsp; &nbsp; xy.y := WinPos.Top + 200;<br>&nbsp; &nbsp; SetCursorPos(xy.x,xy.y);<br>&nbsp; &nbsp; Mouse_Event(MOUSEEVENTF_LEFTDown,xy.x,xy.y,0,0);<br>&nbsp; &nbsp; Mouse_Event(MOUSEEVENTF_LEFTUP, &nbsp;xy.x,xy.y,0,0);<br><br><br>&nbsp; end<br>&nbsp; else ShowMessage('window not found');<br><br>end;<br>//---------------------------------------------------------------------------<br>function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;<br>var<br>&nbsp; &nbsp;i : integer;<br>&nbsp; &nbsp;tempStr : string;<br>&nbsp; &nbsp;ClassName: Array[0..255] of char;<br>begin<br>&nbsp; &nbsp;i := 0;<br>&nbsp; &nbsp;GetClassName(hWnd,ClassName,255);<br><br>&nbsp; &nbsp;if Pos('EDIT',UpperCase(StrPas(ClassName))) &gt; 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Inc(cnt);<br>&nbsp; &nbsp; &nbsp; &nbsp;case cnt of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(Hwnd,WM_SETTEXT,0,LongInt(pchar(exbFileName))); //写上文件名<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;Result:=TRUE;<br>end;
 
你这个是模拟点击窗体中的按钮,我要的是点击弹出菜单。<br>不过仍然谢谢你的帮助。
 
变通一下总会吧
 
但是弹出菜单的位置不固定哦
 
你能不能控制菜单的弹出,如果能,不就行了?<br>1、先让菜单弹出来。<br>&nbsp; &nbsp;xy.x := 指定的x; &nbsp;//在(x,y)单击右键可以弹出菜单,(x,y)指在窗口内的相对位置。<br>&nbsp; &nbsp;xy.y := 指定的y ;<br>&nbsp; &nbsp;SetCursorPos(xy.x,xy.y);<br>&nbsp; //!!改变左键为右键 &nbsp;MOUSEEVENTF_LEFTDown ,可能是MOUSEEVENTF_RIGHTDown 和 MOUSEEVENTF_RIGHTUP<br>&nbsp; &nbsp;Mouse_Event(MOUSEEVENTF_LEFTDown,xy.x,xy.y,0,0);<br>&nbsp; &nbsp;Mouse_Event(MOUSEEVENTF_LEFTUP, &nbsp;xy.x,xy.y,0,0);<br>2、发送鼠标左键消息 (或键盘消息)<br>&nbsp; &nbsp;xy.x :=指定的X; &nbsp;//(X,Y)是你要的菜单项在窗口内的相对位置。<br>&nbsp; &nbsp;xy.y :=指定的Y;<br>&nbsp; &nbsp;SetCursorPos(xy.x,xy.y);<br>&nbsp; &nbsp;Mouse_Event(MOUSEEVENTF_LEFTDown,xy.x,xy.y,0,0);<br>&nbsp; &nbsp;Mouse_Event(MOUSEEVENTF_LEFTUP, &nbsp;xy.x,xy.y,0,0);<br><br>提示:可以拷贝屏幕,在画图等工具里察看点的坐标。
 
&gt;提示:可以拷贝屏幕,在画图等工具里察看点的坐标。<br>看来只有这样了,不过每次都得重设菜单的位置哦,有点麻烦。
 
有没有更好的方法?没有的话就准备结帖了。
 
==&gt;不过每次都得重设菜单的位置哦,有点麻烦。<br>不会吧?<br>第二个点相对第一个点应该不变(也不是绝对的),第一个点相对于窗口不变(可以做到)。实在不行,可以最大化窗口啊,坐标转换也行。
 
差不多了,谢谢,结帖。
 
后退
顶部