반응형
C #에서 마우스 클릭을 어떻게 시뮬레이트합니까?
C #에서 마우스 클릭을 어떻게 시뮬레이트합니까?
과거의
어딘가
에서 찾은 예
입니다. 도움이 될 수 있습니다.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public Form1()
{
}
public void DoMouseClick()
{
//Call the imported function with the cursor's current position
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
//...other code needed for the application
}
현재 사용중인 아래 코드를 생성하기 위해 여러 소스를 결합했습니다. 또한 Windows.Forms 참조를 제거하여 추가 참조없이 콘솔 및 WPF 응용 프로그램에서 사용할 수 있습니다.
using System;
using System.Runtime.InteropServices;
public class MouseOperations
{
[Flags]
public enum MouseEventFlags
{
LeftDown = 0x00000002,
LeftUp = 0x00000004,
MiddleDown = 0x00000020,
MiddleUp = 0x00000040,
Move = 0x00000001,
Absolute = 0x00008000,
RightDown = 0x00000008,
RightUp = 0x00000010
}
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePoint lpMousePoint);
[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
public static void SetCursorPosition(int x, int y)
{
SetCursorPos(x, y);
}
public static void SetCursorPosition(MousePoint point)
{
SetCursorPos(point.X, point.Y);
}
public static MousePoint GetCursorPosition()
{
MousePoint currentMousePoint;
var gotPoint = GetCursorPos(out currentMousePoint);
if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
return currentMousePoint;
}
public static void MouseEvent(MouseEventFlags value)
{
MousePoint position = GetCursorPosition();
mouse_event
((int)value,
position.X,
position.Y,
0,
0)
;
}
[StructLayout(LayoutKind.Sequential)]
public struct MousePoint
{
public int X;
public int Y;
public MousePoint(int x, int y)
{
X = x;
Y = y;
}
}
}
System.Windows.Forms의 Button과 같은 일부 컨트롤에는 "PerformClick"메서드가 있습니다.
Mouse.Click();
Microsoft.VisualStudio.TestTools.UITesting
Marcos가 게시 한 코드를 시도했지만 효과가 없었습니다. 내가 Y 좌표에 주어진 것은 커서가 Y 축에서 움직이지 않았습니다. 아래 코드는 응용 프로그램이 아닌 바탕 화면의 왼쪽 모서리를 기준으로 커서 위치를 원할 때 작동합니다.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_MOVE = 0x0001;
public void DoMouseClick()
{
X = Cursor.Position.X;
Y = Cursor.Position.Y;
//move to coordinates
pt = (Point)pc.ConvertFromString(X + "," + Y);
Cursor.Position = pt;
//perform click
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
I only use mouse_event function to actually perform the click. You can give X and Y what coordinates you want, i used values from textbox:
X = Convert.ToInt32(tbX.Text);
Y = Convert.ToInt32(tbY.Text);
they are some needs i can't see to dome thing like Keith or Marcos Placona did instead of just doing
using System;
using System.Windows.Forms;
namespace WFsimulateMouseClick
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1_Click(button1, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, 1, 1, 1));
//by the way
//button1.PerformClick();
// and
//button1_Click(button1, new EventArgs());
// are the same
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("clicked");
}
}
}
참고URL : https://stackoverflow.com/questions/2416748/how-do-you-simulate-mouse-click-in-c
반응형
'program tip' 카테고리의 다른 글
std :: swap ()을 오버로드하는 방법 (0) | 2020.07.28 |
---|---|
C ++에서 stringstream에서 string으로 어떻게 변환합니까? (0) | 2020.07.28 |
파이썬은 여러 파일 형식을 가져옵니다 (0) | 2020.07.28 |
adb shell을 통해 활동을 시작할 수 있습니까? (0) | 2020.07.28 |
동일한 통합 문서의 여러 워크 시트에 대해 판다를 사용하여 pd.read_excel () (0) | 2020.07.28 |