调整控件的大小和位置时限制位置
在移动控件并调整控件的大小时,请将控件限制在某个区域,此处的限制区域位于面板内部

在此处查看移动控件的源代码

模仿表单设计以在运行时调整控件的大小和位置
达到效果:
1.控件移动区域只能在面板内部移动


2.移动控件时,鼠标光标移动区域是面板,停止移动时,鼠标光标移动区域是整个屏幕。
这需要设置Cursor.Clip属性
停止移动时,鼠标光标移动区域将设置为整个屏幕
Cursor.Clip = SystemInformation.VirtualScreen;
这包括调整大小时,即移动FrameControl时,还必须限制鼠标光标以移动区域
代码
public class MoveControlHelper
{
//移动控件
public Control ctrl;
//移动控件的父级控件
public static Control ctrlParent;
//获取父级控件的位置和大小(采用屏幕坐标)
public static Func GetCtrlParentClipWhereMouseDown;
public void SetCtrlLimit(Control ctrl)
{
this.ctrl = ctrl;
ctrl.SetMove();
ctrl.MouseDown += Ctrl_MouseDown;
ctrl.MouseMove += Ctrl_MouseMove;
ctrl.Resize += Ctrl_Resize;
ctrl.MouseUp += Ctrl_MouseUp;
}
//获取边框控件
private FrameControl GetFrameControl()
{
foreach (Control item in ctrlParent?.Controls)
{
if (item is FrameControl frameControl && frameControl.Visible == true)
{
return frameControl;
}
}
return null;
}
//鼠标按下时
private void Ctrl_MouseDown(object sender, MouseEventArgs e)
{
Rectangle ctrlParentRect = GetCtrlParentClipWhereMouseDown.Invoke();
//鼠标按下时,限制鼠标光标移动区域为移动控件的父级控件
Cursor.Clip = ctrlParentRect;
//如果是边框控件,则不再绑定事件
if (sender is FrameControl)
{
return;
}
FrameControl frameControl = GetFrameControl();
if (frameControl != null)
{
//清除绑定
frameControl.MouseDown -= Ctrl_MouseDown;
frameControl.MouseUp -= Ctrl_MouseUp;
//重新绑定
frameControl.MouseDown += Ctrl_MouseDown;
frameControl.MouseUp += Ctrl_MouseUp;
}
}
//移动时
private void Ctrl_MouseMove(object sender, MouseEventArgs e)
{
Control ctrl = (Control)sender;
SetCtrlLoc(ctrl);
}
//调整大小时
private void Ctrl_Resize(object sender, EventArgs e)
{
Control ctrl = (Control)sender;
SetCtrlLoc(ctrl);
SetCtrlSize(ctrl);
}
//释放鼠标时
private void Ctrl_MouseUp(object sender, EventArgs e)
{
//鼠标释放时,鼠标光标移动区域为整个屏幕
Cursor.Clip = SystemInformation.VirtualScreen;
}
///
/// 限制控件位置
///
private void SetCtrlLoc(Control ctrl)
{
//左边
if (ctrl.Location.X <= 0)
{
ctrl.Location = new Point(0, ctrl.Location.Y);
}
//上边
if (ctrl.Location.Y <= 0)
{
ctrl.Location = new Point(ctrl.Location.X, 0);
}
//右边
if (ctrl.Location.X + ctrl.Width >= ctrlParent.Width)
{
ctrl.Location = new Point(ctrlParent.Width - ctrl.Width, ctrl.Location.Y);
}
//下边
if (ctrl.Location.Y + ctrl.Height >= ctrlParent.Height)
{
ctrl.Location = new Point(ctrl.Location.X, ctrlParent.Height - ctrl.Height);
}
}
///
/// 限制控件大小
///
private void SetCtrlSize(Control ctrl)
{
if (ctrl.Height >= ctrlParent.Height - 5)
ctrl.Height = ctrlParent.Height - 5;
if (ctrl.Width >= ctrlParent.Width - 5)
ctrl.Width = ctrlParent.Width - 5;
}
}
实现
MoveControlHelper.ctrlParent = panel1;
MoveControlHelper.GetCtrlParentClipWhereMouseDown += () =>
new Rectangle(new Point(MoveControlHelper.ctrlParent.Location.X + Location.X + 8,
MoveControlHelper.ctrlParent.Location.Y + Location.Y + 8 + SystemInformation.CaptionHeight),
MoveControlHelper.ctrlParent.Size);
MoveControlHelper moveControlHelper = new MoveControlHelper();
moveControlHelper.SetCtrlLimit(label1);
moveControlHelper.SetCtrlLimit(button1);
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-351986-1.html
rap更是垃圾
也就是看是否属于无害通过