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控件的拖动和缩放的实现就此完成。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
栏 目:C#教程
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5957.html
您可能感兴趣的文章
- 01-10C#通过反射获取当前工程中所有窗体并打开的方法
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10winform实现创建最前端窗体的方法
- 01-10C#实现由四周向中心缩小的窗体退出特效
- 01-10WinForm实现自定义右下角提示效果的方法
- 01-10Extjs4如何处理后台json数据中日期和时间
- 01-10.net2.0+ Winform项目实现弹出容器层
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10delphi制作wav文件的方法
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery