在 C# 控制台应用程序中获取鼠标光标位置

Get Mouse Cursor position in a C# console app

所以我需要获取 C# 控制台应用程序中的鼠标位置。 不是 应用程序中的光标。比如说光标位于屏幕的上角,它会输出 0,0。我需要将 X 和 Y 保存到 int 变量

但鼠标指针在应用程序外或应用程序内的任何位置。

编辑:

How Do I get the values of "GetCursorPos()" (the X and Y)

本程序每1秒获取鼠标在屏幕上的X、Y位置

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Threading;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                // New point that will be updated by the function with the current coordinates
                Point defPnt = new Point();

                // Call the function and pass the Point, defPnt
                GetCursorPos(ref defPnt);

                // Now after calling the function, defPnt contains the coordinates which we can read
                Console.WriteLine("X = " + defPnt.X.ToString());
                Console.WriteLine("Y = " + defPnt.Y.ToString());
                Thread.Sleep(1000);
            }
        }

        // We need to use unmanaged code
        [DllImport("user32.dll")]

        // GetCursorPos() makes everything possible
        static extern bool GetCursorPos(ref Point lpPoint);
    }
}

Source