C # examples of QQ adsorption function

Source: Internet
Author: User

I found a lot of information on the Internet and found it was not easy to use. I tried to pull it for half a day. I hope it will help you.


Step 1: Pull the time Timer from the components in the toolbox to the winform to implement this function. The following figure shows the attributes of the Timer component, I put the control on the form and it has not been modified. For details, see

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/195423MT-0.jpg "title =" Controller. jpg "/>

Step 2: Add the following code to the loading time of the form. The code is relatively simple and can be understood at a glance. I will not explain it here.

// Form loading event private void Communicate_Load (object sender, EventArgs e) {// C # imitate QQ adsorption function Timer StopRectTimer = new Timer (); StopRectTimer. tick + = new EventHandler (timereffectick); StopRectTimer. interval = 100; StopRectTimer. enabled = true ;}




Step 3: trigger a time event LocationChanged when the location Value of the form occurs in the table.) The LocationChanged event calls a method. The Code is as follows:

// Trigger the event private void Communicate_LocationChanged (object sender, EventArgs e) {this. mStopAnthor ();} private void mStopAnthor () {if (this. top <= 0) {StopAanhor = AnchorStyles. top;} else if (this. left <= 0) {StopAanhor = AnchorStyles. left;} else if (this. left> = Screen. primaryScreen. bounds. width-this. width) {StopAanhor = AnchorStyles. right;} else {StopAanhor = AnchorStyles. none ;}}


Step 4: This is the last step and the most important step. The Tick event in the time component timer has the following code: Add a piece of code before the event. The Code is as follows:

Internal AnchorStyles StopAanhor = AnchorStyles. none; // This code must be added. // This method is triggered whenever this time component passes through the specified event interval. private void timer1_Tick (object sender, EventArgs e) {if (this. bounds. contains (Cursor. position) {switch (this. stopAanhor) {case AnchorStyles. top: this. location = new Point (this. location. x, 0); break; case AnchorStyles. left: this. location = new Point (0, this. location. y); break; case AnchorStyles. right: this. location = new Point (Screen. primaryScreen. bounds. width-this. width, this. location. y); break ;}} else {switch (this. stopAanhor) {case AnchorStyles. top: this. location = new Point (this. location. x, (this. height-2) * (-1); break; case AnchorStyles. left: this. location = new Point (-1) * (this. width-2), this. location. y); break; case AnchorStyles. right: this. location = new Point (Screen. primaryScreen. bounds. width-2, this. location. y); break ;}}}



In this way, the QQ adsorption function is implemented. I hope this example will help you.










This article is from the "Xin Feier _ Mao" blog. For more information, contact the author!

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.