如何得到系统中当前光标所在位置的窗口句柄?就是用户正在输入东西的那个窗口

karl 2003-09-29 11:49:38
如何得到系统中当前光标所在位置的窗口句柄?
就是用户正在输入东西的那个窗口
...全文
616 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
karl 2003-10-10
  • 打赏
  • 举报
回复
AttachThreadInput(GetcurrentThreadID,GetWindowThreadProcessID(GetForegroundWindow,nil),True)
GetFocus;
完成!!!
karl 2003-10-09
  • 打赏
  • 举报
回复
To doudouding()
GetForegroundWindow函数只能返回当前应用程序的的句柄,我想要的是能不能得到当前控件的句柄,就是具体一个输入框,按钮。。。等等控件
naughtyboy 2003-10-09
  • 打赏
  • 举报
回复
GetCaretPos
doudouding 2003-10-08
  • 打赏
  • 举报
回复
to楼上
如果楼主的意思就是得到当前的活动窗口用哪个函数,可用GetForegroundWindow获得前台窗口句柄
otherface 2003-10-05
  • 打赏
  • 举报
回复
关注光标版.
楼主的意思就是得到当前的活动窗口用哪个函数.
lwluser 2003-10-04
  • 打赏
  • 举报
回复
晕,,看错了,,你说的是光标,,对不起..我以为是鼠标,,
不过也没事,,把鼠标移到光标所在的那个窗口就行了,,, :)
:):(
lwluser 2003-10-04
  • 打赏
  • 举报
回复
function GetSursorPos(
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;
sixgj 2003-10-02
  • 打赏
  • 举报
回复
呵呵。
naughtyboy 2003-09-30
  • 打赏
  • 举报
回复
得到光标的位置用GetCaretPos
然后调用WindowFromPoint
没必要使用hook,用个timer或者后台线程就足够了
ghyghostII 2003-09-29
  • 打赏
  • 举报
回复
HWND WindowFromPoint(

POINT Point // structure with point
);

返回当前光标位置的控件句柄



BOOL GetCursorPos(

LPPOINT lpPoint // address of structure for cursor position
);

这个函数返回前前光标位置,,
把GetCursorPos返回值传入windowfrompoint参数即可


karl 2003-09-29
  • 打赏
  • 举报
回复
别急!!
如何得到系统中当前光标所在位置?
HWND WindowFromPoint(
POINT Point // point
);
  • 打赏
  • 举报
回复
HWND WindowFromPoint(

POINT Point // structure with point
);

返回当前光标位置的控件句柄



BOOL GetCursorPos(

LPPOINT lpPoint // address of structure for cursor position
);

这个函数返回前前光标位置,,
把GetCursorPos返回值传入windowfrompoint参数即可
  • 打赏
  • 举报
回复
windowfrompoint

这个函数

分太好得
  • 打赏
  • 举报
回复
windowfrompoint

这个函数

分太好得了,
wxjh 2003-09-29
  • 打赏
  • 举报
回复
要么就是我理解错了,如果错了,那么就是要使用hook了,很简单的
wxjh 2003-09-29
  • 打赏
  • 举报
回复
呵呵

unit Unit1;

interface

uses
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.
wxjh 2003-09-29
  • 打赏
  • 举报
回复
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;
karl 2003-09-29
  • 打赏
  • 举报
回复
但是我激活后鼠标可能移动到别的窗口上方,但还是使用的那个激活的窗口
  • 打赏
  • 举报
回复
你激活也是得移动鼠标位置吧,,

karl 2003-09-29
  • 打赏
  • 举报
回复
倒~~~
呵呵...
加载更多回复(8)

1,183

社区成员

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

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