본문 바로가기
C++ 200제/코딩 IT 정보

C# 마우스 매크로 만들기 Mouse 자동 클릭 프로그램 강좌

by vicddory 2019. 2. 22.
반응형

C# 마우스 매크로, Mouse 클릭, 위치 이동 소스 예제


웹 서핑으로 얻은 소스 정리하여 소개합니다.


마우스 매크로에 필요한 기능은 2개입니다.


  1. 마우스 커서 위치 이동
  2. 마우스 자동 클릭 이벤트 발생



C# 마우스 매크로 만들기 Mouse 자동 클릭 프로그램 강좌C# 마우스 매크로 만들기 Mouse 자동 클릭 프로그램 강좌





그래서, 아래처럼 『마우스 매크로』에 필요한 MouseEvent 함수, SetCursorPos 함수를 선언합니다.


1
2
3
4
5
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void MouseEvent(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
 
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
cs
  • MouseEvent 함수 - 마우스 자동 클릭 이벤트 발생
  • SetCursorPos 함수 - 마우스 커서 위치 이동 발생


그리고 두 함수를 이용할 수 있도록 마우스 매크로용 함수를 하나 더 선언합니다. 이 함수는 마우스 커서를 이동한 후 자동 클릭 이벤트를 발생합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void MouseSetPosCustom(int x, int y, int interval = 100)
{
    try
    {
        SetCursorPos(x, y);
        stoppeing_event_.WaitOne(interval_);
 
        MouseClickCustom(interval);
    }
    catch (Exception e)
    {
        MessageBox.Show("MouseSetPosCustom\r\n" + e.Message);
    }
}
cs


5번 SetCursorPos 함수에 x, y 좌표만 인자로 전달하면 커서는 이동합니다.


6번 라인처럼 ManualResetEvent를 주어 혹시 모를 윈도우 폼 블록 현상도 방지합니다. 잠시 쉬어가는 여유를 두지 않으면 어떤 동작을 하던 사용자 눈에 마우스 이동이 보이질 않습니다. 그래서 약간의 여유를 두는 것이 좋습니다.


C# 마우스 매크로, Mouse 클릭, 위치 이동 소스 예제[C# 마우스 제어 소스]


이어서 마우스 이동에 필요한 변수입니다. 마우스 매크로에 없어선 안 될 부분이기도 합니다.


1
2
3
4
private const int kMouseEventMove = 0x0001/* mouse move */
private const int kMouseEventLeftDown = 0x0002/* left button down */
private const int kMouseEventLeftUp = 0x0004/* left button up */
private const int kMouseEventRightDown = 0x0008/* right button down */
cs


그냥 그대로 사용하시면 돼요. 변수 이름만 바꿔서요.


그다음 8번 라인의 『MouseClickCustom 함수』는 아래 코드입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
public void MouseClickCustom(int interval = 100)
{
    try
    {
        MouseEvent(kMouseEventLeftDown, 0000);
        MouseEvent(kMouseEventLeftUp, 0000);
        stoppeing_event_.WaitOne(interval);
    }
    catch (Exception e)
    {
        MessageBox.Show("MouseClickCustom\r\n" + e.Message);
    }
}
cs


5번 라인처럼 마우스 왼쪽 버튼을 아래로 내리고, 이어서 위로 올려줍니다.


마찬가지로 자동 클릭 후에 약간의 여유를 둡니다.



C# 마우스 매크로, Mouse 클릭, 위치 이동 소스 예제C# 마우스 매크로 만들기 Mouse 자동 클릭 프로그램 강좌


아래는 마우스 매크로 전체 소스입니다.



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class MouseControl
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void MouseEvent(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
 
    [DllImport("user32")]
    public static extern int SetCursorPos(int x, int y);
 
    private const int kMouseEventMove = 0x0001/* mouse move */
    private const int kMouseEventLeftDown = 0x0002/* left button down */
    private const int kMouseEventLeftUp = 0x0004/* left button up */
    private const int kMouseEventRightDown = 0x0008/* right button down */
 
    private readonly ManualResetEvent stoppeing_event_ = new ManualResetEvent(false);
    TimeSpan interval_;
 
    public MouseControl()
    {
        interval_ = TimeSpan.FromMilliseconds(100);
        stoppeing_event_.Reset();
    }
 
    public void MouseClickCustom(int interval = 100)
    {
        try
        {
            MouseEvent(kMouseEventLeftDown, 0000);
            MouseEvent(kMouseEventLeftUp, 0000);
            stoppeing_event_.WaitOne(interval);
        }
        catch (Exception e)
        {
            MessageBox.Show("MouseClickCustom\r\n" + e.Message);
        }
    }
 
    public void MouseSetPosCustom(int x, int y, int interval = 100)
    {
        try
        {
            SetCursorPos(x, y);
            stoppeing_event_.WaitOne(interval_);
 
            MouseClickCustom(interval);
        }
        catch (Exception e)
        {
            MessageBox.Show("MouseSetPosCustom\r\n" + e.Message);
        }
    }
}
cs


C# 마우스 매크로, Mouse 클릭, 위치 이동 소스 예제

반응형