C# Winfom控件、窗体、Panel拖动缩放、移动位置通用类(ControlMoveTool.cs)
拖拽标题区移动Panel组件
https://www.cscode.net/archive/newdoc/476078900068357.html
拖拽边框缩放功能
C# 全选
ControlMoveTool.Handle(pnlProperty, true, false, false, false); //拖拽边框缩放功能
ControlMoveTool 类
C# 全选
/// <summary>
/// C#-Winfom控件拖动缩放、移动位置工具类
/// www.csframework.com C/S框架网
/// </summary>
public class ControlMoveTool
{
/// <summary>
/// 鼠标移动到控件边缘被捕获的宽度值(像素)
/// </summary>
const int CaptureMouseWidth = 5;
/// <summary>
/// 缩放控件最小宽度
/// </summary>
const int MinWidth = 10;
/// <summary>
/// 缩放控件最小高度
/// </summary>
const int MinHeight = 10;
/// <summary>
/// 鼠标位置类型
/// </summary>
private EnumMousePointPosition _MousePointPosition;
//两个临时变量
private Point pTmp1, pTmp2;
/// <summary>
/// 当前控件(窗体或Panel)
/// </summary>
private Control _control;
/// <summary>
/// 拖拽边框线高亮
/// </summary>
private Color _focusColor = Color.Green;
bool _showFocus;
bool _maxIfDoubleClick;
bool _allowMove;
bool _allowResize;
/// <summary>
/// 绑定控件(窗体或Panel)
/// </summary>
/// <param name="control">窗体或Panel</param>
/// <param name="allowResize">允许缩放</param>
/// <param name="allowMove">允许移动</param>
/// <param name="showFocus">拖拽边框线高亮</param>
/// <param name="maxIfDoubleClick">双击最大化</param>
public static void Handle(Control control,
bool allowResize = true,
bool allowMove = true,
bool showFocus = true,
bool maxIfDoubleClick = true)
{
var handle = new ControlMoveTool
{
_control = control,
_allowResize = allowResize,
_allowMove = allowMove,
_showFocus = showFocus,
_maxIfDoubleClick = maxIfDoubleClick,
};
handle.BindingEvents();
}
private void BindingEvents()
{
this._control.Cursor = Cursors.Default; //默认
this._control.MouseDown += new System.Windows.Forms.MouseEventHandler(On_MouseDown);
this._control.MouseMove += new System.Windows.Forms.MouseEventHandler(On_MouseMove);
this._control.MouseLeave += new System.EventHandler(On_MouseLeave);
this._control.DoubleClick += On_DoubleClick;
this._control.Paint += On_Paint;
}
private void On_DoubleClick(object sender, EventArgs e)
{
if (_maxIfDoubleClick)
{
if (_control is Form form)
{
if (form.WindowState == FormWindowState.Maximized)
form.WindowState = FormWindowState.Normal;
else
form.WindowState = FormWindowState.Maximized;
}
}
}
private bool _IsCapture = false;
Pen _pen;
private void On_Paint(object sender, PaintEventArgs e)
{
if (_showFocus)
{
if (_IsCapture)
{
_pen = new Pen(_focusColor, CaptureMouseWidth);
}
else
{
_pen = new Pen(_control.BackColor, CaptureMouseWidth);
}
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 0);
switch (_MousePointPosition)
{
case EnumMousePointPosition.MouseSizeLeft://左
p1 = new Point(0, 0);
p2 = new Point(0, _control.Height);
break;
case EnumMousePointPosition.MouseSizeRight://右
p1 = new Point(_control.Width, 0);
p2 = new Point(_control.Width, _control.Height);
break;
case EnumMousePointPosition.MouseSizeTop://上
p1 = new Point(0, 0);
p2 = new Point(_control.Width, 0);
break;
case EnumMousePointPosition.MouseSizeBottom://下
p1 = new Point(0, _control.Height);
p2 = new Point(_control.Width, _control.Height);
break;
}
e.Graphics.DrawLine(_pen, p1, p2);
}
}
private void On_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
pTmp1.X = e.X;
pTmp1.Y = e.Y;
pTmp2.X = e.X;
pTmp2.Y = e.Y;
}
private void On_MouseLeave(object sender, System.EventArgs e)
{
_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
_control.Cursor = Cursors.Arrow;
}
private void On_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
_control.Refresh();
Control ctrl = (sender as Control);
//按住鼠标左键
if (e.Button == MouseButtons.Left)
{
switch (_MousePointPosition)
{
case EnumMousePointPosition.MouseDrag:
if (_allowMove)
{
ctrl.Left = ctrl.Left + e.X - pTmp1.X;
ctrl.Top = ctrl.Top + e.Y - pTmp1.Y;
}
break;
case EnumMousePointPosition.MouseSizeBottom:
if (_allowResize)
{
ctrl.Height = ctrl.Height + e.Y - pTmp2.Y;
pTmp2.X = e.X;
pTmp2.Y = e.Y; //记录光标拖动的当前点
}
break;
case EnumMousePointPosition.MouseSizeBottomRight:
if (_allowResize)
{
ctrl.Width = ctrl.Width + e.X - pTmp2.X;
ctrl.Height = ctrl.Height + e.Y - pTmp2.Y;
pTmp2.X = e.X;
pTmp2.Y = e.Y; //记录光标拖动的当前点
}
break;
case EnumMousePointPosition.MouseSizeRight:
if (_allowResize)
{
ctrl.Width = ctrl.Width + e.X - pTmp2.X;
pTmp2.X = e.X;
pTmp2.Y = e.Y; //记录光标拖动的当前点
}
break;
case EnumMousePointPosition.MouseSizeTop:
if (_allowResize)
{
ctrl.Top = ctrl.Top + (e.Y - pTmp1.Y);
ctrl.Height = ctrl.Height - (e.Y - pTmp1.Y);
}
break;
case EnumMousePointPosition.MouseSizeLeft://拉升左边框
if (_allowResize)
{
ctrl.Left = ctrl.Left + e.X - pTmp1.X;
ctrl.Width = ctrl.Width - (e.X - pTmp1.X);
}
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
if (_allowResize)
{
ctrl.Left = ctrl.Left + e.X - pTmp1.X;
ctrl.Width = ctrl.Width - (e.X - pTmp1.X);
ctrl.Height = ctrl.Height + e.Y - pTmp2.Y;
pTmp2.X = e.X;
pTmp2.Y = e.Y; //记录光标拖动的当前点
}
break;
case EnumMousePointPosition.MouseSizeTopRight:
if (_allowResize)
{
ctrl.Top = ctrl.Top + (e.Y - pTmp1.Y);
ctrl.Width = ctrl.Width + (e.X - pTmp2.X);
ctrl.Height = ctrl.Height - (e.Y - pTmp1.Y);
pTmp2.X = e.X;
pTmp2.Y = e.Y; //记录光标拖动的当前点
}
break;
case EnumMousePointPosition.MouseSizeTopLeft:
if (_allowResize)
{
ctrl.Left = ctrl.Left + e.X - pTmp1.X;
ctrl.Top = ctrl.Top + (e.Y - pTmp1.Y);
ctrl.Width = ctrl.Width - (e.X - pTmp1.X);
ctrl.Height = ctrl.Height - (e.Y - pTmp1.Y);
}
break;
default:
break;
}
if (ctrl.Width < MinWidth) ctrl.Width = MinWidth;
if (ctrl.Height < MinHeight) ctrl.Height = MinHeight;
}
else //移动到捕获区域,记录光标位置
{
_MousePointPosition = GetMousePointPosition(ctrl.Size, e); //判断光标的位置状态
_IsCapture = false
|| _MousePointPosition == EnumMousePointPosition.MouseSizeLeft
|| _MousePointPosition == EnumMousePointPosition.MouseSizeRight
|| _MousePointPosition == EnumMousePointPosition.MouseSizeTop
|| _MousePointPosition == EnumMousePointPosition.MouseSizeBottom;
switch (_MousePointPosition) //改变光标
{
case EnumMousePointPosition.MouseSizeNone:
_control.Cursor = Cursors.Arrow; //箭头
break;
case EnumMousePointPosition.MouseDrag:
//_panel.Cursor = Cursors.SizeAll; //四方向
_control.Cursor = Cursors.Default; //默认
break;
case EnumMousePointPosition.MouseSizeBottom:
_control.Cursor = Cursors.SizeNS; //南北
break;
case EnumMousePointPosition.MouseSizeTop:
_control.Cursor = Cursors.SizeNS; //南北
break;
case EnumMousePointPosition.MouseSizeLeft:
_control.Cursor = Cursors.SizeWE; //东西
break;
case EnumMousePointPosition.MouseSizeRight:
_control.Cursor = Cursors.SizeWE; //东西
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
_control.Cursor = Cursors.SizeNESW; //东北到南西
break;
case EnumMousePointPosition.MouseSizeBottomRight:
_control.Cursor = Cursors.SizeNWSE; //东南到西北
break;
case EnumMousePointPosition.MouseSizeTopLeft:
_control.Cursor = Cursors.SizeNWSE; //东南到西北
break;
case EnumMousePointPosition.MouseSizeTopRight:
_control.Cursor = Cursors.SizeNESW; //东北到南西
break;
default:
break;
}
}
}
/// <summary>
/// 确定光标在控件不同位置的样式
/// </summary>
/// <param name="size"></param>
/// <param name="e"></param>
/// <returns></returns>
private EnumMousePointPosition GetMousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
{
if ((e.X >= -1 * CaptureMouseWidth) | (e.X <= size.Width) | (e.Y >= -1 * CaptureMouseWidth) | (e.Y <= size.Height))
{
if (e.X < CaptureMouseWidth)
{
if (e.Y < CaptureMouseWidth)
{
return EnumMousePointPosition.MouseSizeTopLeft;
}
else
{
if (e.Y > -1 * CaptureMouseWidth + size.Height)
{
return EnumMousePointPosition.MouseSizeBottomLeft;
}
else
{
return EnumMousePointPosition.MouseSizeLeft;
}
}
}
else
{
if (e.X > -1 * CaptureMouseWidth + size.Width)
{
if (e.Y < CaptureMouseWidth)
{
return EnumMousePointPosition.MouseSizeTopRight;
}
else
{
if (e.Y > -1 * CaptureMouseWidth + size.Height)
{
return EnumMousePointPosition.MouseSizeBottomRight;
}
else
{
return EnumMousePointPosition.MouseSizeRight;
}
}
}
else
{
if (e.Y < CaptureMouseWidth)
{
return EnumMousePointPosition.MouseSizeTop;
}
else
{
if (e.Y > -1 * CaptureMouseWidth + size.Height)
{
return EnumMousePointPosition.MouseSizeBottom;
}
else
{
return EnumMousePointPosition.MouseDrag;
}
}
}
}
}
else
{
return EnumMousePointPosition.MouseSizeNone;
}
}
}
C# 全选
/// <summary>
/// 鼠标位置类型
/// </summary>
public enum EnumMousePointPosition
{
/// <summary>
/// 无
/// </summary>
MouseSizeNone = 0,
/// <summary>
/// 拉伸右边框
/// </summary>
MouseSizeRight = 1,
/// <summary>
/// 拉伸左边框
/// </summary>
MouseSizeLeft = 2,
/// <summary>
/// 拉伸下边框
/// </summary>
MouseSizeBottom = 3,
/// <summary>
/// 拉伸上边框
/// </summary>
MouseSizeTop = 4,
/// <summary>
/// 拉伸左上角
/// </summary>
MouseSizeTopLeft = 5,
/// <summary>
/// 拉伸右上角
/// </summary>
MouseSizeTopRight = 6,
/// <summary>
/// 拉伸左下角
/// </summary>
MouseSizeBottomLeft = 7,
/// <summary>
/// 拉伸右下角
/// </summary>
MouseSizeBottomRight = 8,
/// <summary>
/// 鼠标拖动
/// </summary>
MouseDrag = 9
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
csframework.workflow C/S框架网