C uses mouse_event function to simulate mouse function

  • 2021-07-10 20:35:45
  • OfStack

Below, I share the C # analog mouse for everyone through the code. The specific contents are as follows:

Presumably, many people may encounter small functions that need to simulate mouse clicks in project development, and many people will adopt the function mouse_event after 100 years, but I don't want to discuss how to use the function mouse_event, because it doesn't make much sense.


static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo) 
{ 
 int x = dx, y = dy; 
 edit_position(dwFlags, dx, dy, ref x, ref y); 
 IntPtr hWndFromPoint = WindowFromPoint(x, y); 
 screen_to_client(hWndFromPoint, ref x, ref y); 
 send_message(hWndFromPoint, dwFlags, cButtons, x, y); 
} 


What did you find in the above code? If you find out that you know what this article is about, maybe you will be interested in reading it, but it's okay to think that I am working on the construction site in such a miserable situation now.

When the mouse clicks on the target, it will deliver messages to the target window clicked by the mouse. Different messages will be delivered according to different mouse buttons and states. A complete "Left Mouse Click" event process is "WM_LBUTTONDOWN +

WM_LBUTTONUP "means that the mouse" first presses the left key + then lifts the left key ". Because mouse_event can simulate the mouse click process instead of a direct complete mouse click process, there is also" press and lift "

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);
mouse_event the cursor will not move to the relative position when the quantity of MOUSEEVENTF_MOVE is not provided. "Relative position of cursor = current position of cursor + new position of cursor" If the absolute position of "MOUSEEVENTF_ABSOLUTE" is provided, the "new position of cursor" will prevail instead of adding "current position of cursor"


static void edit_position(int dwFlags, int dx, int dy, ref int x, ref int y) 
{ 
 Point pos = MousePosition; 
 x = x + pos.X; 
 y = y + pos.Y; 
 if ((dwFlags | MOUSEEVENTF_ABSOLUTE) == dwFlags) 
  SetCursorPos(dx, dy); 
 if ((dwFlags | MOUSEEVENTF_MOVE) == dwFlags) 
  SetCursorPos(x, y); 
} 

The edit_position function is mainly used for MOUSEEVENTF_MOVE and MOUSEEVENTF_ABSOLUTE

1 support for relative/absolute cursor position modification


static void send_message(IntPtr hWnd, int dwFlags, int cButtons, int x, int y) 
{ 
 if ((dwFlags | MOUSEEVENTF_LEFTDOWN) == dwFlags) 
  SendMessage(hWnd, WM_LBUTTONDOWN, cButtons, MakeDWord(x, y)); 
 if ((dwFlags | MOUSEEVENTF_LEFTUP) == dwFlags) 
  SendMessage(hWnd, WM_LBUTTONUP, cButtons, MakeDWord(x, y)); 
 if ((dwFlags | MOUSEEVENTF_RIGHTDOWN) == dwFlags) 
  SendMessage(hWnd, WM_RBUTTONDOWN, cButtons, MakeDWord(x, y)); 
 if ((dwFlags | MOUSEEVENTF_RIGHTUP) == dwFlags) 
  SendMessage(hWnd, WM_RBUTTONUP, cButtons, MakeDWord(x, y)); 
 if ((dwFlags | MOUSEEVENTF_MIDDLEDOWN) == dwFlags) 
  SendMessage(hWnd, WM_MBUTTONDOWN, cButtons, MakeDWord(x, y)); 
 if ((dwFlags | MOUSEEVENTF_MIDDLEUP) == dwFlags) 
  SendMessage(hWnd, WM_MBUTTONUP, cButtons, MakeDWord(x, y)); 
} 

send_message function is mainly used to simulate the process of mouse clicking, above I mentioned "first left press + then left lift" in the above code you will see very clearly, if you can try on the contrary, what will be the consequences

It's better for you to do it yourself.


static int MakeDWord(int low, int high) 
{ 
 return low + (high * Abs(~ushort.MaxValue)); 
} 
static int Abs(int value) 
{ 
 return ((value >> 31) ^ value) - (value >> 31); 
} 
MakeDWord /  To merge integers, the function mainly combines two short Merge into 1 A int , divided into low , high Two parts 
 
 
static bool screen_to_client(IntPtr hwnd, ref int x, ref int y) 
{ 
 bool bRetVal = false; 
 Point lpptPos = new Point(x, y); 
 if ((bRetVal = ScreenToClient(hwnd, ref lpptPos))) 
 { 
  x = lpptPos.X; 
  y = lpptPos.Y; 
 } 
 return bRetVal; 
} 
screen_to_client Function is named Siyi, which is mainly used to convert the coordinates on the screen to the corresponding coordinates on the window client  
public const int WM_LBUTTONDOWN = 513; //  Press the left mouse button  
public const int WM_LBUTTONUP = 514; //  Lift the left mouse button  
public const int WM_RBUTTONDOWN = 516; //  Right mouse button down  
public const int WM_RBUTTONUP = 517; //  Lift the right mouse button  
public const int WM_MBUTTONDOWN = 519; //  Middle mouse button press  
public const int WM_MBUTTONUP = 520; //  Middle mouse button up  
public const int MOUSEEVENTF_MOVE = 0x0001; //  Move the mouse    
public const int MOUSEEVENTF_LEFTDOWN = 0x0002; //  Press the left mouse button   
public const int MOUSEEVENTF_LEFTUP = 0x0004; //  Lift the left mouse button   
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //  Right mouse button down   
public const int MOUSEEVENTF_RIGHTUP = 0x0010; //  Lift the right mouse button    
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //  Middle mouse button press  
public const int MOUSEEVENTF_MIDDLEUP = 0x0040; //  Middle mouse button up    
public const int MOUSEEVENTF_ABSOLUTE = 0x8000; //  Absolute coordinates  


[DllImport("user32.dll", SetLastError = true)] 
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam); 
[DllImport("user32.dll", SetLastError = true)] 
public static extern IntPtr WindowFromPoint(int xPoint, int yPoint); 
[DllImport("user32.dll", SetLastError = true)] 
public static extern int SetCursorPos(int x, int y); 
[DllImport("user32.dll", SetLastError = true)] 
public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt); 
// [DllImport("user32", SetLastError = true)] 
// public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); 


Right-click (silent):


mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0); 

Double-click with the left mouse button (silent):


mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0); 

Mouse movement (relative position):


mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0); 

Mouse movement (absolute position):


mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0); 

The above content is more, please study hard, hoping to help everyone.


Related articles: