How to perform a Virtual Mouse click C# without us

2019-04-02 01:09发布

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.

How would I do this?

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-02 01:24
[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/.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-02 01:29

When i simulate key strokes and mouse click, I use Windows Input Simulator

查看更多
Melony?
4楼-- · 2019-04-02 01:33

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
}
查看更多
Juvenile、少年°
5楼-- · 2019-04-02 01:38

I think the function you're looking for is 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.

This method posts messages directly on the input queue associated with the program, based on the process handle you use (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.

I'm sure if you search around the internet for samples for PostMessage mouse events you'll find plenty

查看更多
爷的心禁止访问
6楼-- · 2019-04-02 01:42

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...):

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";
    }

}

}

查看更多
放我归山
7楼-- · 2019-04-02 01:44

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

查看更多
登录 后发表回答