C # Control Mouse operation

Source: Internet
Author: User

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();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.