C # Development Instance Mouse Chapter

Source: Internet
Author: User

Operation Control of the mouse:

The mouse is an important part of the computer, there are many default settings, such as double-click time interval, flicker frequency, movement speed, etc., this article uses C # to obtain these basic information.

1.1 Getting mouse information

① instance 001 get mouse Double-click time interval

The main API function used is getdoubleclicktime. Functions are primarily used to determine the time interval between 2 consecutive mouse clicks that are processed into a double-click.

The main program code is as follows:

1[DllImport ("user32.dll", EntryPoint ="Getdoubleclicktime")]2 3          Public Static extern intgetdoubleclicktime ();4 5         Private voidFrm_main_load (Objectsender, EventArgs e)6         {7 8Label1. Text = Getdoubleclicktime () +"milliseconds";9}

② instance 002 get cursor blink frequency

The main API function is Getcaretblinktime, which gets the time the cursor blinks, the return value is the int type, and the millisecond is the unit.

1[DllImport ("user32.dll", EntryPoint ="Getcaretblinktime")]2 3          Public Static extern intgetcaretblinktime ();4 5         Private voidFrm_main_load (Objectsender, EventArgs e)6         {7 8Label1. Text = Getcaretblinktime () +"milliseconds";9}

③ instance 003 Gets the number of mouse keys

A variety of mouse types, in addition to the key, there are rollers, and some with function keys, the main use of the API function for Getsystemmetrics,intcount to specify the information to obtain.

1          Public Const intSM = +;2 3[DllImport ("user32.dll", EntryPoint ="GetSystemMetrics")]4 5          Public Static extern intGetSystemMetrics (intintcount);6 7         Private voidFrm_main_load (Objectsender, EventArgs e)8         {9             intIntcon =GetSystemMetrics (SM);TenLabel1. Text ="number of mouse keys"+ Intcon +"a"; One}

④ instance 004 shows the mouse waiting aperture

The cursor property of the form class is used primarily to get or set the cursors that appear when the mouse is over the form.

1         Private void Frm_main_load (object  sender, EventArgs e)2        {3this              . Cursor = cursors.waitcursor; 4         }

⑤ get the position of the mouse on the form

Getting the mouse position is important in the development process by clicking the mouse to get the current position of the mouse. It mainly uses the X and Y properties of the MouseEventArgs class, and the MouseEventArgs class provides data for MouseUp, MouseDown, and MouseMove events.

1         Private void Frm_mousedown (object  sender, MouseEventArgs e)2        {3              this. Label1. Text = e.x.tostring; 4             this. Label2. Text = e.y.tostring; 5         }

⑥ Recording Mouse behavior

⑦ the mouse click Key by intercepting system messages

1.2 Basic Mouse settings

① mouse pointer shape

The mouse pointer often has multiple styles, such as "Wait" and open the linked "small hand". This example puts the mouse on the label to show the small hand.

1       this. Label1. Cursor = Cursors.hand;

② the graphic style of the mouse

When the user uses the mouse, in order to be beautiful, customizes the mouse picture.

1         Private void button1_click (object  sender, EventArgs e)2        {3 This             new Cursor ("pen_il.cur");   // Change mouse picture 4         }

③ around key interchange function

In the control Panel, the user can set the mouse settings, here around the key interchange used by the API function for Swapmousebutton. In Swapmousebutton (), return to the true exchange around key, otherwise normal.

1[System.Runtime.InteropServices.DllImport ("user32.dll", EntryPoint ="Swapmousebutton")]2 3          Public Static extern intSwapmousebutton (intbswap);4 5          Public voidDefultrightbutton ()6         {7Swapmousebutton (1);8         }9          Public voidDefultleftbutton ()Ten         { OneSwapmousebutton (0); A         } -  -         Private voidButton1_Click (Objectsender, EventArgs e) the         { -              This. Defultleftbutton (); -         } -         Private voidButton2_Click (Objectsender, EventArgs e) +         { -              This. Defultrightbutton (); +}

④ Fixed Mouse control area

In real-world applications, it is sometimes possible to limit the movement of the mouse so that it can only move in one area.

1         Private voidButton1_Click (Objectsender, EventArgs e)2         {3              This. Cursor =NewCursor (Cursor.Current.Handle);4Cursor.position =NewPoint (cursor.position.x, CURSOR.POSITION.Y);5Cursor.clip =NewRectangle ( This. Location, This. Size);6         }7         Private voidButton2_Click (Objectsender, EventArgs e)8         {9screen[] Screens =Screen.allscreens;Ten              This. Cursor =NewCursor (Cursor.Current.Handle); OneCursor.clip = screens[0]. Bounds; A}

1.3 Application of mouse operation in practice

① Hide mouse button

In practical applications, the mouse is sometimes hidden and manipulated by using the keyboard. The main API function used is showcursor. ShowCursor (bool bShow); BShow is true to show the pointer if False to hide the pointer.

1[System.Runtime.InteropServices.DllImport ("user32.dll", EntryPoint ="ShowCursor")]2          Public Static extern BOOLShowCursor (BOOLbShow);3 4         Private voidButton1_Click (Objectsender, EventArgs e)5         {6ShowCursor (false);//Hide Mouse7         }8         Private voidButton2_Click (Objectsender, EventArgs e)9         {TenShowCursor (true);//Show Mouse One}

C # Development Instance Mouse Chapter

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.