在c#中获取鼠标位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1316681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:03:35  来源:igfitidea点击:

Getting mouse position in c#

c#mouse-position

提问by Athiwat Chunlakhan

How do I get the mouse position? I want it in term of screen position.

我如何获得鼠标位置?我想要它在屏幕位置方面。

I start my program I want to set to the current mouse position.

我启动了我想设置为当前鼠标位置的程序。

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

Edit:This must happen before the form is created.

编辑:这必须在创建表单之前发生。

采纳答案by RichieHindle

You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."

您应该使用System.Windows.Forms.Cursor.Position:“在屏幕坐标中表示光标位置的点。”

回答by Kevin LaBranche

To get the position look at the OnMouseMove event. The MouseEventArgs will give you the x an y positions...

要获得位置,请查看 OnMouseMove 事件。MouseEventArgs 将为您提供 x 和 y 位置...

protected override void OnMouseMove(MouseEventArgs mouseEv) 

To set the mouse position use the Cursor.Position property.

要设置鼠标位置,请使用 Cursor.Position 属性。

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

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

回答by adrianbanks

Cursor.Positionwill get the current screen poisition of the mouse (if you are in a Control, the MousePositionproperty will also get the same value).

Cursor.Position将获得鼠标当前的屏幕位置(如果您在Control 中,则MousePosition属性也将获得相同的值)。

To set the mouse position, you will have to use Cursor.Positionand give it a new Point:

要设置鼠标位置,您必须使用Cursor.Position并给它一个新的Point

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

You can do this in your Mainmethod before creating your form.

您可以Main在创建表单之前在您的方法中执行此操作。

回答by Mo0gles

If you don't want to reference Forms you can use interop to get the cursor position:

如果您不想引用表单,您可以使用互操作来获取光标位置:

using System.Runtime.InteropServices;
using System.Windows; // Or use whatever point class you like for the implicit cast operator

/// <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);
    // NOTE: If you need error handling
    // bool success = GetCursorPos(out lpPoint);
    // if (!success)

    return lpPoint;
}

回答by Benjamin Crouzier

To answer your specific example:

要回答您的具体示例:

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

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

Don't forget to add using System.Windows.Forms;, and adding the reference to it (right click on references > add reference > .NET tab > Systems.Windows.Forms > ok)

不要忘记添加using System.Windows.Forms;,并添加对它的引用(右键单击引用>添加引用>.NET选项卡>Systems.Windows.Forms>确定)

回答by James

System.Windows.Forms.Control.MousePosition

Gets the position of the mouse cursor in screen coordinates."The Position property is identical to the Control.MousePosition property."

获取鼠标光标在屏幕坐标中的位置。“Position 属性与 Control.MousePosition 属性相同。”

回答by DeathRs

Initialize the current cursor. Use it to get the position of X and Y

初始化当前光标。使用它来获取 X 和 Y 的位置

this.Cursor = new Cursor(Cursor.Current.Handle);
int posX = Cursor.Position.X;
int posY = Cursor.Position.Y;

回答by Carlos Alberto Flores Onofre

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

}

}

回答by V.7

If you need to get current position in form's area(got experimentally), try:

如果您需要获取表单区域中的当前位置(通过实验获得),请尝试:

Console.WriteLine("Current mouse position in form's area is " + 
    (Control.MousePosition.X - this.Location.X - 8).ToString() +
    "x" + 
    (Control.MousePosition.Y - this.Location.Y - 30).ToString()
);

Although, 8and 30integers were found by experimenting.

虽然,通过实验发现了830个整数。

Would be awesome if someone could explain why exactly these numbers ^.

如果有人能解释这些数字的确切原因,那就太棒了 ^。



Also, there's another variant(considering code is in Form's CodeBehind):

此外,还有另一种变体(考虑到代码在 Form 的 CodeBehind 中):

Point cp = this.PointToClient(Cursor.Position); // Getting a cursor's position according form's area
Console.WriteLine("Cursor position: X = " + cp.X + ", Y = " + cp.Y);

回答by Austin Traphofner

You must also have the following imports in order to import the DLL

您还必须具有以下导入才能导入 DLL

using System.Runtime.InteropServices;
using System.Diagnostics;