方法1:
//特定窗口的特定位置按下鼠标左键
procedure TForm1.Button2Click(Sender: TObject);
var
Wnd: HWND;
WinPos: TRect;
xy: TPoint;
begin
Wnd := FindWindow('TAppBuilder',nil); //找到Delphi IDE 的 handle
if Wnd <> 0 then
begin
SetForegroundWindow(Wnd);
GetWindowRect(Wnd,WinPos);
xy.x := WinPos.Left + 10; //(10,10)是窗口内的相对位置。
xy.y := WinPos.Top + 10;
SetCursorPos(xy.x,xy.y);
Mouse_Event(MOUSEEVENTF_LEFTDown,xy.x,xy.y,0,0);
Mouse_Event(MOUSEEVENTF_LEFTUP, xy.x,xy.y,0,0);
end
else ShowMessage('window not found');
end;
方法2:
procedure TMessage.Button5Click(Sender: TObject);
var
hwnd: Integer;
begin
hwnd := FindWindow(nil,pchar(edit1.text));
if hwnd<>0 then
begin
//hwnd := FindWindowEx(hwnd,0,pchar(edit1.text),nil);
hwnd := FindWindowEx(hwnd,0,'TButton',pchar(edit3.text));
if hwnd=0 then showmessage('没找到按钮!');
SendMessage(hwnd,WM_LBUTTONDOWN,MK_LBUTTON,0);
SendMessage(hwnd,WM_LBUTTONUP,MK_LBUTTON,0);
end
else showmessage('没找到窗口!');
end;