■ 마우스 이벤트를 발생시키는 방법을 보여준다.
▶ MouseEventFlags.cs
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 |
namespace TestProject; /// <summary> /// 마우스 이벤트 플래그 /// </summary> [Flags] public enum MouseEventFlags { /// <summary> /// 이동 /// </summary> Move = 0x00000001, /// <summary> /// 왼쪽 DOWN /// </summary> LeftDown = 0x00000002, /// <summary> /// 왼쪽 UP /// </summary> LeftUp = 0x00000004, /// <summary> /// 오른쪽 DOWN /// </summary> RightDown = 0x00000008, /// <summary> /// 오른쪽 UP /// </summary> RightUp = 0x00000010, /// <summary> /// 가운데 DOWN /// </summary> MiddleDown = 0x00000020, /// <summary> /// 가운데 UP /// </summary> MiddleUp = 0x00000040, /// <summary> /// 절대 /// </summary> Absolute = 0x00008000 }; |
▶ MousePoint.cs
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 |
using System.Runtime.InteropServices; namespace TestProject; /// <summary> /// 마우스 포인트 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct MousePoint { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// X /// </summary> public int X; /// <summary> /// Y /// </summary> public int Y; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MousePoint(x, y) /// <summary> /// 생성자 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> public MousePoint(int x, int y) { X = x; Y = y; } #endregion } |
▶ MouseHelper.cs
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using System.Runtime.InteropServices; namespace TestProject; /// <summary> /// 마우스 헬퍼 /// </summary> public static class MouseHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 커서 위치 설정하기 - SetCursorPos(x, y) /// <summary> /// 커서 위치 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>처리 결과</returns> [DllImport("user32", EntryPoint = "SetCursorPos")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetCursorPos(int x, int y); #endregion #region 커서 위치 구하기 - GetCursorPos(mousePoint) /// <summary> /// 커서 위치 구하기 /// </summary> /// <param name="mousePoint">마우스 포인트</param> /// <returns>처리 결과</returns> [DllImport("user32")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetCursorPos(out MousePoint mousePoint); #endregion #region 마우스 이벤트 발생시키기 - mouse_event(flag, deltaX, deltaY, data, extraInformation) /// <summary> /// 마우스 이벤트 발생시키기 /// </summary> /// <param name="flag">플래그</param> /// <param name="deltaX">델타 X</param> /// <param name="deltaY">델타 Y</param> /// <param name="data">데이터</param> /// <param name="extraInformation">부가 정보</param> [DllImport("user32")] private static extern void mouse_event(int flag, int deltaX, int deltaY, int data, int extraInformation); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 커서 위치 설정하기 - SetCursorPosition(mousePoint) /// <summary> /// 커서 위치 설정하기 /// </summary> /// <param name="mousePoint">마우스 포인트</param> public static void SetCursorPosition(MousePoint mousePoint) { SetCursorPos(mousePoint.X, mousePoint.Y); } #endregion #region 커서 위치 설정하기 - SetCursorPosition(x, y) /// <summary> /// 커서 위치 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> public static void SetCursorPosition(int x, int y) { SetCursorPos(x, y); } #endregion #region 커서 위치 구하기 - GetCursorPosition() /// <summary> /// 커서 위치 구하기 /// </summary> /// <returns>마우스 포인트</returns> public static MousePoint GetCursorPosition() { MousePoint mousePoint; bool result = GetCursorPos(out mousePoint); if(!result) { mousePoint = new MousePoint(0, 0); } return mousePoint; } #endregion #region 마우스 이벤트 발생시키기 - MouseEvent(flag) /// <summary> /// 마우스 이벤트 발생시키기 /// </summary> /// <param name="flag">플래그</param> public static void MouseEvent(MouseEventFlags flag) { MousePoint mousePoint = GetCursorPosition(); mouse_event((int)flag, mousePoint.X, mousePoint.Y, 0, 0); } #endregion } |
▶ MainForm.cs
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.executeButton.Click += executeButton_Click; this.showMessageButton.Click += showMessageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 실행 버튼 클릭시 처리하기 - executeButton_Click(sender, e) /// <summary> /// 실행 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void executeButton_Click(object sender, EventArgs e) { Point clientPoint = this.showMessageButton.Location; Point screenPoint = PointToScreen(clientPoint); screenPoint.X += this.showMessageButton.Width / 2; screenPoint.Y += this.showMessageButton.Height / 2; MouseHelper.SetCursorPosition(screenPoint.X, screenPoint.Y); MouseHelper.MouseEvent(MouseEventFlags.LeftDown); MouseHelper.MouseEvent(MouseEventFlags.LeftUp ); } #endregion #region 메시지 표시 버튼 클릭시 처리하기 - showMessageButton_Click(sender, e) /// <summary> /// 메시지 표시 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void showMessageButton_Click(object sender, EventArgs e) { MessageBox.Show(this, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } #endregion } } |