问题描述
如何在不先移动光标位置的情况下模拟鼠标点击?
我有一个程序在前台窗口上执行鼠标点击,我在其中使用SendInput或SendMessage模拟鼠标左键单击。问题是,如果我将光标设置为点击位置将进行点击,但是如果我没有设置光标而不是没有点击发生,即使通过我传递x和y点。我想在不需要实际移动光标的情况下执行鼠标左键。
提前谢谢。
我尝试过:
// -------------- ----------------- SendInput -------------------------------- //
How to simulate mouse clicks without moving the cursor position first?
I have a program that perform mouse click on foreground window, in which I am using SendInput or SendMessage to emulate left mouse click. The issue is, that if I set cursor to clicking position will make the click, however if I don't set cursor than no click happens even trough I do pass x and y points. I would like to perform left mouse click without the need of actually moving the cursor at all.
Thank you in advance.
What I have tried:
// ------------------------------- SendInput --------------------------------//
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[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
}
enum SendInputEventType : int
{
InputMouse,
InputKeyboard,
InputHardware
}
enum SystemMetric
{
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
}
[DllImport("user32.dll")]
static extern int GetSystemMetrics(SystemMetric smIndex);
public static void ClickLeftMouseButton(Point p)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = (p.X * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
mouseInput.mkhi.mi.dy = (p.Y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
mouseInput.mkhi.mi.mouseData = 0;
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN | MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP | MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}
// ------------------------ ------- SendInput -------------------------------- //
// ------------------------------- SendMessage -------- ------------------------ //
// ------------------------------- SendInput --------------------------------//
// ------------------------------- SendMessage --------------------------------//
public enum WMessages : int
{
WM_LBUTTONDOWN = 0x201, //Left mousebutton down
WM_LBUTTONUP = 0x202, //Left mousebutton up
WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
WM_RBUTTONDOWN = 0x204, //Right mousebutton down
WM_RBUTTONUP = 0x205, //Right mousebutton up
WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
WM_KEYDOWN = 0x100, //Key down
WM_KEYUP = 0x101, //Key up
WM_CLICK = 0x00F5,
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string sClassName, string sAppName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
private void button1_Click(object sender, EventArgs e)
{
Point pt = new Point(263, 88);
IntPtr handle = WindowFromPoint(pt.X, pt.Y);
SendMessage(this.Handle, (int)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X, pt.Y));
}
// ------------------------------- SendMessage --------------- ----------------- //
}
// ------------------------------- SendMessage --------------------------------//
推荐答案
这篇关于鼠标模拟器无需设置光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!