简体   繁体   English

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

[英]Get Mouse Cursor position in a C# console app

So I need to get the mouse position in a C# Console app.所以我需要在 C# 控制台应用程序中获取鼠标位置。 Not the cursor in the app.不是应用程序中的光标。 Like say the cursor was at the top corner of the screen it would output 0,0.就像说光标在屏幕的顶角它会输出 0,0。 and i need to save the X and Y to int variables我需要将 X 和 Y 保存到 int 变量

But the mouse pointer anywhere out or in the app.但是鼠标指针在应用程序外或应用程序中的任何位置。

EDIT:编辑:

How Do I get the values of "GetCursorPos()" (the X and Y)我如何获得“GetCursorPos()”的值(X 和 Y)

This program will get X, Y position of mouse on screen each 1 second该程序将每 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来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM