【问题标题】:Getting mouse position in c#在c#中获取鼠标位置
【发布时间】:2009-08-22 18:34:54
【问题描述】:

如何获取鼠标位置?我想要它的屏幕位置。

我启动我想要设置到当前鼠标位置的程序。

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

编辑:这必须在创建表单之前进行。

【问题讨论】:

  • 查看适用于当今版本的 .NET Framework 的更新答案。

标签: c# mouse-position


【解决方案1】:

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

【讨论】:

  • Cursor.Position 在屏幕外显示我的工具提示方式:(-
  • @Thomas Eyde:我猜,但这可能是因为鼠标位置在屏幕坐标中,而您的工具提示位置是相对于其父窗口的?您可能需要使用PointToClient
  • 是的,我必须这样做。
【解决方案2】:

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

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

【讨论】:

  • 如何引用POINT类型?
  • 添加对 System.Drawing 的引用
  • 很棒的解决方案。但是你不需要声明 struct POINT。只需使用 Win32Interop.Structs 命名空间。
  • @ManpreetSinghDhillon Win32Interop.Structs 在 .Net Core 中可用吗?如果是,在哪个 nuget 包/系统参考下?
  • @ManpreetSinghDhillon 使用您自己的结构允许您将其隐式转换为您在代码中使用的任何点,它更平滑一些。如果 Win32Interop.Structs 对您来说足够了,那么请务必继续使用它!
【解决方案3】:

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

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

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

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

【讨论】:

    【解决方案4】:

    回答你的具体例子:

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

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

    【讨论】:

      【解决方案5】:
      System.Windows.Forms.Control.MousePosition
      

      Gets the position of the mouse cursor in screen coordinates. "Position 属性与 Control.MousePosition 属性相同。"

      【讨论】:

      • 不必粗鲁。这是主要答案的替代方案。我只是更喜欢这个,因为另一个“Cursor.Position”听起来像文本类型光标恕我直言,“MousePosition”更明显。
      • @Jan Dvorak 当然,是的,我认为这可能会有所帮助。我会说这样的话“请您提供更多信息,以便我了解这与之前给出的答案有何不同?”
      • @JanDvorak 如果您认为单行者没有帮助(顺便说一句,他们有),那么它不取决于问题是 1 天还是 3 岁。 +1 替代方法。
      【解决方案6】:

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

      protected override void OnMouseMove(MouseEventArgs mouseEv) 
      

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

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

      【讨论】:

        【解决方案7】:

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

        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()
        );
        

        虽然,在实验过程中发现了 830 整数。如果有人能解释为什么这些数字确实有效,那就太棒了。


        另外,还有另一种变体(考虑到代码在表单的代码隐藏中):

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

        【讨论】:

          【解决方案8】:
             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;
            }
          

          }

          【讨论】:

            【解决方案9】:

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

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

            【讨论】:

              【解决方案10】:

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

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

              【讨论】:

                【解决方案11】:

                在提出问题时,此答案不符合条件,但今天您可以在 PresentationCore 程序集中使用命名空间 System.Windows.Input 中的静态方法 Mouse.GetPosition(IInputElement)。这在 .NET Framework 3.0 及更高版本中有效。查找更多关于https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.mouse.getposition?view=windowsdesktop-6.0#System_Windows_Input_Mouse_GetPosition_System_Windows_IInputElement_的信息

                例子:

                // displayArea is a StackPanel and txtBoxMousePosition is
                // a TextBox used to display the position of the mouse pointer.
                Point position = Mouse.GetPosition(displayArea);
                txtBoxMousePosition.Text = "X: " + position.X +
                    "\n" +
                    "Y: " + position.Y;
                

                【讨论】:

                  猜你喜欢
                  • 2022-01-15
                  • 1970-01-01
                  • 2014-07-16
                  • 2018-10-26
                  • 2015-10-22
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多