这有点像金山词霸的屏幕取词。要获取当前鼠标位置的类名和句柄,只须通过 WindowFromPoint和GetClassName 这两个Win32函数就可以完成任务,不过,如果要获取当前鼠标位置的字符,可能要复杂得多。下面是很简单的范例,大家应该都可以轻易弄清楚的。 type
TForm1 = class(TForm)
NameLB: TLabel;
ClassLB: TLabel;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure GetMousePosHwndAndClassName(Sender : TPoint);
publicend;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Timer1Timer(Sender: TObject);
var
rPos: TPoint;
begin
if boolean(GetCursorPos(rPos)) then
GetMousePosHwndAndClassName(rPos);
end;procedure TForm1.GetMousePosHwndAndClassName(Sender: TPoint);
var
hWnd: THandle;
aName: array [0..255] of char;
begin
hWnd := WindowFromPoint(Sender);
NameLB.Caption := ’Handle : ’ + IntToStr(hWnd);if boolean(GetClassName(hWnd, aName, 256)) then
ClassLB.Caption := ’ClassName : ’ + string(aName)
elseClassLB.Caption := ’ClassName : not found’;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.FormStyle := fsStayOnTop;
Timer1.Interval := 50;
end;
TForm1 = class(TForm)
NameLB: TLabel;
ClassLB: TLabel;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure GetMousePosHwndAndClassName(Sender : TPoint);
publicend;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Timer1Timer(Sender: TObject);
var
rPos: TPoint;
begin
if boolean(GetCursorPos(rPos)) then
GetMousePosHwndAndClassName(rPos);
end;procedure TForm1.GetMousePosHwndAndClassName(Sender: TPoint);
var
hWnd: THandle;
aName: array [0..255] of char;
begin
hWnd := WindowFromPoint(Sender);
NameLB.Caption := ’Handle : ’ + IntToStr(hWnd);if boolean(GetClassName(hWnd, aName, 256)) then
ClassLB.Caption := ’ClassName : ’ + string(aName)
elseClassLB.Caption := ’ClassName : not found’;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.FormStyle := fsStayOnTop;
Timer1.Interval := 50;
end;
解决方案 »
- C# winform中 线程之间的通信问题 请了解这方面的朋友们指点指点!
- text to speech 如何调用speak并且触发相应事件。
- Windows Mobile 上使用C# 读取 “001.XMl”文件 怎样的读取速度最快
- 暂停循环和继的问题! 欢迎来看看!
- vs2008打包一个外部exe文件,并要让其在安装时自动运行,能做到吗?
- 菜鸟问题:C#中如何得到已经被隐藏得主窗口对象?
- vs2003没有响应,进程CPU占用率100%
- C# SWITCH 不能有比较符号吗
- winform下创建了一个全局类,但不循环读取数据
- c#编的程序,想在98或2000下运行,应该是不行的,还要加上那些东东?
- vs.net 超级难题 ! 难到又是vs.net 的bug.
- 寻找csharp.chm
Cursor.Position.Y
Cursor.Position.Y
是在程序里鼠标的位置吧
Cursor.Position.Y
还是你误进了.net板
static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")]
static extern bool GetCursorPos(out POINT lpPoint);
Cursor.Position.X
Cursor.Position.Y
可以
Cursor.Position.Y