我正在尝试编写一个函数来确定鼠标是否超出像素范围(特定控件的像素范围)
问题是该函数仅适用于Form
,不适用于我测试过的按钮或任何其他控件...我缺少什么?
''' <summary>
''' Determinates whether the mouse pointer is over a pixel range of the specified control.
''' </summary>
''' <param name="Control">The control.</param>
''' <returns>
''' <c>true</c> if mouse is inside the pixel range, <c>false</c> otherwise.
''' </returns>
Private Function MouseIsOverControl(ByVal [Control] As Control) As Boolean
Return [Control].Bounds.Contains(MousePosition)
End Function
PS:我知道鼠标事件的用法,但这个函数是通用的。
你需要改造鼠标位置 https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition(v=vs.110).aspx进入客户端坐标并测试客户端矩形 https://msdn.microsoft.com/en-us/library/system.windows.forms.control.clientrectangle(v=vs.110).aspx的控制。
VB.NET
Imports System.Windows.Forms
Public Function MouseIsOverControl(ByVal c As Control) As Boolean
Return c.ClientRectangle.Contains(c.PointToClient(Control.MousePosition))
End Function
C#
using System.Windows.Forms;
public bool MouseIsOverControl(Control c)
{
return c.ClientRectangle.Contains(c.PointToClient(Control.MousePosition));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)