Control mouse operation includes many kinds, such as limited mouse movement range, set the mouse around the key, control the mouse display and hide. In this section, you will learn about controlling mouse operations with two specific examples.
1. Limit the range of mouse movement
Using API functions Clipcursor and GetWindowRect can realize the function of restricting the range of mouse movement. The API function declaration is as follows:
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
public extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public extern static int GetWindowRect(int hwnd, ref RECT lpRect);
Example Controls mouse movement
This example implements the ability to restrict the range of mouse movement through API functions Clipcursor and GetWindowRect.
The main program code is as follows.
Click the control Mouse Movement button, the mouse can only move in the form, the key code is as follows: public struct RECT//声明参数的值
{
public int left;
public int top;
public int right;
public int bottom;
}
public void Lock(System.Windows.Forms.Form ObjectForm)
{
RECT _FormRect = new RECT();
GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
ClipCursor(ref _FormRect);
} Click the "Resume Move" button, the mouse resumed movement, the key code is as follows:public void UnLock()
{
RECT _ScreenRect = new RECT();
_ScreenRect.top = 0;
_ScreenRect.left = 0;
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
ClipCursor(ref _ScreenRect); }
2. Mouse settings
Setting the mouse includes setting the left and right mouse keys, displaying and hiding the mouse, and setting the time interval for double-clicking the mouse. The mouse is typically set using API functions Swapmousebutton, ShowCursor, Setdoubleclicktime, and Getdoubleclicktime. The declarations of these functions are as follows:
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
public extern static int SwapMouseButton(int bSwap);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
public extern static bool ShowCursor(bool bShow);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
public extern static int SetDoubleClickTime(int wCount);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
public extern static int GetDoubleClickTime();