When using the Form Designer of Vs, we can find that the control can be dragged and the size can be adjusted. How can I use the above functions in my own program? The following method is worth learning!
Using system; <br/> using system. windows. forms; <br/> using system. drawing; <br/> namespace controlsizechangeex <br/> {<br/> /// <summary> <br/> // This class implements sizing and moving functions for <br/> /// runtime editing of graphic controls <br/> /// </Summary> <br/> public class pickbox <br/>{< br/> ///// //////////////////////////////////////// /// // <br/> // Private Constants And variables <br/> ////////////////////////////////// /// // <br/> private const int box_size = 8; <br/> private color box_color = color. white; <br/> private containercontrol m_container; <br/> private control m_control; <br/> private label [] LBL = new label [8]; <br/> private int startl; <br/> private int startt; <br/> private int startw; <br/> private int starth; <br/> Private int startx; <br/> private int starty; <br/> private bool dragging; <br/> private cursor [] arrarrow = new cursor [] {cursors. sizenwse, cursors. sizens, <br/> cursors. sizenesw, cursors. sizewe, cursors. sizenwse, cursors. sizens, <br/> cursors. sizenesw, cursors. sizewe }; <br/> private cursor oldcursor; <br/> private const int min_size = 20; <br/> // constructor creates 8 sizing handles & Wires mouse events <br/> // to each that implement sizing functions <br/> // <br/> Public pickbox () <br/> {<br/> for (INT I = 0; I <8; I ++) <br/>{< br/> LBL [I] = new label (); <br/> LBL [I]. tabindex = I; <br/> LBL [I]. flatstyle = 0; <br/> LBL [I]. borderstyle = borderstyle. fixedsingle; <br/> LBL [I]. backcolor = box_color; <br/> LBL [I]. cursor = arrarrow [I]; <br/> LBL [I]. TEXT = ""; <br/> LBL [I]. bringto Front (); <br/> LBL [I]. mousedown + = new mouseeventhandler (this. lbl_mousedown); <br/> LBL [I]. mousemove + = new mouseeventhandler (this. lbl_mousemove); <br/> LBL [I]. mouseup + = new mouseeventhandler (this. lbl_mouseup ); <br/>}< br/> ////////////////////////// //////////////////////////////////////// <br/> // public methods <br/> //////////////////////////// /// // <br />/< Br/> // wires a click event handler to the passed Control <br/> // that attaches a pick box to the control when it is clicked <br/> // <br/> Public void wirecontrol (control CTL) <br/>{< br/> CTL. click + = new eventhandler (this. selectcontrol ); <br/>}< br/> /////////////////////////////// /// // <br/> // private methods <br/> ////////////////////////////////// //// /// // <Br/> // attaches A pick box to the sender Control <br/> // <br/> private void selectcontrol (Object sender, eventargs e) <br/>{< br/> If (m_control is control) <br/>{< br/> m_control.cursor = oldcursor; <br/> // remove event any pre-existing Event Handlers appended by this class <br/> m_control.mousedown-= new mouseeventhandler (this. ctl_mousedown); <br/> m_co Ntrol. mousemove-= new mouseeventhandler (this. ctl_mousemove); <br/> m_control.mouseup-= new mouseeventhandler (this. ctl_mouseup); <br/> m_control = NULL; <br/>}< br/> m_control = (Control) sender; <br/> // Add Event Handlers for moving the selected control around <br/> m_control.mousedown + = new mouseeventhandler (this. ctl_mousedown); <br/> m_control.mousemove + = new mouseeventhandler (this. ctl_mouse Move); <br/> m_control.mouseup + = new mouseeventhandler (this. ctl_mouseup); <br/> // Add sizing handles to control's container (Form or picturebox) <br/> for (INT I = 0; I <8; I ++) <br/>{< br/> m_control.parent.controls.add (LBL [I]); <br/> LBL [I]. bringtofront (); <br/>}< br/> // position sizing handles around Control <br/> movehandles (); <br/> // display sizing handles <br/> showhandles (); <br/> oldc Ursor = m_control.cursor; <br/> m_control.cursor = cursors. sizeall; <br/>}< br/> Public void remove () <br/>{< br/> hidehandles (); <br/> m_control.cursor = oldcursor; <br/>}< br/> private void showhandles () <br/>{< br/> If (m_control! = NULL) <br/>{< br/> for (INT I = 0; I <8; I ++) <br/> {<br/> LBL [I]. visible = true; <br/>}< br/> private void hidehandles () <br/> {<br/> for (INT I = 0; I <8; I ++) <br/> {<br/> LBL [I]. visible = false; <br/>}< br/> private void movehandles () <br/>{< br/> int SX = m_control.left-box_size; <br/> int Sy = m_control.top-box_size; <br/> int Sw = m_control.width + box _ Size; <br/> int SH = m_control.height + box_size; <br/> int HB = box_size/2; <br/> int [] arrposx = new int [] {SX + HB, SX + Sw/2, SX + Sw-Hb, SX + Sw-Hb, <br/> SX + Sw-Hb, SX + Sw/2, SX + HB, SX + HB }; <br/> int [] arrposy = new int [] {SY + HB, SY + sh/2, SY + sh-Hb, <br/> SY + sh-Hb, SY + sh-Hb, SY + sh/2}; <br/> for (INT I = 0; I <8; I ++) <br/> LBL [I]. setbounds (arrposx [I], arrpos Y [I], box_size, box_size ); <br/>}< br/> /////////////////////////////// /// // <br/> // mouse events that implement sizing of the picked Control <br/> /////////////////////////// /// // <br />/< br/> // store control position and size when mouse button pushed over <br/> // any sizing handle <br/> // <br/> private void lbl_mousedown (Object Sender, mouseeventargs e) <br/>{< br/> dragging = true; <br/> startl = m_control.left; <br/> startt = m_control.top; <br/> startw = m_control.width; <br/> starth = m_control.height; <br/> hidehandles (); <br/>}< br/> // <br/> // size the picked control in accordance with Sizing handle being dragged <br/> // 0 1 2 <br/> // 7 3 <br/> // 6 5 4 <br/> // <br/> private void lbl_mousemove (Object sender, Mouseeventargs e) <br/>{< br/> int L = m_control.left; <br/> int W = m_control.width; <br/> int T = m_control.top; <br/> int H = m_control.height; <br/> If (dragging) <br/> {<br/> switch (Label) sender ). tabindex) <br/>{< br/> case 0: // dragging top-left Sizing Box <br/> L = startl + E. x <startl + startw-min_size? Startl + E. X: startl + startw-min_size; <br/> T = startt + E. Y <startt + starth-min_size? Startt + E. y: startt + starth-min_size; <br/> W = startl + startw-m_control.left; <br/> H = startt + starth-m_control.top; <br/> break; <br/> case 1: // dragging top-center Sizing Box <br/> T = startt + E. Y <startt + starth-min_size? Startt + E. y: startt + starth-min_size; <br/> H = startt + starth-m_control.top; <br/> break; <br/> case 2: // dragging top-Right Sizing Box <br/> W = startw + E. x> min_size? Startw + E. X: min_size; <br/> T = startt + E. Y <startt + starth-min_size? Startt + E. y: startt + starth-min_size; <br/> H = startt + starth-m_control.top; <br/> break; <br/> case 3: // dragging right-middle Sizing Box <br/> W = startw + E. x> min_size? Startw + E. x: min_size; <br/> break; <br/> case 4: // dragging right-bottom Sizing Box <br/> W = startw + E. x> min_size? Startw + E. X: min_size; <br/> H = starth + E. Y> min_size? Starth + E. y: min_size; <br/> break; <br/> case 5: // dragging center-bottom Sizing Box <br/> H = starth + E. y> min_size? Starth + E. y: min_size; <br/> break; <br/> case 6: // dragging left-bottom Sizing Box <br/> L = startl + E. x <startl + startw-min_size? Startl + E. X: startl + startw-min_size; <br/> W = startl + startw-m_control.left; <br/> H = starth + E. Y> min_size? Starth + E. y: min_size; <br/> break; <br/> case 7: // dragging left-middle Sizing Box <br/> L = startl + E. x <startl + startw-min_size? Startl + E. x: startl + startw-min_size; <br/> W = startl + startw-m_control.left; <br/> break; <br/>}< br/> L = (L <0 )? 0: L; <br/> T = (T <0 )? 0: T; <br/> m_control.setbounds (L, t, W, H ); <br/>}< br/> // <br/> // display sizing handles around picked control once sizing has completed <br/> // <br /> private void lbl_mouseup (Object sender, mouseeventargs e) <br/>{< br/> dragging = false; <br/> movehandles (); <br/> showhandles (); <br/>}< br/> /////////////////////////////// /// // <br/> // mouse events That move the picked control around the form <br/> //////////////////////////// /// // <br/> // <br/> // get mouse pointer starting position on mouse down and hide sizing handles <br/> // <br/> private void ctl_mousedown (Object sender, mouseeventargs e) <br/>{< br/> dragging = true; <br/> startx = E. x; <br/> starty = E. y; <br/> hidehandles (); <br/>}< br/> // <br/>/ /Reposition the dragged Control <br/> // <br/> private void ctl_mousemove (Object sender, mouseeventargs e) <br/>{< br/> If (dragging) <br/>{< br/> int L = m_control.left + E. x-startx; <br/> int T = m_control.top + E. y-starty; <br/> int W = m_control.width; <br/> int H = m_control.height; <br/> L = (L <0 )? 0: (L + W> m_control.parent.clientrectangle.width )? <Br/> m_control.parent.clientrectangle.width-W: l); <br/> T = (T <0 )? 0: (t + H> m_control.parent.clientrectangle.height )? <Br/> m_control.parent.clientrectangle.height-H: T); <br/> m_control.left = L; <br/> m_control.top = T; <br/>}< br/> // <br/> // display sizing handles around picked control once dragging has completed <br/> // <br /> private void ctl_mouseup (Object sender, mouseeventargs e) <br/>{< br/> dragging = false; <br/> movehandles (); <br/> showhandles (); <br/>}< br/>
Create a pickbox object and call the wirecontrol of this object (the control you want to change). The method is fine.