티스토리 뷰

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;
   }
}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함