Search code examples
c#telerik

Determine if the cursor location is in a specific area of the screen


I would like to know when my cursor is located within a specific aera ( a small rectangle for instance in the very right of the screen ).

When My cursor is in this aera, the form i'm dragging must have a higher height.

By now, i just have this :

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

}

Thus, i need to create a condition to know if my cursor is located within a specific aera ( right of the screen ) Can anyone help me on this thanks in advance.


Solution

  • Cursor.Position is in screen coordinates. You may test if the position is withing a specified range:

    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
    

    edit: to test if the cursor is inside a border area, just like @Philip wrote:

    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