Obtener la posición del ratón en c#


¿Cómo obtengo la posición del ratón? Lo quiero en cuanto a la posición de la pantalla.

Comienzo mi programa que quiero establecer en la posición actual del ratón.

Location.X = ??
Location.Y = ??

Edit: Esto debe suceder antes de que se cree el formulario.

Author: Athiwat Chunlakhan, 2009-08-22

8 answers

Debe usar el sistema .Windows.Forma.Cursor.Position: "Un punto que representa la posición del cursor en las coordenadas de la pantalla."

 159
Author: RichieHindle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-22 18:43:21

Si no desea hacer referencia a los Formularios, puede usar interop para obtener la posición del cursor:

/// <summary>
/// Struct representing a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public static implicit operator Point(POINT point)
    {
        return new Point(point.X, point.Y);
    }
}

/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

public static Point GetCursorPosition()
{
    POINT lpPoint;
    GetCursorPos(out lpPoint);
    //bool success = User32.GetCursorPos(out lpPoint);
    // if (!success)

    return lpPoint;
}
 71
Author: Mo0gles,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-01 15:43:41

Cursor.Position obtendrá el veneno de pantalla actual del ratón (si está en un control , la propiedad MousePosition también obtendrá el mismo valor).

Para establecer la posición del ratón, tendrá que usar Cursor.Position y darle un nuevo Punto :

Cursor.Position = new Point(x, y);

Puedes hacer esto en tu método Main antes de crear tu formulario.

 17
Author: adrianbanks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-22 19:03:06

Para responder a su ejemplo específico:

// your example
Location.X = Cursor.Position.X;
Location.Y = Cursor.Position.Y;

// sample code
Console.WriteLine("x: " + Cursor.Position.X + " y: " + Cursor.Position.Y);

No olvide agregar using System.Windows.Forms; y agregar la referencia a él (haga clic con el botón derecho en referencias > agregar referencia > pestaña.NET > Sistemas.Windows.Formularios > ok)

 13
Author: Benjamin Crouzier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-15 19:55:57
System.Windows.Forms.Control.MousePosition

Obtiene la posición del cursor del ratón en las coordenadas de la pantalla. "La propiedad de Posición es idéntica al Control.Propiedad MousePosition."

 10
Author: James,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-05 14:09:19

Para obtener la posición mira el evento OnMouseMove. Los MouseEventArgs te darán las posiciones x y y...

protected override void OnMouseMove(MouseEventArgs mouseEv) 

Para establecer la posición del ratón utilice el cursor.Propiedad de posición.

Http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx

 7
Author: klabranche,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-22 18:42:06

Inicializa el cursor actual. Úselo para obtener la posición de X e Y

this.Cursor = new Cursor(Cursor.Current.Handle);
int posX = Cursor.Position.X;
int posY = Cursor.Position.Y;
 3
Author: DeathRs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-14 13:21:26
   internal static class CursorPosition {
  [StructLayout(LayoutKind.Sequential)]
  public struct PointInter {
     public int X;
     public int Y;
     public static explicit operator Point(PointInter point) => new Point(point.X, point.Y);       
  }

  [DllImport("user32.dll")]
  public static extern bool GetCursorPos(out PointInter lpPoint);

  // For your convenience
  public static Point GetCursorPosition() {
     PointInter lpPoint;
     GetCursorPos(out lpPoint);
     return (Point) lpPoint;
  }

}

 3
Author: Carlos Alberto Flores Onofre,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-24 21:42:37