下面一段程序模拟鼠标为什么不行?

stgsd 2006-01-23 12:59:39
明明已经向目标窗口发了鼠标消息,为什么就没有效果呢?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
Timer1: TTimer;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ComboBox1Click(Sender: TObject);
private
{ Private declarations }
procedure myhotkey(var msg: Twmhotkey);message wm_hotkey;
public
{ Public declarations }
end;

var
Form1: TForm1;
hh:hwnd;
hhh:integer;
p:tpoint;
implementation

{$R *.dfm}
//列举所有窗口
procedure TForm1.Button1Click(Sender: TObject);
var
h:hwnd;
r:array[0..254]of char;
begin
if combobox1.Items.Count>0 then combobox1.Items.Clear;
h:=getwindow(handle,gw_hwndfirst);
while h<>0 do
begin
if getwindowtext(h,@r,255)>0 then
form1.ComboBox1.Items.Add(strpas(@r)) ;
h:=getwindow(h,gw_hwndnext);
end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
hhh:=GlobalAddAtom('hotkey');
RegisterHotKey(handle, hhh, 0, vk_home);
end;
//按‘home’选鼠标将要点击的地方
procedure Tform1.myhotkey(var msg: Twmhotkey);
begin
getcursorpos(p);
//
end;
//在下面模拟鼠标,timer为5秒
procedure TForm1.Timer1Timer(Sender: TObject);
begin
sendmessage(hh,$203,p.X,p.Y);
//sendmessage(hh,wm_lbuttondown,p.x,p.y);
sleep(200);
//sendmessage(hh,wm_lbuttonup,p.x,p.y);
end;
//控制模拟开关
procedure TForm1.Button2Click(Sender: TObject);
begin
if timer1.Enabled=false then
timer1.Enabled:=true else
timer1.Enabled:=false;
end;
//选定一个窗口
procedure TForm1.ComboBox1Click(Sender: TObject);
begin
hh:=findwindow(nil,pchar(combobox1.Items[combobox1.itemindex]));
end;

end.
...全文
234 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
reborter 2006-02-04
  • 打赏
  • 举报
回复
学习
DemonLoveLizzy 2006-02-04
  • 打赏
  • 举报
回复
WPARAM为空,LPARAM是一个POINTER,结构跟POINTE一样,高位为X,低位为Y.也许LPARAM是一个INTEGER,自己设高低位.反正VB是这样.用INTEGER发送参数试试.
rainstormmaster 2006-01-27
  • 打赏
  • 举报
回复
也就是说调用sendmessage之前,先要让窗口获得焦点,这有一段vb代码你参考一下:
Dim Tid1 As Long, Tid2 As Long, pid As Long
Tid1 = GetWindowThreadProcessId(mhwnd, pid)
Tid2 = App.ThreadID
Call AttachThreadInput(Tid1, Tid2, True)
SetFocusApi mhwnd
'在按钮相对坐标(1,1)处点击,最后一个参数用0应该也没关系
SendMessage mhwnd, WM_LBUTTONDOWN, MK_LBUTTON, ByVal &H10001
SendMessage mhwnd, WM_LBUTTONUP, MK_LBUTTON, ByVal &H10001


最后说一句:
sendmessage(hh,wm_lbuttondown,p.x,p.y);
这一句里,你的wparam和lparam参数都不对

rainstormmaster 2006-01-27
  • 打赏
  • 举报
回复
The WM_LBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is in the client area of a window. If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.
stgsd 2006-01-24
  • 打赏
  • 举报
回复
mouse_event 就是单纯的操作鼠标,移动、单击、双击等等。和窗口句柄没关系。
所以和我的目的无关,我不是要在屏幕上按
liuyan55 2006-01-24
  • 打赏
  • 举报
回复
帮顶
耙子 2006-01-24
  • 打赏
  • 举报
回复
mouse_event 就是单纯的操作鼠标,移动、单击、双击等等。和窗口句柄没关系。
stgsd 2006-01-24
  • 打赏
  • 举报
回复
mouse_event能用于没有焦点的窗口吗?
它的参数好象没有窗口句柄啊!
kongguangming 2006-01-23
  • 打赏
  • 举报
回复
用mouse_event

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧