2015/10/22

C# 透過Win32控制滑鼠 mouse_event

這個方法除了滑鼠移動之外其他是都可以用的
不管你的x/y輸入正確數值也無法移動到正確的地方

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

namespace GetCurrentPoint
{
    public partial class Form1 : Form
    {
        [DllImport("User32")]
        public static extern void mouse_event(
            int dwFlags,
            int dx,
            int dy,
            int dwData,
            int dwExtraInfo
        );

        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_WHEEL = 0x0800;
        const int MOUSEEVENTF_XDOWN = 0x0080;
        const int MOUSEEVENTF_XUP = 0x1000;
        const int MOUSEEVENTF_HWHEEL = 0x01000;


        public Form1()
        {
            InitializeComponent();
            int x = 768, y = 1024;
            mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
        }


    }
}