Kingdz changed to cainiao, one C # Small instance per day-play with mouse

Source: Internet
Author: User

The example that we bring to you today is a relatively small group of people, C # controls the mouse. Haha, I think some children's shoes can be studied by themselves. You can simply imitate a button genie.

If it is more abnormal, you are powerful, and you can develop "technology software". ---- wow, WG, haha, but how can I study "good yin?

Well, I am not talking to everyone anymore. Let's get started.

You can control the mouse's movement range, control the mouse's position, control the mouse's left and right keys, and display and hide the mouse.

Control the mouse and use our system's API --- "what do you don't know about the system's API ?" Ask Google.

First, let's get the mouse position. We only need to catch this mouse, so we can proceed with the next step.

First, it is declared that. Net does not provide a function to change the pointer position and simulate click operations.

So we can only go to Windows API.

Haha, effect. First, we need to reference the namespace that calls the API.

 
UsingSystem. runtime. interopservices;

Then we can call

 
[Dllimport("USER32")]

Then we have done our work with preparation. We can start the road below. We need a timer to get our mouse position all the time, then, of course we need a coordinate to display our data.

TimerTimer;PointP =NewPoint(1, 1 );

Now we have the two global variables above. We can start our overall performance building, hahaha.

 
[Dllimport("USER32")]Public extern static boolGetcursorpos (RefPointCpoint );

Remember to use ref. Otherwise, a problem may occur. The next step is much easier.

Private voidForm1_load (ObjectSender,EventargsE) {timer =NewTimer(); Timer. interval = 1; timer. Tick + =NewEventhandler(Timer_tick); timer. Enabled =True;}VoidTimer_tick (ObjectSender,EventargsE) {getcursorpos (ref P); lblx. Text = p. x. tostring (); lbly. Text = P. Y. tostring ();}

OK. Now, let's take a look. The above getcursorpos is actually a function that we need to get the mouse coordinates.

Now let's define the range of mouse movement.

Clipcursor and getwindowrect are required to restrict mouse movement.

First, we first try to restrict the movement of the mouse and place the mouse in a form.

[Dllimport("USER32")]Public extern static intClipcursor (RefRectR );[Dllimport("USER32")]Public extern static intGetwindowrect (IntH,RefRectR );

All right, both functions are available. Next let's continue with the rect above.

Is a structure type. I declare the rect

Public structRect{Public intLeft;Public intTop;Public intRight;Public intBottom ;}

Then you can use your own methods to facilitate operations. Of course, the subsequent steps are to get the form handle.

 
Public voidLock (FormObjectform ){RectFormrect =NewRect(); Getwindowrect (objectform. Handle. toint32 (),RefFormrect); clipcursor (RefFormrect );}

Objectform. Handle. toint32 () to get the handle. I will not elaborate on the specific usage of the handle here. We may encounter it in future instances. We will be able to explain it slowly.

Lock (This);

Okay, that's done. Of course, it's much easier to restore the mouse's moving range. You just need to restore the coordinates.

 
Private voidBtnunlock_click (ObjectSender,EventargsE ){RectUr =NewRect(); Ur. Left = 0; Ur. Top = 0; Ur. Bottom =Screen. Primaryscreen. workingarea. Bottom; Ur. Right =Screen. Primaryscreen. workingarea. Right; clipcursor (RefUR );}

Let's continue. Next we will set the mouse position and use the buttons to control the mouse display position.

There is no doubt that we still find the function to locate the mouse.

[Dllimport("USER32")]Public extern static voidSetcursorpos (IntX,IntY); the rest is much simpler.
 
Private voidBtnmove_click (ObjectSender,EventargsE) {setcursorpos (Convert. Toint32 (txtx. Text ),Convert. Toint32 (txty. Text ));}

Next I will operate the left and right mouse buttons to show and hide the mouse and set the time interval for double-clicking the mouse.

Generally, you can use the API functions swapmousebutton, showcursor, setdoubleclicktime, and getdoubleclicktime to set the mouse.

The following figure shows how to select an event with the mouse clicked. We need an enumeration about the mouse.

[Flags]EnumMouseeventflag:Uint{Move = 0x0001, leftdown = 0x0002, leftup = 0x0004, rightdown = 0x0008, rightup = 0x0010, middledown = 0x0020, middleup = 0x040, xdown = 0x0080, xup = 0x0100, wheel = 0x0800, virtualdesk = 0X4000, absolute = 0x8000}

What is the meaning of each enumeration value? Let's take a look.

Const int move = 0x0001; move the mouse
Const int leftdown = 0x0002; simulate left mouse button Press
Const int leftup = 0x0004; simulate left mouse button lifting
Const int rightdown = 0x0008; simulate right-clicking
Const int rightup = 0x0010; simulate right-clicking and lifting
Const int middledown = 0x0020; simulate the mouse key and press
Const int middleup = 0x0040; simulate middle mouse button lifting
Const int absolute = 0x8000; indicates whether absolute coordinates are used

[ Dllimport ( "User32.dll" )] Public static extern void Mouse_event ( Mouseeventflag Flags, Int DX, Int Dy, Uint Data, Uintptr Extrainfo ); Private void Btno_click ( Object Sender, Eventargs E) {setcursorpos ( Convert . Toint32 (txtx. Text ), Convert . Toint32 (txty. Text); mouse_event (Mouseeventflag . Leftdown, 0, 0, 0, Uintptr . Zero); mouse_event ( Mouseeventflag . Leftdown, 0, 0, 0, Uintptr . Zero );}

Well, let's take a look at it. I 'd like to explain it to you here. Later I saw nothing. You can go to msdn to find relevant information.

Http://msdn.microsoft.com/en-us/library/ms646260%28v=VS.85%29.aspx

Http://msdn.microsoft.com/en-us/library/ms646260 (V = vs.85). aspx

The two links have their descriptions.

Of course, for the sake of simplicityCodeYou can write

Mouse_event (Mouseeventflag. Leftdown, 0, 0, 0, 0); Other code will not be explained.

Two consecutive left-click events constitute a double-click event:

 
Mouse_event (Mouseeventflag. Leftdown |Mouseeventflag. Leftup, 0, 0, 0,Uintptr. Zero); mouse_event (Mouseeventflag. Leftdown |Mouseeventflag. Leftup, 0, 0, 0,Uintptr. Zero );

Now let's talk about the mouse operation.

[Dllimport("USER32")]Public static extern intShowcursor (BoolBshow );Private voidBtnhide_click (ObjectSender,EventargsE) {showcursor (False);}Private voidBtnshow_click (ObjectSender,EventargsE) {showcursor (True);}
 
 
 

Haha, you will not try anything else. Please try it yourself. If any problem is found, please call kingdz. Thank you!

author: kingdz
Source: http://www.cnblogs.com/hihell/
about the author: I like programming, good at ASP. net website development, like MVC, Silverlight development, like communication, like innovation
My QQ: 860866679
MSN: wangdezhen @ Li Ve.com is looking forward to the progress of those with common aspirations.
the copyright of this article is shared by the author and the blog site. You are welcome to reprint it. However, you must retain this statement without the consent of the author, and provide the original connection clearly on the Article page, if you have any questions, contact me through kingdz's email
. Thank you very much.

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.