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

在C#控制台应用程序中获取鼠标光标位置,c#,.net-core,console,C#,.net Core,Console,因此,我需要在C#Console应用程序中获取鼠标位置。 不是应用程序中的光标。比如说光标在屏幕的上角,它会输出0,0。我需要将X和Y保存为int变量 但是鼠标指针在应用程序中的任何位置 编辑: 如何获取“GetCursorPos()”(X和Y)的值 这个程序每1秒将获得鼠标在屏幕上的X,Y位置 using System; using System.Runtime.InteropServices; using System.Drawing; using System.Threading; na

因此,我需要在C#Console应用程序中获取鼠标位置。 不是应用程序中的光标。比如说光标在屏幕的上角,它会输出0,0。我需要将X和Y保存为int变量

但是鼠标指针在应用程序中的任何位置

编辑:

如何获取“GetCursorPos()”(X和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);
    }
}

在windows上,您可以使用WINAPI本机函数(
GetCursorPos
)进行此操作。这是否回答了您的问题?我需要得到它,而不是设置它@Маааааааааааааа107。您可以将WinForms库添加到项目中并使用Cursor.Position(它支持get和set),或者使用WinApi,但不使用SetCursorPos,只需使用GetCursorPos,我如何获取“GetCursorPos()的值”(X和Y)