PickBoxTest示例(1)
using System; using System.Windows.Forms; using System.Drawing; namespace PickBoxTest { /// <summary> /// 这个类实现了窗体控件的 大小和移动函数 /// 用户运行时编辑 /// </summary> public class PickBox { ////////////////////////////////////////////////////////////////// // 私有常量和变量 ////////////////////////////////////////////////////////////////// private const int BOX_SIZE = 6; private Color BOX_COLOR = Color.FromArgb(0, 0, 255); //private Color BOX_COLOR = Color.Blue; //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; 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 void WireControl(Control ctl) { ctl.Click += new EventHandler(this.SelectControl); } ///////////////////////////////////////////////////////////////// //私有方法 ///////////////////////////////////////////////////////////////// // // 将pick box 附加到 相应的控件上 // private void SelectControl(object sender, EventArgs e) { //移除上一个控件的事件处理. if (m_control is Control) { m_control.Cursor = oldCursor; 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; //添加用于将选定控件移动的事件处理程序 m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown); m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove); m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp); //将大小调整手柄添加到控件容器中 for (int i = 0; i < 8; i++) { m_control.Parent.Controls.Add(lbl[i]); lbl[i].BringToFront(); } //位置控制周围的大小调整 MoveHandles(); //显示大小调整手柄 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); } 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(); } 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 上左脚的拖动 sizing box l = startl + 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 上中间的拖动 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 上右脚边 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 右中 sizing box w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; break; case 4: // Dragging 右下 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 下中 sizing box h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 6: // Dragging 左下 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 右中 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); } } private void lbl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); } private void ctl_MouseDown(object sender, MouseEventArgs e) { dragging = true; startx = e.X; starty = e.Y; HideHandles(); } private void ctl_MouseMove(object sender, MouseEventArgs 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; } } private void ctl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); } } }
namespace WinForm003 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; } private PickBox pb = new PickBox(); private void Form1_Load(object sender, EventArgs e) { this.Text = "WinForm003示例"; this.StartPosition = FormStartPosition.CenterScreen; foreach (Control c in this.Controls) { pb.WireControl(c); } } private void Form1_Click(object sender, EventArgs e) { pb.Remove(); } } }