在自己的窗口(Form1)上放一个Timer控件,两个edit控件,在Timer控件的OnTimer里写:
procedure TForm1.Timer1Timer(Sender: TObject);
var
ClassName: PChar; //控件类名
ptCursor: TPoint; //鼠标位置
hWndOver: HWND; //窗口句柄
Text: PChar; //文本内容
begin
GetCursorPos(ptCursor); //获取鼠标位置
//获取鼠标下的窗口句柄,可以是别的窗口
hWndOver := WindowFromPoint(ptCursor);
GetMem(ClassName, 100); //分配内存
GetMem(Text, 255); //分配内存
try
//获取鼠标下控件的类名
GetClassName(hWndOver, ClassName, 100);
//获取鼠标下的文本
SendMessage(hWndOver, WM_GETTEXT, 255, LongInt(Text));
//在自己窗口的edit1, edit2中显示获取的类名和文本
edit1.Text :=String(ClassName);
edit2.Text :=String(Text);
finally
FreeMem(ClassName); //释放内存
FreeMem(Text);
end;
end;
Timer控件的作用就是使每隔一段时间(在Timer控件的interval里设定时间的间隔)就获取鼠标的位置
procedure TForm1.Timer1Timer(Sender: TObject);
var
ClassName: PChar; //控件类名
ptCursor: TPoint; //鼠标位置
hWndOver: HWND; //窗口句柄
Text: PChar; //文本内容
begin
GetCursorPos(ptCursor); //获取鼠标位置
//获取鼠标下的窗口句柄,可以是别的窗口
hWndOver := WindowFromPoint(ptCursor);
GetMem(ClassName, 100); //分配内存
GetMem(Text, 255); //分配内存
try
//获取鼠标下控件的类名
GetClassName(hWndOver, ClassName, 100);
//获取鼠标下的文本
SendMessage(hWndOver, WM_GETTEXT, 255, LongInt(Text));
//在自己窗口的edit1, edit2中显示获取的类名和文本
edit1.Text :=String(ClassName);
edit2.Text :=String(Text);
finally
FreeMem(ClassName); //释放内存
FreeMem(Text);
end;
end;
Timer控件的作用就是使每隔一段时间(在Timer控件的interval里设定时间的间隔)就获取鼠标的位置
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货