How to perform a Virtual Mouse click C# without us

2019-04-02 01:38发布

问题:

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?

回答1:

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



回答2:

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
}


回答3:

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



回答4:

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

}

}



回答5:

You'll need the resolution of the machine that it's on, use the System.Windows.Forms.Screen class, here: 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.

Here's a brief explanation / question



回答6:

[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/.



回答7:

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