本篇文章主要介紹了C#中WinForm控制項的拖動和縮放的實現代碼,C# WinForm控制項的拖動和縮放是個很有用的功能,有興趣的可以瞭解一下。
C# WinForm控制項的拖動和縮放是個很有用的功能。實現起來其實很簡單的,主要是設計控制項的MouseDown、MouseLeave、MouseMove事件,下面的幾個步驟將逐步實現C# WinForm控制項的拖動和縮放的功能。
1、定義一個枚舉類型,描述游標狀態
private enum EnumMousePointPosition { MouseSizeNone = 0, //'無 MouseSizeRight = 1, //'展開右邊框 MouseSizeLeft = 2, //'展開左邊框 MouseSizeBottom = 3, //'展開下邊框 MouseSizeTop = 4, //'展開上邊框 MouseSizeTopLeft = 5, //'展開左上方 MouseSizeTopRight = 6, //'展開右上方 MouseSizeBottomLeft = 7, //'展開左下角 MouseSizeBottomRight= 8, //'展開右下角 MouseDrag = 9 // '滑鼠拖動 }
2、定義幾個變數
const int Band = 5; const int MinWidth=10; const int MinHeight=10; private EnumMousePointPosition m_MousePointPosition; private Point p,p1;
3、定義自己的MyMouseDown事件
private void MyMouseDown(object sender,System.Windows.Forms.MouseEventArgs e) { p.X=e.X; p.Y=e.Y; p1.X=e.X; p1.Y=e.Y; }
4、定義自己的MyMouseLeave事件
private void MyMouseLeave(object sender, System.EventArgs e) { m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; this.Cursor=Cursors.Arrow; }
5、設計一個函數,確定游標在控制項不同位置的樣式
private EnumMousePointPosition MousePointPosition(Size size,System.Windows.Forms.MouseEventArgs e) { if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)) { if (e.X < Band) { if (e.Y < Band) {return EnumMousePointPosition.MouseSizeTopLeft;} else { if (e.Y > -1 * Band + size.Height) {return EnumMousePointPosition.MouseSizeBottomLeft;} else {return EnumMousePointPosition.MouseSizeLeft;} } } else { if (e.X > -1 * Band + size.Width) { if (e.Y < Band) {return EnumMousePointPosition.MouseSizeTopRight;} else { if (e.Y > -1 * Band + size.Height) {return EnumMousePointPosition.MouseSizeBottomRight;} else {return EnumMousePointPosition.MouseSizeRight;} } } else { if (e.Y < Band) {return EnumMousePointPosition.MouseSizeTop;} else { if (e.Y > -1 * Band + size.Height) {return EnumMousePointPosition.MouseSizeBottom;} else {return EnumMousePointPosition.MouseDrag;} } } } } else {return EnumMousePointPosition.MouseSizeNone;} }
6、定義自己的MyMouseMove事件,在這個事件裡,會使用上面設計的函數
private void MyMouseMove(object sender,System.Windows.Forms.MouseEventArgs e) { Control lCtrl =(sender as Control); if (e.Button==MouseButtons.Left) { switch (m_MousePointPosition) { case EnumMousePointPosition.MouseDrag: lCtrl.Left =lCtrl.Left+ e.X - p.X; lCtrl.Top =lCtrl.Top+ e.Y - p.Y; break; case EnumMousePointPosition.MouseSizeBottom: lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X=e.X; p1.Y=e.Y; //'記錄游標拖動的當前點 break; case EnumMousePointPosition.MouseSizeBottomRight: lCtrl.Width = lCtrl.Width + e.X - p1.X; lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X=e.X; p1.Y=e.Y; //'記錄游標拖動的當前點 break; case EnumMousePointPosition.MouseSizeRight: lCtrl.Width = lCtrl.Width + e.X - p1.X; // lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X=e.X; p1.Y=e.Y; //'記錄游標拖動的當前點 break; case EnumMousePointPosition.MouseSizeTop: lCtrl.Top = lCtrl.Top + (e.Y - p.Y); lCtrl.Height = lCtrl.Height - (e.Y - p.Y); break; case EnumMousePointPosition.MouseSizeLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Width = lCtrl.Width - (e.X - p.X); break; case EnumMousePointPosition.MouseSizeBottomLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Width = lCtrl.Width - (e.X - p.X); lCtrl.Height = lCtrl.Height+ e.Y - p1.Y; p1.X=e.X; p1.Y=e.Y; //'記錄游標拖動的當前點 break; case EnumMousePointPosition.MouseSizeTopRight: lCtrl.Top = lCtrl.Top + (e.Y - p.Y); lCtrl.Width = lCtrl.Width + (e.X - p1.X); lCtrl.Height = lCtrl.Height - (e.Y - p.Y); p1.X=e.X; p1.Y=e.Y; //'記錄游標拖動的當前點 break; case EnumMousePointPosition.MouseSizeTopLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Top = lCtrl.Top + (e.Y - p.Y); lCtrl.Width = lCtrl.Width - (e.X - p.X); lCtrl.Height = lCtrl.Height - (e.Y - p.Y); break; default: break; } if (lCtrl.Width<MinWidth) lCtrl.Width=MinWidth; if (lCtrl.Height<MinHeight) lCtrl.Height=MinHeight; } else { m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判斷游標的位置狀態 switch (m_MousePointPosition) //'改變游標 { case EnumMousePointPosition.MouseSizeNone: this.Cursor = Cursors.Arrow; //'箭頭 break; case EnumMousePointPosition.MouseDrag: this.Cursor = Cursors.SizeAll; //'四方向 break; case EnumMousePointPosition.MouseSizeBottom: this.Cursor = Cursors.SizeNS; //'南北 break; case EnumMousePointPosition.MouseSizeTop: this.Cursor = Cursors.SizeNS; //'南北 break; case EnumMousePointPosition.MouseSizeLeft: this.Cursor = Cursors.SizeWE; //'東西 break; case EnumMousePointPosition.MouseSizeRight: this.Cursor = Cursors.SizeWE; //'東西 break; case EnumMousePointPosition.MouseSizeBottomLeft: this.Cursor = Cursors.SizeNESW; //'東北到南西 break; case EnumMousePointPosition.MouseSizeBottomRight: this.Cursor = Cursors.SizeNWSE; //'東南到西北 break; case EnumMousePointPosition.MouseSizeTopLeft: this.Cursor = Cursors.SizeNWSE; //'東南到西北 break; case EnumMousePointPosition.MouseSizeTopRight: this.Cursor = Cursors.SizeNESW; //'東北到南西 break; default: break; } } }
7、製作一個初始化過程,將介面panel1上的所有控制項都綁定MyMouseDown、MyMouseLeave、MyMouseMove事件,記得在Form初始化或者Form_Load時先執行它。
private void initProperty() { for(int i = 0; i < this.panel1.Controls.Count; i++) { this.panel1.Controls[i].MouseDown+= new System.Windows.Forms.MouseEventHandler(MyMouseDown); this.panel1.Controls[i].MouseLeave+= new System.EventHandler(MyMouseLeave); this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove); } }
8、ok,在運行之前你在panel1上放幾個控制項,執行程式,應該可以隨便移動、改變其大小了
C# WinForm控制項的拖動和縮放的實現就此完成。