Method of C simulating window to operate mouse

  • 2021-07-06 11:40:15
  • OfStack

In this paper, an example is given to describe the method of C # simulating window to operate mouse. Share it for your reference. The specific implementation method is as follows:


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace winapi
{
  class Program
  {
    [DllImport("user32.dll", EntryPoint = "mouse_event", SetLastError = true)]
    private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    const int MOUSEEVENTF_MOVE = 0x0001;  //  Move the mouse 
    const int MOUSEEVENTF_LEFTDOWN = 0x0002;//  Simulate left mouse button pressing 
    const int MOUSEEVENTF_LEFTUP = 0x0004; // Simulate the left mouse button lifting 
    const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // Simulated right mouse button press 
    const int MOUSEEVENTF_RIGHTUP = 0x0010;//  Simulate right mouse button lifting 
    const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // Simulate middle mouse button press 
    const int MOUSEEVENTF_MIDDLEUP = 0x0040; // Simulate middle mouse button lifting 
    const int MOUSEEVENTF_ABSOLUTE = 0x8000; // Indicate whether absolute coordinates are used 
    static void Main(string[] args)
    {
      //  Move the mouse 
      mouse_event(MOUSEEVENTF_MOVE, 400, 0, 0, 0);
      // Click the right mouse button 
      mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 410, 0, 0, 0);
      Console.ReadLine();
    }
  }
}

I hope this article is helpful to everyone's C # programming.


Related articles: