基本逻辑:
1、用于边界检测的鼠标移动过程(无需按鼠标左键),当移至控制边界(在一定范围内)时,更改显示的光标;
2、这时,如果您按下鼠标左键(或右键),请记录鼠标的位置和控件的大小;
3、在按住鼠标左键的同时更改控件的大小(三个方向)或位置(五个方向);
4、完整的逻辑:边界检测;
5、作为其自己的类编写,可用于初始化控件以具有通过拖动鼠标来更改大小的功能。
数据准备:方向枚举,用于记录方向
public enum MouseDirection { East, West, South, North, Southeast, Southwest, Northeast, Northwest, None }
鼠标移动:列出了四个方向:东南,西北,西南和东北
if (e.Location.X >= control.Width - 5 && e.Location.Y >= control.Height - 5 ) { control.Cursor = Cursors.SizeNWSE; direction = MouseDirection.Southeast; } else if (e.Location.X <= 5 && e.Location.Y <= 5 ) { control.Cursor = Cursors.SizeNWSE; direction = MouseDirection.Northwest; } else if (e.Location.X <= 5 && e.Location.Y >= control.Height - 5 ) { control.Cursor = Cursors.SizeNESW; direction = MouseDirection.Southwest; } else if (e.Location.X >= control.Width - 5 && e.Location.Y <= 5 ) { control.Cursor = Cursors.SizeNESW; direction = MouseDirection.Northeast; }
单击左键以记录信息

if (e.Button == MouseButtons.Left) { mouseDownPoint = e.Location; oldSize = control.Size; }
按左键移动
if (e.Button == MouseButtons.Left) { if (direction != MouseDirection.None)//改变大小 { Point oldLocation = control.Location; ResizeControl(e.Location);//改变大小的方法 if (EventControlResize != null)//自定义改变大小事件 EventControlResize(new ControlInfo(control.Location, oldLocation, control.Size, oldSize, direction)); } else//移动位置 { Point oldLocation = control.Location; control.Cursor = Cursors.SizeAll; Point location = GetPointToParent(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)); RelocationControl(location);//改变位置的方法 if (EventControlLocationChanged != null)//自定义改变位置事件 EventControlLocationChanged(new ControlInfo(control.Location, oldLocation, control.Size, oldSize, direction)); } }

更改东南(右下)方向,控件为拖动控件,方向为方向,高度和宽度分别计算,最小值为10,最大值为到达边界的右下方父容器的
private void ResizeControl(Point p) { Point mousePosition = GetPointToParent(p);//将控件内坐标转换为父容器坐标 Point location = control.Location; if (direction == MouseDirection.Southeast)//东南 { control.Cursor = Cursors.SizeNWSE; if (mousePosition.X - control.Left <= 10) { mousePosition.X = control.Left + 10; control.Width = 10; } else if (mousePosition.X - control.Left >= control.Parent.Width - control.Left) { mousePosition.X = control.Parent.Width; control.Width = control.Parent.Width - control.Left; } else { control.Width = mousePosition.X - control.Left; } if (mousePosition.Y - control.Top <= 10) { mousePosition.Y = control.Top + 10; control.Height = 10; } else if (mousePosition.Y - control.Top >= control.Parent.Height - control.Top) { mousePosition.Y = control.Parent.Height; control.Height = control.Parent.Height - control.Top; } else { control.Height = mousePosition.Y - control.Top; } }
}
如何更改位置
private void RelocationControl(Point location) { if (location.X < 0) { location.X = 0; } else if (location.X > control.Parent.Width - control.Width) { location.X = control.Parent.Width - control.Width; } if (location.Y < 0) { location.Y = 0; } else if (location.Y > control.Parent.Height - control.Height) { location.Y = control.Parent.Height - control.Height; } control.Location = location; //control.Parent.Refresh(); }

本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-351988-1.html
晶~
而不是再揣测他派了条老船