HWND WindowFromPoint( POINT Point // structure with point );返回当前光标位置的控件句柄BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position );这个函数返回前前光标位置,, 把GetCursorPos返回值传入windowfrompoint参数即可
别急!! 如何得到系统中当前光标所在位置? HWND WindowFromPoint( POINT Point // point );
HWND WindowFromPoint( POINT Point // structure with point );返回当前光标位置的控件句柄BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position );这个函数返回前前光标位置,, 把GetCursorPos返回值传入windowfrompoint参数即可
);返回当前光标位置的控件句柄BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position
);这个函数返回前前光标位置,,
把GetCursorPos返回值传入windowfrompoint参数即可
如何得到系统中当前光标所在位置?
HWND WindowFromPoint(
POINT Point // point
);
);返回当前光标位置的控件句柄BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position
);这个函数返回前前光标位置,,
把GetCursorPos返回值传入windowfrompoint参数即可
和GetClassName 这两个Win32函数就可以完成任务,不过,如果要获取当前鼠标位置的字符,可能要复杂得多。
下面是很简单的范例,大家应该都可以轻易弄清楚的。
typeTForm1 = class(TForm)NameLB: TLabel;ClassLB: TLabel;Timer1: TTimer;procedure Timer1Timer(Sender: TObject);procedure FormCreate(Sender: TObject);privateprocedure GetMousePosHwndAndClassName(Sender : TPoint);public
end;
varForm1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Timer1Timer(Sender: TObject);varrPos: TPoint;beginif boolean(GetCursorPos(rPos)) thenGetMousePosHwndAndClassName(rPos);end;
procedure TForm1.GetMousePosHwndAndClassName(Sender: TPoint);varhWnd: THandle;aName: array [0..255] of char;beginhWnd := WindowFromPoint(Sender);NameLB.Caption := ’Handle : ’ + IntToStr(hWnd);
if boolean(GetClassName(hWndaName256)) thenClassLB.Caption := ’ClassName : ’ + string(aName)else
ClassLB.Caption := ’ClassName : not found’;end;
procedure TForm1.FormCreate(Sender: TObject);beginForm1.FormStyle := fsStayOnTop;Timer1.Interval := 50;end;
必须得在TIMER中进行时时鼠标位置跟踪
这样就能时时得到鼠标所在控件的句柄或类名了楼主不要做坏事喽~~
);只是返回当前鼠标位置,和用户输入数据的那个光标没有关系吧!我要得到当前激活的那个输入窗口的句柄
非坏事,是想研究一下
兄台,两个都是你啊?
继承下去的:)
GetCursorPos是返回当前鼠标光标所在的屏幕绝对位置。你的光标所在的控件也是有一个位置的吧
那么使用GetCursorPos函数就能取得那个控件的位置坐标,
var
cursor_postion:tpoint;
handle:hwnd;
class_name:pchar;
intResult:integer;
strClassName:string;
begin
Timer1.Enabled :=false;
getcursorpos(cursor_postion);
handle:=windowfrompoint(cursor_postion);
if handle<>0 then
begin
GetMem(class_name,256);
intResult:=getclassname(handle,class_name,256);
//showmessage(inttostr(intResult));//判断是否得到类型
form1.Memo1.Lines.Add(Trim(string(class_name)));
FreeMem(Class_Name);
end;
timer1.Enabled :=true;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
CheckBox1: TCheckBox;
RadioButton1: TRadioButton;
Timer1: TTimer;
Memo1: TMemo;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Timer1Timer(Sender: TObject);
var
cursor_postion:tpoint;
handle:hwnd;
class_name:pchar;
intResult:integer;
strClassName:string;
begin
Timer1.Enabled :=false;
getcursorpos(cursor_postion);
handle:=windowfrompoint(cursor_postion);
if handle<>0 then
begin
GetMem(class_name,256);
intResult:=getclassname(handle,class_name,256);
//showmessage(inttostr(intResult));//判断是否得到类型
form1.Memo1.Lines.Add(Trim(string(class_name)));
FreeMem(Class_Name);
end;
timer1.Enabled :=true;
end;end.
然后调用WindowFromPoint
没必要使用hook,用个timer或者后台线程就足够了
lpPoint:LPPOINT //指向鼠标位置的指针
);BOOL;stdcall;
如果上面的函数执行成功,返回一个非零的值,并将鼠标位置存在lpPoint中,
用上面的函数取得鼠标位置后,用WindowFromPoint函数通过鼠标坐标取得
该点的窗口句柄:
function WindowFromPoint(
Point:TPoint //TPoint结构,存着鼠标坐标点
)HWND;stdcall;
如果WindowFromPoint执行成功,返回窗口句柄,否则返回NULL,注意此函数不能
取得隐藏的或处于不活动状态的窗体,即使这个坐标点就处于其窗口内,要进行无限
搜索,用ChlidWindowFromPoint函数:
function ChlidWinfowFromPoint(
hWndParent:HWND; //父窗口句柄
Point:TPoint; //坐标点
)HWND;stdcall
举例:
1.创建一个新的应用程序;
2.往Form1中加入一个Memo1和一个Timer1;procedure TForm1.Timer1Timer(Sender:TObject);
var
aClassName,aWndText:array[0..254] of char;
hWnd,hWndOwer:hWnd; //等于hWndOwer:THandle
pos:TPoint;
begin
//取得鼠标位置
GetCursorPos(pos);
//根据鼠标位置得到它所在的窗体句柄
hWnd:=WindowFromPoint(pos);
//由窗口句柄得到窗口类名和标题
GetClassName(hWnd,@aClassName,255);
GetWindowText(hWnd,@aWndText,255);
//显示
with Memo1.Lines do
begin
Clear;
Add('窗体句柄:'+IntToStr(hWnd));
Add('窗体类名:'+aClassName);
Add('窗体标题:'+aWndText);
end;
//由窗口句柄得到父窗体
hWndOwer:=GetWindow(hWnd,GW_OWNER);
GetClassName(hWndOwer,@aClassName,255);
GetWindowText(hWndOwer,@aWndText,255);
with Memo1.Lines do
begin
Add('父窗口句柄:'+IntToStr(hWndOwer));
Add('父窗口类名:'+aClassName);
Add('父窗口标题:'+aWndText);
end;
end;3.往Form1中加入一个Button1;
procedure TForm1.Button1Click(Sender:TObject);
begin
Timer1.Enable:=not Timer1.Enable;
if Timer1.Enable then
Button1.Caption:='停止'
else
Button1.Caption:='开始';
end;
如果楼主的意思就是得到当前的活动窗口用哪个函数,可用GetForegroundWindow获得前台窗口句柄
GetForegroundWindow函数只能返回当前应用程序的的句柄,我想要的是能不能得到当前控件的句柄,就是具体一个输入框,按钮等等控件
GetFocus;
完成!!!