2015/10/22

C# 透過Win32取得滑鼠位置 GetCursorPos

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GetCurrentPoint
{
    public partial class Form1 : Form
    {
        [DllImport("User32")]
        internal extern static bool GetCursorPos(out MousePoint point);

        internal struct MousePoint {
            public int x;
            public int y;
        };

        public Form1()
        {
            InitializeComponent();
            MousePoint point;
            GetCursorPos(out point);
            Console.WriteLine(point.x + "," + point.y);
        }
    }
}