When using the form designer with VS, we can see that the controls are draggable and can be resized. How to use the above function in your own program? The following methods are worth learning!
Using System; Using System.Windows.Forms; Using System.Drawing; namespace Controlsizechangeex {//<summary>//This class implements sizing and moving functions for runtime editing of graphic controls//</summary> public class Pickbox {////////// PRIVATE CONSTANTS and VARIABLES//////////// Private Const int box_size = 8; Private Color box_color = Color.White; Private ContainerControl M_container; Private Control M_control; Private label[] LbL = new Label[8]; private int Startl; private int Startt; private int startw; private int Starth; private int startx; private int starty; private bool dragging; Private cursor[] Arrarrow = new cursor[] {cursors.sizenwse, cursors.sizens,CURSORS.SIZENESW, Cursors.sizewe, Cursors.sizenwse, Cursors.sizens, CURSORS.SIZENESW, Cursors.SizeWE}; Private Cursor oldcursor; Private Const int min_size = 20; Constructor creates 8 sizing handles & Wires mouse events//to all that implement sizing fun Ctions//Public Pickbox () {for (int i = 0; i < 8; i++) { Lbl[i] = new Label (); Lbl[i]. TabIndex = i; Lbl[i]. FlatStyle = 0; Lbl[i]. BorderStyle = BorderStyle.FixedSingle; Lbl[i]. BackColor = Box_color; Lbl[i]. Cursor = Arrarrow[i]; Lbl[i]. Text = ""; Lbl[i]. BringToFront (); Lbl[i]. MouseDown + = new MouseEventHandler (This.lbl_mousedown); Lbl[i]. MouseMove + = new MouseEventHandler (this.lbl_mousemove); Lbl[i]. MouseUp + = new MouseEventHandler (THIS.LBl_mouseup); }}////////////////////////////////////////////////////////////////////Public METHODS //Wires a Click event handler To the passed control//So attaches a pick box to the control when it's clicked//public V OID Wirecontrol (Control ctl) {ctl. Click + = new EventHandler (this. Selectcontrol); }///////////////////////////////////////////////////////////////////PRIVATE METHODS//////// //Attaches a pick box to the sender contro L//private void Selectcontrol (object sender, EventArgs e) {if (M_control is Con Trol) {M_control. Cursor = Oldcursor; Remove event any pre-existing event handleRS appended by this class M_control. MouseDown-= new MouseEventHandler (This.ctl_mousedown); M_control. MouseMove-= new MouseEventHandler (this.ctl_mousemove); M_control. MouseUp-= new MouseEventHandler (this.ctl_mouseup); M_control = null; } M_control = (control) sender; ADD event handlers for moving the selected control around M_control. MouseDown + = new MouseEventHandler (This.ctl_mousedown); M_control. MouseMove + = new MouseEventHandler (this.ctl_mousemove); M_control. MouseUp + = new MouseEventHandler (this.ctl_mouseup); ADD sizing handles to Control ' s <a href= "Http://lib.csdn.net/base/docker" class= ' Replace_word ' title= "Docker Knowledge Base" target= ' _blank ' style= ' color: #df3434; Font-weight:bold; ' >Container</a> (Form or PictureBox) for (int i = 0; i < 8; i++) {m _control. Parent.controLs. ADD (Lbl[i]); Lbl[i]. BringToFront (); }//position sizing handles around Control movehandles (); Display sizing handles showhandles (); Oldcursor = M_control. Cursor; M_control. Cursor = Cursors.sizeall; } public void Remove () {hidehandles (); M_control. Cursor = Oldcursor; } private void Showhandles () {if (M_control! = null) {for ( int i = 0; I < 8; i++) {Lbl[i]. Visible = true; }}} private void Hidehandles () {for (int i = 0; i < 8; i++) {Lbl[i]. Visible = false; }} private void Movehandles () {int SX = M_control. Left-box_size; int SY = M_control.Top-box_size; int SW = M_control. Width + box_size; int SH = M_control. Height + box_size; int HB = BOX_SIZE/2; int[] Arrposx = new int[] {SX+HB, SX + SW/2, SX + SW-HB, SX + SW-HB, SX + SW-HB, SX + SW/2, SX+HB, SX+HB}; int[] Arrposy = new int[] {SY+HB, SY+HB, SY+HB, sy + SH/2, sy + SH-HB, sy + SH-HB, sy + SH-HB, sy + SH/2}; for (int i = 0; i < 8; i++) Lbl[i]. SetBounds (Arrposx[i], arrposy[i], box_size, box_size); }///////////////////////////////////////////////////////////////////MOUSE EVENTS that IMPLEMENT Sizin G of the PICKED CONTROL///////////////////////////////////////////////////////////////////// Store control position and size when mouse button pushed on//any sizing handle//private void Lbl_mousedown (object sender, MouseEventArgs e) {dragging = true; Startl = M_control. Left; Startt = M_control. Top; STARTW = M_control. Width; Starth = M_control. Height; Hidehandles (); }///Size the picked control in accordance with sizing handle being dragged//0 1 2 7 3//6 5 4//private void Lbl_mousemove (object sender, MouseEventArgs E ) {int L = M_control. Left; int w = M_control. Width; int t = M_control. Top; int h = M_control. Height; if (dragging) {switch ((Label) sender). TabIndex) {case 0://dragging top-left sizing box L = STA RTL + e.x < Startl + startw-min_size? Startl + E.x:startl + startw-min_size; t = Startt + e.y < Startt + starth-min_size? Startt + E.y:startt + starth-mIn_size; W = Startl + Startw-m_control. Left; H = Startt + Starth-m_control. Top; Break Case 1://dragging top-center sizing Box T = Startt + e.y < Startt + starth-min_size? Startt + E.y:startt + starth-min_size; H = Startt + Starth-m_control. Top; Break Case 2://dragging top-right sizing Box W = startw + e.x > min_size? STARTW + e.x:min_size; t = Startt + e.y < Startt + starth-min_size? Startt + E.y:startt + starth-min_size; H = Startt + Starth-m_control. Top; Break Case 3://dragging right-middle sizing Box W = startw + e.x > min_size? STARTW + e.x:min_size; Break Case 4://dragging right-bottom sizing Box W = startw + e.x > min_size? STARTW + e.x:min_size; H = Starth + e.y > min_size? Starth + e.y:min_size; Break Case 5://dragging center-bottom sizing box h = Starth + e.y > min_size? Starth + e.y:min_size; Break Case 6://dragging left-bottom sizing box L = Startl + e.x < Startl + startw-min_size? Startl + E.x:startl + startw-min_size; W = Startl + Startw-m_control. Left; H = Starth + e.y > min_size? Starth + e.y:min_size; Break Case 7://dragging left-middle sizing box L = Startl + e.x < Startl + startw-min_size? Startl + E.x:startl + startw-min_size; W = STARTL + Startw-m_control. Left; Break } L = (L < 0)? 0:l; t = (T < 0)? 0:t; M_control. SetBounds (L, T, W, h); }}////Display sizing handles around picked control once sizing has completed// private void Lbl_mouseup (object sender, MouseEventArgs e) {dragging = false; Movehandles (); Showhandles (); }///////////////////////////////////////////////////////////////////MOUSE EVENTS that MOVE the PICKED CONTROL AROUND the FORM//////////////////////////////////////////////////////////////////// /Get mouse pointer starting position on mouse down and hide sizing handles//private void Ctl_mousedow N (object sender, MouseEventArgs e) {dragging = true; StartX = e.x; Starty = e.y; Hidehandles (); }////reposition the dragged control//private void Ctl_mousemove (object sender, Mo Useeventargs e) {if (dragging) {int L = M_control. Left + E.x-startx; int t = M_control. Top + e.y-starty; int w = M_control. Width; int h = M_control. Height; L = (L < 0)? 0: ((l + w > M_control. Parent.ClientRectangle.Width)? M_control. PARENT.CLIENTRECTANGLE.WIDTH-W: L); t = (T < 0)? 0: ((t + H > M_control. Parent.ClientRectangle.Height)? M_control. Parent.clientrectangle.height-h: t); M_control. left = l; M_control. Top = t; }}////Display sizing handles around picked control once dragging has completed// private void Ctl_mouSeup (object sender, MouseEventArgs e) {dragging = false; Movehandles (); Showhandles (); } } }
Create a Pickbox object that calls the Wirecontrol of this object (the control you want to change);
How C # Custom size and change is large