版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/beibeisong/article/details/50419272
基本逻辑:
1、鼠标移动过程(未按下鼠标左键)进行边界检测,当移动到控件边界时(某范围内),改变显示的光标;
2、此时,若按下鼠标左键(也可是右键),记录鼠标的位置和控件的大小;
3、按下鼠标左键移动过程中,改变控件的大小(三个方向)或者location(五个方向);
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)); } }
改变东南(右下)方向,control为拖动的控件,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(); }