确定鼠标是否在控件上方? (在控制像素范围内) [英] Determine whether the mouse is over a control? (over a Control pixel range)

查看:219
本文介绍了确定鼠标是否在控件上方? (在控制像素范围内)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数应确定鼠标是否超出像素范围(特定控件的像素范围)

I'm trying to write a function that should determine whether the mouse is over a range in pixels (the pixel range of a specific Control)

问题是该功能仅适用于Form的边界,不适用于按钮或我测试过的任何其他控件...我缺少什么?

The problem is that the function only works for the bounds of the Form, don't work for buttons or any other control that I've tested ...what I'm missing?

''' <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:我知道Mouse事件的用法,但是此功能仅用于常规用法.

PS: I know the usage of the Mouse events, but this function is for generic usage.

推荐答案

您需要转换 ClientRectangle .

You need to transform the MousePosition into client coordinates and test the ClientRectangle of the control.

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));
}

这篇关于确定鼠标是否在控件上方? (在控制像素范围内)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆