winform 控件拖动和改变大小的例子
要实现界面上控件的可调整大小和位置可以用下面的代码实现,主要是对Panel控件进行了一点改造,应该也适用于其他容器控件。
欢迎大家到我的网站去下载实例。如果有什么不对的地方,请大家指正。
主要的思路是在容器内为所有的控件添加MouseDown,MouseLeave,MouseMove三个事件,在MouseLeave事件中保存对控件位置和大小的修改,保存到XML文件中,程序启动时会调用XML文件内的控件属性,以此来实现该功能。
下面是Panel类代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | public partial class PanelEx :Panel { /// <summary> /// 光标状态 /// </summary> private enum EnumMousePointPosition { MouseSizeNone = 0, //'无 MouseSizeRight = 1, //'拉伸右边框 MouseSizeLeft = 2, //'拉伸左边框 MouseSizeBottom = 3, //'拉伸下边框 MouseSizeTop = 4, //'拉伸上边框 MouseSizeTopLeft = 5, //'拉伸左上角 MouseSizeTopRight = 6, //'拉伸右上角 MouseSizeBottomLeft = 7, //'拉伸左下角 MouseSizeBottomRight = 8, //'拉伸右下角 MouseDrag = 9 // '鼠标拖动 } #region 属性 private static string xmlDocPath = "" ; private XmlDocument doc; private const int Band = 5; private const int MinWidth = 10; private const int MinHeight = 10; private EnumMousePointPosition m_MousePointPosition; private Point p, p1; #endregion public PanelEx() { InitializeComponent(); } #region 改变控件大小和移动位置用到的方法 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; } /// <summary> /// 鼠标离开事件需要改进 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MyMouseLeave( object sender, EventArgs e) { Control s = (Control)sender; SaveStyle(s); m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; this .Cursor = Cursors.Arrow; } private void SaveStyle(Control s) { XmlNodeList nodes = doc.GetElementsByTagName(s.Name); XmlElement xn; if (nodes.Count != 1) { xn = doc.CreateElement(s.Name); } else { xn = (XmlElement)doc.GetElementsByTagName(s.Name)[0]; } if (s.Top < 0 || s.Top > this .Height - s.Top) s.Top = 0; xn.SetAttribute( "Top" , s.Top.ToString()); if (s.Left < 0 || s.Left > this .Width - s.Left) s.Left = 0; xn.SetAttribute( "Left" , s.Left.ToString()); if (s.Width + s.Left > this .Width) s.Width = this .Width - s.Left; xn.SetAttribute( "Width" , s.Width.ToString()); if (s.Height + s.Top > this .Height) s.Height = this .Height - s.Top; xn.SetAttribute( "Height" , s.Height.ToString()); XmlNodeList xnl = doc.GetElementsByTagName( this .Name); XmlElement xnp; if (xnl.Count < 1) { xnp = doc.CreateElement( this .Name); } else { xnp = (XmlElement)xnl[0]; } xnp.AppendChild((XmlNode)xn); doc.DocumentElement.AppendChild((XmlNode)xnp); doc.Save(xmlDocPath); } 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; } } 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 ; } } } #endregion #region 初始化鼠标事件委托和控件大小和移动 private void initProperty() { for ( int i = 0; i < this .Controls.Count; i++) { this .Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown); this .Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave); this .Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove); } } private void initStyle() { Control s; for ( int i = 0; i < this .Controls.Count; i++) { s = this .Controls[i]; XmlNodeList nodes = doc.GetElementsByTagName(s.Name); if (nodes.Count == 1) { XmlAttributeCollection xac = nodes[0].Attributes; foreach (XmlAttribute xa in xac) { if (xa.Value == "" ) continue ; switch (xa.Name) { case "Top" : var Top = Convert.ToInt32(xa.Value); s.Top = Top; break ; case "Left" : var Left = Convert.ToInt32(xa.Value); s.Left = Left; break ; case "Width" : var Width = Convert.ToInt32(xa.Value); s.Width = Width; break ; case "Height" : var Height = Convert.ToInt32(xa.Value); s.Height = Height; break ; default : break ; } } } } } #endregion /// <summary> /// 用于实现容器内控件移动和改变大小的方法 /// </summary> /// <param name="XmlDoc">用于保存控件的属性的XML文档</param> public void InitMouseAndContolStyle( string XmlDocPath) { xmlDocPath = XmlDocPath; doc = new XmlDocument(); doc.Load(XmlDocPath); initProperty(); initStyle(); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步