如何判断鼠标所在位置的控件名称?

csqiu 2005-07-15 01:52:30
如题.谢谢谢谢!!!!!!
...全文
183 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
byeshuang 2005-07-15
  • 打赏
  • 举报
回复
关注……

顶……
MagicianLiu 2005-07-15
  • 打赏
  • 举报
回复
可以实现:
大概思路:用GetCursorPos获得鼠标坐标X,Y(象素)
然后变量窗体内的控件集合(controls)
计算每个控件的坐标和长宽,转成在屏幕上的坐标(象素)。但是缇和象素不知道如何转换的。转换的不对,有的控件可以,有的不可以。

参考代码
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI '定义点(Point)结构
X As Long '点在X坐标(横坐标)上的坐标值
Y As Long '点在Y坐标(纵坐标)上的坐标值
End Type
Dim p As POINTAPI
GetCursorPos p
Me.Caption = p.X & "," & p.Y
For Each obj In Controls
If (p.X > (Me.Left + obj.Left) \ 15 And p.X < (Me.Left + obj.Left + obj.Width) \ 15) And (p.Y > (Me.Top + obj.Top) \ 15 And p.Y < (Me.Top + obj.Top + obj.Height) \ 15) Then '不好意思,象素和缇不知道咋转换。。
MsgBox obj.Name
Exit For
End If
Next
csqiu 2005-07-15
  • 打赏
  • 举报
回复
可是有多个控件啊.这样一来量太大.
byeshuang 2005-07-15
  • 打赏
  • 举报
回复
那就把那个控件的所在位置的范围记下来,鼠标一进入这个范围就取得控件名称。
wangtopcool 2005-07-15
  • 打赏
  • 举报
回复
用个笨办法
Dim pRect As Rect

Private Sub Form_Load()
pRect.Left = Picture1.Left
pRect.top = Picture1.top
pRect.right = pRect.Left + Picture1.Width
pRect.buttom = pRect.top + Picture1.Height
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X > pRect.Left And X < pRect.right And Y > pRect.top And pRect.buttom Then
Picture1.ToolTipText = "在Picture1内"
End If
End Sub
csqiu 2005-07-15
  • 打赏
  • 举报
回复
谢谢.我的本意是给每个控件加ToolTipText功能,但原先的控件没这个属性而且连 MouseMove的功能也没有,所以只好自己做.因而必须知道鼠标所在位置的控件名称.不知各位有和高招?谢谢.
byeshuang 2005-07-15
  • 打赏
  • 举报
回复
在你的每个控件的mousemove事件得到该控件的名称,就是很麻烦。呵呵

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ToolTipText = Me.Command1.Caption
End Sub
唐古拉山 2005-07-15
  • 打赏
  • 举报
回复
只能取得控件的类名

另外,Label控件取不到类名称的


wangtopcool 2005-07-15
  • 打赏
  • 举报
回复
晕,搞错了
没看清楚题目,不好意思
wangtopcool 2005-07-15
  • 打赏
  • 举报
回复
在窗体上放两个textbox控件
Private Sub Form_Load()
Me.ScaleMode = 3
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = X
Text2.Text = Y
End Sub

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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