确定光标位置是否在屏幕的特定区域

本文关键字:区域 屏幕 是否 光标 位置 | 更新日期: 2023-09-27 18:24:25

我想知道我的光标何时位于特定区域(例如屏幕右侧的一个小矩形)内。

当我的光标在这个区域时,我拖动的表单必须有更高的高度。

现在,我只有这个:

private void Form1_LocationChanged(object sender, EventArgs e)
{
    if (Cursor.Position == new Point(-1037, 516))
    {
        this.Height = 450;
    }
}

因此,我需要创建一个条件来知道我的光标是否位于特定的区域(屏幕右侧)有人能帮我吗提前谢谢。

确定光标位置是否在屏幕的特定区域

private void Form1_LocationChanged(object sender, EventArgs e)
{   
     //THE POSITION OF MY RECTANGLE HERE IS ON THE UPPER LEFT
     Rectangle rec = new Rectangle(0,0,100,100); //CHANGE THIS DIMENSION TO YOUR LIKING
     if (rec.Contains(Cursor.Position))
     {
         //DO YOUR STUFF HERE
     }
}

希望这会有所帮助。

Cursor.Position在屏幕坐标中。您可以测试位置是否在指定范围内:

Const RANGE_X As Integer = 20;
Const RANGE_Y As Integer = 20;
if ( Screen.PrimaryScreen.Bounds.Width - RANGE_X <= Cursor.Position.X And _
     Cursor.Position.Y <= RANGE_Y )
   ' we're near the top right edge

编辑:测试光标是否在边界区域内,就像@Philip写的那样:

Const BORDER_SIZE As Integer = 100;     ' In pixel
Rectangle border = new Rectangle(
    BORDER_SIZE, 
    BORDER_SIZE, 
    Screen.PrimaryScreen.Bounds.Width - BORDER_SIZE, 
    Screen.PrimaryScreen.Bounds.Height - BORDER_SIZE);  
If ( Not border.Contains(Cursor.Position) ) Then
    '  ... yes the cursor is in the border area