这有点像金山词霸的屏幕取词。要获取当前鼠标位置的类名和句柄,只须通过 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);
public
end;
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)
else
ClassLB.Caption := ’ClassName : not found’;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.FormStyle := fsStayOnTop;
Timer1.Interval := 50;
end;