Click
機能しません-理由がわからず、解決策が見つかりません:(
すなわち。Click(150,215)
マウスをクライアント領域に移動し、そこでクリックする必要があります。
[DllImport("user32.dll")]
private static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);
[DllImport("user32", SetLastError = true)]
private static extern int SetCursorPos(int x, int y);
static void MouseMove(int x, int y)
{
Point p = new Point(x * -1, y * -1);
ScreenToClient(hWnd, ref p);
p = new Point(p.X * -1, p.Y * -1);
SetCursorPos(p.X, p.Y);
}
static void Click(int x, int y)
{
MouseMove(x, y);
SendMessage(hWnd, WM_LBUTTONDOWN, (IntPtr)0x1, new IntPtr(y * 0x10000 + x));
SendMessage(hWnd, WM_LBUTTONUP, (IntPtr)0x1, new IntPtr(y * 0x10000 + x));
}
編集: 次の方法は機能していますが、「より高度な」と言えます。
もちろん、私はそれを使用できますmouse_event
が、SendMessage() の解決策を見たいと思います...
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
const int LEFTDOWN = 0x00000002;
const int LEFTUP = 0x00000004;
static void Click(int x, int y)
{
MouseMove(x, y);
mouse_event((int)(LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(LEFTUP), 0, 0, 0, 0);
}
しばらくして、MouseInput
byを使った別の方法を見つけましSendInput
たが、非常に長いです:P
縮むことはできますか?
[Flags]
enum MouseEventFlags : uint
{
MOUSEEVENTF_MOVE = 0x0001,
MOUSEEVENTF_LEFTDOWN = 0x0002,
MOUSEEVENTF_LEFTUP = 0x0004,
MOUSEEVENTF_RIGHTDOWN = 0x0008,
MOUSEEVENTF_RIGHTUP = 0x0010,
MOUSEEVENTF_MIDDLEDOWN = 0x0020,
MOUSEEVENTF_MIDDLEUP = 0x0040,
MOUSEEVENTF_XDOWN = 0x0080,
MOUSEEVENTF_XUP = 0x0100,
MOUSEEVENTF_WHEEL = 0x0800,
MOUSEEVENTF_VIRTUALDESK = 0x4000,
MOUSEEVENTF_ABSOLUTE = 0x8000
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[StructLayout(LayoutKind.Explicit)]
struct MouseKeybdhardwareInputUnion
{
[FieldOffset(0)]
public MouseInputData mi;
}
struct MouseInputData
{
public int dx;
public int dy;
public uint mouseData;
public MouseEventFlags dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public SendInputEventType type;
public MouseKeybdhardwareInputUnion mkhi;
}
enum SendInputEventType : int
{
InputMouse,
}
static void Click(int x, int y)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = x;
mouseInput.mkhi.mi.dy = y;
mouseInput.mkhi.mi.mouseData = 0;
MouseMove(x, y);
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}