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

karl 2003-09-29 11:49:38
如何得到系统中当前光标所在位置的窗口句柄?
就是用户正在输入东西的那个窗口
...全文
611 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)
我将带领大家来系统学习Windows的窗口编程,包括消息、窗口、GDI绘图、游戏开发等。本课程比较基础,非常适合初学者入门,读者可以边学习边实践。具体的章节目录和课程内容如下所示:---------------------------------------------Windows游戏编程系列之1:GUI界面编程及游戏入门实战1、Windows创建第一个窗口 WinMain入口函数 5进行Windows编程的调试手法 6窗口从哪里来? 7窗口编程的步骤 7窗口编程需要的主要结构 8窗口编程需要的主要API 92、Windows的窗口过程与消息机制 如何留住窗口? 121)Windows的消息与消息循环 142)消息处理函数与常用消息 17)Windows的窗口过程函数 19 3、GDI编程之设备上下文 1)GDI的通用编程框架 222)GDI的绘图步骤 253)GDI获取设备句柄 254、GDI编程之绘制几何图形 画点、线 28颜色COLORREF 29矩形 29画圆、饼图、弦图 305、GDI编程之自定义画笔画刷画笔简介 32画刷简介 33画笔案例 33画刷案例 346、GDI编程之绘制文字 DrawText函数 35TextOut 函数 (wingdi.h) 36CreateFont函数 37绘制文本案例 377、GDI编程之绘制位图 位图简介 381)在资源添加位图资源 392)从资源加载位图: LoadBitmap 393)创建一个与当前DC相匹配的DC(内存DC) 394)将bitmap放入匹配的DC:SelectObject 405)成像(1:1 比例 ) 406)取出位图 407)释放位图 418)释放匹配的DC 41绘制位图案例 41   8、Windows鼠标键盘消息 一、键盘消息 421、键盘消息 422、消息参数: 423、消息的使用: 424、键盘消息的案例代码 43二、鼠标消息 441、基本鼠标消息 442、双击消息 443、滚轮消息 454、不响应双击消息 45 9、Windows定时器消息 定时器消息介绍 47创建定时器 47关闭定时器 47定时器消息案例代码 4810、GDI游戏之跳舞动画 11、GDI游戏之走路动画 12、GDI贪吃蛇游戏实战  

1,183

社区成员

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

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