如何在不使用鼠标的情况下执行虚拟鼠标单击 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15146275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to perform a Virtual Mouse click C# without using a mouse
提问by Frunk
I'd like to perform a click in a windows application, without using the real mouse (so I can minimize it). Much like a bot would behave.
我想在不使用真正的鼠标的情况下在 Windows 应用程序中执行单击操作(因此我可以将其最小化)。就像机器人的行为一样。
How would I do this?
我该怎么做?
采纳答案by Ron Sijm
I think the function you're looking for is PostMessage
我认为你正在寻找的功能是 PostMessage
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
You can read more about it here on codeproject, and download a demo project, which sends keystrokes.
你可以在 codeproject 上阅读更多关于它的信息,并下载一个演示项目,它发送击键。
This method posts messages directly on the input queue associated with the program, based on the process handle you use (hWnd)
此方法根据您使用的进程句柄 (hWnd),直接在与程序关联的输入队列上发布消息
You can also use this function to send mouse clicks with it, by posting button events, like so:
您还可以使用此功能通过发布按钮事件来发送鼠标点击,如下所示:
PostMessage(hWnd, WM_LBUTTONDBLCLK, 0, l);
More information about those button events can be found here on MSDN.
关于这些按钮事件的更多信息可以在 MSDN上找到。
I'm sure if you search around the internet for samples for PostMessage mouse events you'll find plenty
我敢肯定,如果您在 Internet 上搜索 PostMessage 鼠标事件的示例,您会发现很多
回答by Deep in the Code
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void MouseClick()
{
int x = 100;
int y = 100;
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
I found this at http://social.msdn.microsoft.com/forums/en-US/winforms/thread/86dcf918-0e48-40c2-88ae-0a09797db1ab/.
我在http://social.msdn.microsoft.com/forums/en-US/winforms/thread/86dcf918-0e48-40c2-88ae-0a09797db1ab/找到了这个。
回答by Zaid Amir
When i simulate key strokes and mouse click, I use Windows Input Simulator
当我模拟击键和鼠标点击时,我使用Windows 输入模拟器
回答by plast1K
You'll need the resolution of the machine that it's on, use the System.Windows.Forms.Screen
class, here: SO
您将需要它所在机器的分辨率System.Windows.Forms.Screen
,请在此处使用该类:SO
You will then need to move the mouse to that location, or avoiding that, you may need to hook to the program that's running, and send it an event that causes it to minimize.
然后,您需要将鼠标移动到该位置,或者避免该位置,您可能需要挂钩正在运行的程序,并向其发送一个事件以使其最小化。
It's going to be hard to get something like this to work with C#, as you'll need to inject that DLL into the program. A lower level language like C may be helpful.
将这样的东西与 C# 一起使用会很困难,因为您需要将该 DLL 注入到程序中。像 C 这样的低级语言可能会有所帮助。
回答by Brian
The code below is a repost from here:
下面的代码是从这里转过来的:
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);
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
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
//...other code needed for the application
}
回答by vishal dhaduk
You can use Window Automation to find Minimize button in window and perform click. I find it easy and used a lot. here is the link to understand the whole concept. https://msdn.microsoft.com/en-us/library/windows/desktop/ff486375(v=vs.85).aspx
您可以使用窗口自动化在窗口中找到最小化按钮并执行单击。我发现它很容易并且经常使用。这是了解整个概念的链接。 https://msdn.microsoft.com/en-us/library/windows/desktop/ff486375(v=vs.85).aspx
回答by BinaryLord
You can use a timer, or handle key press, and create in the timer tick or key press function simulate a mouse click, using the user32.dll file (better be in a form shape so you can handle timer interval...):
您可以使用计时器,或处理按键,并在计时器滴答或按键功能中创建模拟鼠标单击,使用 user32.dll 文件(最好采用表单形状,以便您可以处理计时器间隔...):
using System;
using System.Windows.Forms;
namespace Clicker
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int MOUSEEVENT_LEFTDOWN = 0x02;
private const int MOUSEEVENT_LEFTUP = 0x04;
private const int MOUSEEVENT_MIDDLEDOWN = 0x20;
private const int MOUSEEVENT_MIDDLEUP = 0x40;
private const int MOUSEEVENT_RIGHTDOWN = 0x08;
private const int MOUSEEVENT_RIGHTUP = 0x10;
private int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
label2.Text = "Timer is on";
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
label2.Text = "Timer is off";
}
private void timer1_Tick(object sender, EventArgs e)
{
mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
count++;
label3.Text = count + " amount of clicks";
}
}
}
}