본문 바로가기

C#

C# WinAPI - Mouse_Event

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, int dwData, int dwExtraInfo);

[DllImport("user32.dll")] // 커서 위치 제어
static extern int SetCursorPos(int x, int y);

private const uint LMB_DOWN = 0x00000002;		// Left Mouse Button Down
private const uint LMB_UP = 0x00000004;			// Left Mouse Button Up
private const uint RMB_DOWN = 0x00000008;		// Right Mouse Button Down
private const uint RMB_UP = 0x000000010;		// Right Mouse Button Up
private const uint MWB_DOWN = 0x00000020;		// Mouse Wheel Button Down
private const uint MWB_UP = 0x000000040;		// Mouse Wheel Button Up
private const uint MW_SCROLL = 0x00000800;		// Mouse Wheel Scroll (120 ~ -120)

// x = 100, y = 100 Mouse Click
SetCursorPos(100, 100);
mouse_event(LMB_DOWN, 0, 0, 0, 0);
mouse_event(LMB_UP, 0, 0, 0, 0);
mouse_event(MW_SCROLL, 0, 0, -10, 0);

// Drag
mouse_event(LMB_DOWN, 0, 0, 0, 0);
SetCursorPos(100, 100); // using loop 10 point move.
mouse_event(LMB_UP, 0, 0, 0, 0);

// Click
mouse_event(LMB_DOWN | LMB_UP, 0, 0, 0, 0); // ?

// want mouse position X, Y
Cursor.Position = new Point(X,Y);