Code/C#

Cursor.Position, SetCursorPos, mouse_event 샘플코드

Hide Code 2008. 4. 18. 13:12
Cursor.Position, SetCursorPos, mouse_event 를 이용해서 마우스를 컨트롤하는 샘플코드이다.

원하는 위치로 이동할 수도 있고, 마우스 클릭도 조절할 수 있다.

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

namespace MouseSample
{
   class Program
   {
       static void Main(string[] args)
       {
           Point myPoint = new Point();
           myPoint.X = 10;
           myPoint.Y = 10;
           Cursor.Position = myPoint;

           Thread.Sleep(500);

           mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, (UIntPtr)0);
           mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, (UIntPtr)0);

           Thread.Sleep(500);

           SetCursorPos(25, 1030);

           Thread.Sleep(500);

           mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, (UIntPtr)0);
           mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, (UIntPtr)0);

           Thread.Sleep(500);

           myPoint.X = Cursor.Position.X + 200;
           myPoint.Y = Cursor.Position.Y - 200;
           Cursor.Position = myPoint;
       }

       [DllImport("user32.dll")]
       static extern bool SetCursorPos(int X, int Y);

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

       private const uint MOUSEEVENTF_MOVE = 0x0001;
       private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
       private const uint MOUSEEVENTF_LEFTUP = 0x0004;
       private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
       private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
       private const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
       private const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
       private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
   }
}