WindowFromPoint怎样可以得到鼠标指向控件的句柄?

quantum 2002-10-22 02:05:08
WindowFromPoint怎样可以得到鼠标指向控件的句柄? 是否可以用例子说明?
...全文
344 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shujian 2002-11-13
  • 打赏
  • 举报
回复
1.如果是focus停留一个控件上,能得到它的handle吗?
2.能否得到一个应用程序上所有控件的handle吗?
ypyRock 2002-11-10
  • 打赏
  • 举报
回复
今天用 WindowFromPoint 函数,MSDN上说可以找回指定坐标下的窗口句柄,但磁盘共享属性页里的“共享名”等static、edit的句柄却找不到,为什么???
用SPY++来找有时可以,有时也找不到,好奇怪!!!
xzgyb 2002-10-25
  • 打赏
  • 举报
回复
unit FindWindowFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TMainForm = class(TForm)
Label1: TLabel;
Label2: TLabel;
lblCaption: TLabel;
lblClass: TLabel;
btnFind: TButton;
procedure btnFindMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure btnFindMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure btnFindMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
bIsDown: Boolean;
hOldWindow: THandle;
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.btnFindMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
bIsDown := True;
end;

procedure TMainForm.btnFindMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
bIsDown := False;
if hOldWindow <> 0 then
begin
InvalidateRect(hOldWindow, nil, True);
hOldWindow := 0;
end;
end;

procedure TMainForm.btnFindMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
szClassName: array[0..255] of Char;
szCaption: array[0..255] of Char;
hCurWindow, hWindowDC: THandle;
rc: TRect;
CurPt: TPoint;
begin
if bIsDown then
begin
GetCursorPos(CurPt);
hCurWindow := WindowFromPoint(CurPt);
if hCurWindow <> 0 then
begin
GetClassName(hCurWindow, szClassName, 255);
GetWindowText(hCurWindow, szCaption, 255);

if szCaption = '' then
szCaption := '无标题';
lblCaption.Caption := szCaption;
lblClass.Caption := szClassName;

if (hOldWindow <> 0) and (hCurWindow <> hOldWindow) then
InvalidateRect(hOldWindow, nil, True);
hWindowDC := GetDC(hCurWindow);
Windows.GetClientRect(hCurWindow, rc);
FrameRect(hWindowDC, rc, Canvas.Brush.Handle);

hOldWindow := hCurWindow;
ReleaseDC(hCurWindow, hWindowDC);
end;
end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
Canvas.Brush.Color := clRed;
end;

end.
智商无下限 2002-10-24
  • 打赏
  • 举报
回复
麻烦一下:用 TProcessEntry32 的引用什么单元呀?
下面的那段代码应该写在什么事件中呀?
还有:他是如何保证鼠标点击了其他窗体,而本程序还能继续运行的呀?(说得有些不明白,就是那个意思)
Drate 2002-10-24
  • 打赏
  • 举报
回复
使用勾子:
procedure TForm1.Button1Click(Sender: TObject);
var lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
if found then
begin
found := Process32Next(Hand,lppe);
end;
end;
或作用下面的方法:
procedure GetOtherText;
var TextLen: Integer;
TextStr: PChar;
PassHandle1,PassHandle:Hwnd;
PointPos: TPoint;
begin
try
GetCursorPos(PointPos);
PassHandle:=WindowFromPoint(PointPos);
if PassHandle=NULL then exit;
TextLen:=GetWindowTextLength(PassHandle)+1; //得到名字长度,并将长度加1
if TextLen <= 1 then exit;
GetMem(TextStr, 255);
try
SendMessage(PassHandle, WM_GETTEXT, 255, Longint(TextStr));
Label1.Caption := (TextStr);
finally
FreeMem(TextStr);
end;
except
end;
end;
智商无下限 2002-10-24
  • 打赏
  • 举报
回复
!
智商无下限 2002-10-24
  • 打赏
  • 举报
回复
那位高手来给终结一下!最好,带个到那里有通用性的例子:比如可以得到桌面上任意窗体的的句柄!
ZHENG017 2002-10-23
  • 打赏
  • 举报
回复
得到其他窗体用FindWindow().或EnumWindow()...
智商无下限 2002-10-23
  • 打赏
  • 举报
回复
哦!可是那就不灵活了!
40Star 2002-10-23
  • 打赏
  • 举报
回复
最好放到Timer中,就很好使
智商无下限 2002-10-23
  • 打赏
  • 举报
回复
如果当前程序不活动也可以吗?我的意思是:如果用鼠标点饥其他非本程序的窗体也可以吗?(我也太笨了!怎么说不明白了呢!就是那个意思,是用鼠标点什么都好使吗?)
takdick 2002-10-22
  • 打赏
  • 举报
回复
var
p:tpoint;
i:hwnd;
procedure tform1.timer1timer(var sender:tobject);
begin
begin
getcursorpos(p);
i:=windowfrompoint(p);
if i <> 0 then
edit1.text:='handle is'+inttostr(i);
end;
wxjh 2002-10-22
  • 打赏
  • 举报
回复
直接根据点进行判断举例:
var
p:TPOINT;
....
....
BEGIN
getcursorpos(p); //得到鼠标的位置
windowformpoint(p);
....
end;

1,184

社区成员

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

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