如何控制鼠标光标?
我的表单只有几个按钮,我想知道现在光标下有什么按钮。
PS 也许它是重复的,但我找不到这个问题的答案。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我的表单只有几个按钮,我想知道现在光标下有什么按钮。
PS 也许它是重复的,但我找不到这个问题的答案。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
查看
GetChildAtPoint
。如果控件包含在容器中,您将必须做一些额外的工作,请参阅Control.PointToClient
。Have a look at
GetChildAtPoint
. You will have to do some extra work if the controls are contained in a container, seeControl.PointToClient
.也许
GetChildAtPoint
和PointToClient
是大多数人的第一个想法。我也先用过。但是,GetChildAtPoint
无法与不可见或重叠的控件一起正常工作。这是一个运行良好的代码,它可以管理这些情况。这将使您可以在光标下方进行控制。
Maybe
GetChildAtPoint
andPointToClient
is the first idea for most people. I also used it first. But,GetChildAtPoint
doesn't work properly with invisible or overlapped controls. Here's a well-working code and it manages those situations.This will give you the control right under the cursor.
您可以通过多种方式完成此操作:
监听表单控件的
MouseEnter
事件。 “sender”参数将告诉您哪个控件引发了该事件。使用
System.Windows.Forms.Cursor.Location
获取光标位置,并使用Form.PointToClient()
将其映射到表单的坐标。然后,您可以将该点传递给Form.GetChildAtPoint()
以查找该点下的控件。安德鲁
You could do it a number of ways:
Listen to the
MouseEnter
event of your form's controls. The "sender" parameter will tell you what control raised the event.Obtain the cursor position using
System.Windows.Forms.Cursor.Location
and map it to your form's coordinates usingForm.PointToClient()
. You can then pass the point toForm.GetChildAtPoint()
to find the control under that point.Andrew
在每个按钮中定义一个鼠标悬停事件怎么样?
它将发送者按钮分配给按钮类型的公共变量
What about defining an on-Mouse-over event in each button
which assigns the sender button to a public variable of a button type