Controls in the winform mobile window, panel

Source: Internet
Author: User

From: http://www.codeproject.com/Articles/31840/Move-controls-on-a-form-at-runtime

Thank the author a lot.

This article is modified according to a program in codeproject.

You can achieve this by dragging the mouse in the window. Really? Yes

Code:

A form. One class:

Form:

Effect:

Code:

Public partial class frm_movecontrol: FORM {private cls_movecontrol movecontrol; Public frm_movecontrol () {initializecomponent (); movecontrol = new cls_movecontrol (this); // This indicates a window, or a panel or other container movecontrol. applymove (button2); movecontrol. applymove (button3, cls_movecontrol.movedirection.vertical); movecontrol. applymove (button1, Panel1, cls_movecontrol.movedirection.any );}}

Next is the class for mobile controls:

Using system; using system. collections. generic; using system. drawing; using system. text; using system. windows. forms; namespace cachedemo1 {class cls_movecontrol {public Enum movedirection {Any, horizontal, vertical} private int detail = 200; private int int_frmheight = 200; Public int int_frmwidth {get {return detail ;} set {int_frmwidth = value ;}} public int int_frmheight {get {return int_frmh Eight;} set {int_frmheight = value;} public cls_movecontrol (INT width, int height) {this.int _ frmwidth = width; this.int _ frmheight = height ;} // if only the window is passed in, you can use form. Of course, you can also use the control class, because form is also sent to the control class public cls_movecontrol (control FRM) {this.int _ frmwidth = FRM. clientrectangle. width; this.int _ frmheight = FRM. clientrectangle. height; frm. resize + = delegate (Object sender, eventargs e) {this.int _ frmwidth = FRM. clientrectangle. width; this.int _ frmheight = FRM. clientrectangle. height;}/** frm. clientrectangle. width; the reason why clientrectangle is used here * Because clientrectangle can obtain the available area of the window * The height of the title bar of the window can be removed. * The advantage of control is that form can be passed in. You can also wear container controls, such as panel */Public void applymove (Control) {applymove (control, movedirection. any);} public void applymove (Control, movedirection) {applymove (control, control, movedirection);} public void applymove (control, control container, movedirection) {bool isdrag = false; point dragstartpoint = point. empty; control. mousedown + = delegate (Object sender, Mouseeventargs e) {isdrag = true; dragstartpoint = new point (e. x, E. y); control. cursor = cursors. sizeall; control. capture = true;}; control. mouseup + = delegate (Object sender, mouseeventargs e) {isdrag = false; control. cursor = cursors. default; control. capture = false;}; control. mousemove + = delegate (Object sender, mouseeventargs e) {If (isdrag) // true only when the mouse is pressed. Once the mouse is opened, the value is false {// The value can only be moved horizontally. Vertical movement can only be vertical movement. // Any direction can be moved if (movedirection! = Movedirection. vertical) // horizontal and any can enter {int left = container. left + E. x-dragstartpoint. x; // container. left = math. max (0, left); // do not exceed the left boundary container. left = getmiddlevalue (left, container. width, this.int _ frmwidth);} If (movedirection! = Movedirection. horizontal) {int Top = container. top + E. y-dragstartpoint. y; // container. top = math. max (0, top); container. top = getmiddlevalue (top, container. height, this.int _ frmheight );}}};} /// <summary> // do not control the dragged control beyond the window. // </Summary> /// <Param name = "Val"> </param> // /<Param name = "containerwoh"> </param> // <Param name = "frmwidth"> </param> // <returns> </returns> private int getmiddlevalue (INT Val, int containerwoh, int frmwoh) {int min = 0; int max = frmwoh-containerwoh; If (Val <min) {val = min;} If (Val> MAX) {val = max;} console. writeline (VAL); Return Val ;}}}

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.