Personal code repository movable + sliding + rebound button "C # related code"

Source: Internet
Author: User

To achieve the slide function, you need the help of the timer object. To this end, you need to add the code in [Form class constructor:

// You must explicitly declare the timer control for slide to reduce the latency of each call. Mytimer = new timer (); mytimer. interval = inttimerinterval; mytimer. Tick + = new system. eventhandler (this. mytimer_tick );

The following code is bound to the corresponding event of the button you want to add this function:

# Region movable + slip + bounce button "related code" # region variable declaration area // <summary> // you can specify which key you can click to move the button. // </Summary> mousebuttons mousebutton = mousebuttons. right; // pass the current button. Button btntemp = NULL; // The timer used for calculation of sliding. Timer mytimer = NULL; // The trigger interval (in milliseconds) of the timer int inttimerinterval = 20; // The number of frames sliding by the button. The larger the value, the smoother the transition. Static int intnum = 30; // The Temporary Variable int inttimer = intnum for the number of frames; // the time before and after moving. Datetime datbegin, datend; // point poibegin and poiend coordinates before and after movement; // acceleration decreases by a factor of 0.12 // acceleration of resistance; double intb = 0.12; // increase of sliding speed, the larger the sliding speed, the faster the sliding speed. // The acceleration of the last velocity double XV; double YV; // X and Y. Double XA; double ya; // record the position of the current mouse relative to the button. Point mouse_offset = new point (); bool bolmove = false; // whether to allow rebound bool bolreturn = true; # endregion # private void button_mousedown (Object sender, mouseeventargs E) {If (E. button = mousebutton) {btntemp = (button) sender; // bolmove = true; // assign the coordinates of the current mouse relative to the upper left corner of the form to mouse_offset = E. location; // write down the system time. So that the calculation speed is datbegin = datetime. now; poibegin = btntemp. location ;}} private void button_mouseup (Object sender, mouseeventargs e) {If (bolmove & E. button = mousebutton) {// bolmove = false; // write down the coordinates poiend = (button) sender) after the end ). location; datend = datetime. now; # region Acceleration Formula // start to calculate the acceleration of X and Y. // S = vot + 1/2at * t // end speed v = vot + at // when Vo = 0, A = 2 S/(T * t ); V = 2 s/t # endregion int xs = poiend. x-poibegin. x; int ys = poiend. y-poibegin. y; Double T = datend. subtract (datbegin ). totalmilliseconds; XA = (2 * XS)/(T * t); YA = (2 * ys)/(T * t); xv = (2 * XS)/T; YV = (2 * ys)/T; // reset to the default number of frames. Inttimer = intnum; // start the timer control and start to slide slowly... Mytimer. enabled = true;} private void button_mousemove (Object sender, mouseeventargs e) {If (E. button = mousebutton) {bolmove = true; // convert the relative screen coordinates to the coordinates of the relative work area. Int left = pointtoclient (control. mouseposition ). x-mouse_offset.x; int Top = pointtoclient (control. mouseposition ). y-mouse_offset.y; // the left and right sides may cross the border if (left <0) Left = 0; If (left> This. width) Left = This. width-(button) sender ). width; // the upper and lower bounds. If (top <0) Top = 0; If (top> This. height) Top = This. height-(button) sender ). height; (button) sender ). left = left; (button) sender ). top = Top ;}} private void mytimer_tick (Object sender, eventargs e) {// as long as the slide mode is enabled, the mouse (manual) is stopped. If (bolmove) bolmove = false; If (inttimer = 0 | XA = 0 | YA = 0) {mytimer. enabled = false;} else {inttimer --; If (xa <0) btntemp. left = btntemp. left-(INT) (xa x XV * 1000); else btntemp. left = btntemp. left + (INT) (xa x XV * 1000); If (ya <0) btntemp. top = btntemp. top-(INT) (ya * YV * 1000); else btntemp. top = btntemp. top + (INT) (ya * YV * 1000); XA = XA * (1-intb); YA = ya * (1-intb); // the left and right sides may cross the border, after the symbol is changed, it will automatically "rebound" after hitting the wall; If (btntemp. left <0) {btntemp. left = 0; If (bolreturn) {XA =-Xa; xv =-XV ;}} if (btntemp. left> This. width-btntemp. width) {btntemp. left = This. width-btntemp. width; If (bolreturn) {XA =-Xa; xv =-XV ;}// the upper and lower bounds of the IF (btntemp. top <0) {btntemp. top = 0; If (bolreturn) {YA =-Ya; YV =-YV ;}} if (btntemp. top> This. height-btntemp. height) {btntemp. top = This. height-btntemp. height; If (bolreturn) {YA =-Ya; YV =-YV ;}}# endregion # endregion

[By: asion Tang] blog garden label: C #, personal code library

Related Article

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.