如果Cursor位于位置

时间:2018-06-02 05:20:36

标签: c# position cursor

if (Cursor.Position == closeButton.Location)
{
    closeButton.BackColor = Color.FromArgb(255, 231, 76, 60);
}

这个if语句由于某种原因不起作用,有什么帮助吗?

我希望检查Cursor位置是否在Location集中。

1 个答案:

答案 0 :(得分:3)

您需要检查按钮的ClientRectangle属性。所以这是正确的语法:

if (closeButton.ClientRectangle.Contains(closeButton.PointToClient(Cursor.Position)))
{
    closeButton.BackColor = Color.FromArgb(255, 231, 76, 60);
}