如何得到当前鼠标指向的控件(如image,label这类的控件)名称?

zjpjzjcn 2002-07-08 08:25:00
在运行的应用程序中,我想当鼠标移到某一控件时得到该控件的控件名称。请各位帮忙!!
...全文
179 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjpjzjcn 2002-07-17
  • 打赏
  • 举报
回复
大家帮忙呀
zjpjzjcn 2002-07-10
  • 打赏
  • 举报
回复
那得到句柄后能不能发送消息给它,控制它呢?
如果行的话,该如何来实现呢?

请各位帮忙。
王集鹄 2002-07-08
  • 打赏
  • 举报
回复
procedure TForm1.Timer1Timer(Sender: TObject);
var
I: Integer;
S: string;
begin
S := '<没有找到>';
for I := 0 to ComponentCount - 1 do
if (Components[I] is TControl) and
PtInRect(TControl(Components[I]).BoundsRect,
TControl(Components[I]).Parent.ScreenToClient(Mouse.CursorPos)) then
S := Components[I].Name;
Caption := S;
end;
王集鹄 2002-07-08
  • 打赏
  • 举报
回复
procedure TForm1.Timer1Timer(Sender: TObject);
var
I: Integer;
S: string;
begin
S := '<没有找到>';
for I := 0 to ComponentCount - 1 do
if (Components[I] is TControl) and
PtInRect(TControl(Components[I]).BoundsRect,
TControl(Components[I]).Parent.ScreenToClient(Mouse.CursorPos)) then
S := Components[I].Name;
Caption := S;
end;
王集鹄 2002-07-08
  • 打赏
  • 举报
回复
procedure TForm1.Timer1Timer(Sender: TObject);
var
I: Integer;
S: string;
begin
S := '<没有找到>';
for I := 0 to ComponentCount - 1 do
if (Components[I] is TControl) and
PtInRect(TControl(Components[I]).BoundsRect,
TControl(Components[I]).Parent.ScreenToClient(Mouse.CursorPos)) then
S := Components[I].Name;
Caption := S;
end;
netlib 2002-07-08
  • 打赏
  • 举报
回复
还有你要处理的是窗体对象,得到窗口体handle就可以了,
不用非要得到名称。
netlib 2002-07-08
  • 打赏
  • 举报
回复
还有你要处理的是窗体对象,得到窗口体handle就可以了,
不用非要得到名称。
zjpjzjcn 2002-07-08
  • 打赏
  • 举报
回复
zswang(伴水)
谢谢!
直接窗体上的是行的,但在panel里面的label就不行了。
能不能再改进一下。
netlib 2002-07-08
  • 打赏
  • 举报
回复
还有你要处理的是窗体对象,得到窗口体handle就可以了,
不用非要得到名称。
netlib 2002-07-08
  • 打赏
  • 举报
回复
我的代码你不要写在button的事件中,
你把它写在一个timer的ontimer事件中,然后用鼠标在屏幕中移动,
所有的窗口都能得到。
surfguy 2002-07-08
  • 打赏
  • 举报
回复
伴水的是可行了。
searoom 2002-07-08
  • 打赏
  • 举报
回复
查看delphi的帮助
好像说

The WindowFromPoint function does not retrieve the handle of a hidden or disabled window, even if the point is within the window. An application should use the ChildWindowFromPoint function for a nonrestrictive search.

因此可以试试ChildWindowFromPoint函数了
王集鹄 2002-07-08
  • 打赏
  • 举报
回复
procedure TForm1.Timer1Timer(Sender: TObject);
var
I: Integer;
S: string;
vPoint: TPoint;
begin
S := '<没有找到>';
vPoint := ScreenToClient(Mouse.CursorPos);
for I := 0 to ComponentCount - 1 do
if (Components[I] is TControl) and
PtInRect(TControl(Components[I]).BoundsRect, vPoint) then begin
S := Components[I].Name;
Break;
end;
Caption := S;
end;
王集鹄 2002-07-08
  • 打赏
  • 举报
回复
procedure TForm1.Timer1Timer(Sender: TObject);
var
I: Integer;
S: string;
vPoint: TPoint;
begin
S := '<没有找到>';
vPoint := ScreenToClient(Mouse.CursorPos);
for I := 0 to ComponentCount - 1 do
if (Components[I] is TControl) and
PtInRect(TControl(Components[I]).BoundsRect, vPoint) then begin
S := Components[I].Name;
Break;
end;
Caption := S;
end;
王集鹄 2002-07-08
  • 打赏
  • 举报
回复
procedure TForm1.Timer1Timer(Sender: TObject);
var
I: Integer;
S: string;
vPoint: TPoint;
begin
S := '<没有找到>';
vPoint := ScreenToClient(Mouse.CursorPos);
for I := 0 to ComponentCount - 1 do
if (Components[I] is TControl) and
PtInRect(TControl(Components[I]).BoundsRect, vPoint) then begin
S := Components[I].Name;
Break;
end;
Caption := S;
end;
zjpjzjcn 2002-07-08
  • 打赏
  • 举报
回复
continue...
zjpjzjcn 2002-07-08
  • 打赏
  • 举报
回复
谢谢。
不过我试了还是不行啊。
它只能对可以得到焦点的控件起作用。
请继续帮忙
dext 2002-07-08
  • 打赏
  • 举报
回复
楼上有理!
其实在每一个控件中都有一个指明自己类型的属性
netlib 2002-07-08
  • 打赏
  • 举报
回复
var
ClassName: PChar;
ptCursor: TPoint;
hWndOver: HWND;
Text: PChar;
begin
GetCursorPos(ptCursor);
hWndOver := WindowFromPoint(ptCursor);
GetMem(ClassName, 100);
GetMem(Text, 255);
try
GetClassName(hWndOver, ClassName, 100);
SendMessage(hWndOver, WM_GETTEXT, 255, LongInt(Text));
Canvas.FillRect(Rect(5, 20 , PreClassLength + 20, 40));
Canvas.FillRect(Rect(5, 40 , PreTextLength + 20, 60));
PreClassLength := Canvas.TextWidth(ClassName);
PreTextLength := Canvas.TextWidth(Text);
if PreClassLength > PreTextLength then
Width := Canvas.TextWidth(ClassName) + 20
else
Width := Canvas.TextWidth(Text) + 20;
Canvas.TextOut(5, 20, string(ClassName));
Canvas.TextOut(5, 35, string(Text));
finally
FreeMem(ClassName);
FreeMem(Text);
end;
end;
把classname换成name就可以了。
bcb_fans 2002-07-08
  • 打赏
  • 举报
回复
TPoint P;
char Buf[64];
AnsiString ClassName;

GetCursorPos(&P);
GetClassName(WindowFromPoint(P),Buf,sizeof(64));
ClassName = AnsiString(Buf);


5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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