C# 控制項的自訂拖動、改變大小方法

來源:互聯網
上載者:User

在用VS的表單設計器時,我們可以發現控制項都是可以拖動的,並且還可以調整大小。怎麼在自己的程式中可以使用上述功能呢? 下面的方法值得借鑒!

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 /> //<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].BringToFront();<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 /> //////////////////////////////////////////////////////////////////<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 /> //<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_control.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_MouseMove);<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 /> oldCursor = 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 /> }<br /> }<br /> private void HideHandles()<br /> {<br /> for (int i = 0; i < 8; i++)<br /> {<br /> lbl[i].Visible = false;<br /> }<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+hB, 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], arrPosY[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 /> //<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 /> //<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 /> }<br />}<br /> 

 

建立一個PickBox對象 ,  調用此對象的WireControl(你希望改變的控制項);方法就行了。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.