核心类:

 using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace ControlTest
public class AddControlDragAttr
{
//委托事件 用于支持将其他的可拖拉的控件描点置隐藏
public event EventHandler OtherLabelsVisibleEvent; /// <summary>
/// 当前控件
/// </summary>
public Control CurrControl { get; private set; } /// <summary>
/// 当前鼠标点击坐标
/// </summary>
Point downPoint;
/// <summary>
/// 当前控件的左右宽高
/// </summary>
int normalLeft, normalTop, normalWidth, normalHeight; /// <summary>
/// 是否允许拖拉
/// </summary>
bool draging; /// <summary>
/// 控件最小值
/// </summary>
public int ControlMinSize => ; /// <summary>
/// 是否已点击
/// </summary>
bool isDown = false;
/// <summary>
/// 点击可拖动的描点的控件
/// </summary>
Label[] labels = new Label[]; private Color barColor = Color.Black;
/// <summary>
/// 拖动块的颜色
/// </summary>
public Color BarColor
{
get => this.barColor;
set
{
for (int i = ; i < labels.Length; i++)
{
labels[i].BackColor = value;
}
this.barColor = value;
}
}
private bool labsVisible = false;
/// <summary>
/// 是否显示拖动块的描点
/// </summary>
public bool LabsVisible
{
get => labsVisible;
set
{
foreach (var lab in labels)
{
lab.Visible = value;
} labsVisible = value;
}
} private int barSize = ;
/// <summary>
/// 拖动块描点的大小
/// </summary>
public int BarSize
{
get => this.barSize;
set
{
foreach (var item in labels)
{
item.Size = new Size(value, value);
}
this.barSize = value;
}
} /// <summary>
///系统自带的可拖动描点
/// </summary>
Cursor[] cursors = new Cursor[]
{
//获取双向对角线(西北/东南)大小调整光标
Cursors.SizeNWSE,
//获取双向垂直(北/南)大小调整光标
Cursors.SizeNS,
//获取双向对角线(东北/西南)大小调整光标
Cursors.SizeNESW,
//获取双向水平(西/东)大小调整光标
Cursors.SizeWE,
Cursors.SizeNWSE,
Cursors.SizeNS,
Cursors.SizeNESW,
Cursors.SizeWE,
}; public AddControlDragAttr(Control control)
{
CurrControl = control;
CurrControl.MouseDown += control_MouseDown;
CurrControl.MouseUp += control_MouseUp;
CurrControl.MouseMove += control_MouseMove;
for (int i = ; i < labels.Length; i++)
{
labels[i] = new Label
{
TabIndex = i,
BackColor = BarColor,
Cursor = cursors[i],
Text = "",
Visible = false,
//置顶
};
labels[i].MouseDown += label_MouseDown;
labels[i].MouseMove += label_MouseMove;
labels[i].MouseUp += label_MouseUp; CurrControl.Parent.Controls.Add(labels[i]);
}
} //控件事件
private void control_MouseDown(object sender, MouseEventArgs e)
{
downPoint = e.Location;
isDown = true;
LabsVisible = false; } private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDown)
{
int x = CurrControl.Location.X + e.Location.X - downPoint.X;
int y = CurrControl.Location.Y + e.Location.Y - downPoint.Y; CurrControl.Location = new Point(x, y);
LabsVisible = false;
}
} private void control_MouseUp(object sender, MouseEventArgs e)
{
int x = CurrControl.Location.X + e.Location.X - downPoint.X;
int y = CurrControl.Location.Y + e.Location.Y - downPoint.Y; CurrControl.Location = new Point(x, y); SetBarBounds();
isDown = false; LabsVisible = true; OtherLabelsVisibleEvent?.Invoke(null,null);
} //控件拖动块描点事件
private void label_MouseDown(object sender, MouseEventArgs e)
{
this.draging = true;
this.normalLeft = CurrControl.Left;
this.normalTop = CurrControl.Top;
this.normalWidth = CurrControl.Width;
this.normalHeight = CurrControl.Height;
this.LabsVisible = false;
} /// <summary>
/// 0 1 2
/// 7 3
/// 6 5 4
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label_MouseMove(object sender, MouseEventArgs e)
{
int left = CurrControl.Left;
int width = CurrControl.Width;
int top = CurrControl.Top;
int height = CurrControl.Height;
if (draging)
{
switch (((Control)sender).TabIndex)
{
case :
left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
width = normalLeft + normalWidth - CurrControl.Left;
height = normalTop + normalHeight - CurrControl.Top;
break;
case :
top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
height = normalTop + normalHeight - CurrControl.Top;
break;
case :
width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
height = normalTop + normalHeight - CurrControl.Top;
break;
case :
width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
break;
case :
width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
break;
case :
height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
break;
case :
left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
width = normalLeft + normalWidth - CurrControl.Left;
height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
break;
case :
left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
width = normalLeft + normalWidth - CurrControl.Left;
break;
}
left = (left < ) ? : left;
top = (top < ) ? : top;
CurrControl.SetBounds(left, top, width, height);
}
} private void label_MouseUp(object sender, MouseEventArgs e)
{
draging = false;
SetBarBounds();
this.LabsVisible = true;
} //设置拖动块描点的位置
private void SetBarBounds()
{
int x = CurrControl.Left - BarSize;
int y = CurrControl.Top - BarSize;
int w = CurrControl.Width + BarSize;
int h = CurrControl.Height + BarSize;
int b = BarSize / ;
var px = new List<int>
{
x + b,
x + w / ,
x + w - b,
x + w - b,
x + w - b,
x + w / ,
x + b,
x + b,
};
var py = new List<int>
{
y + b,
y + b,
y + b,
y + h / ,
y + h - b,
y + h - b,
y + h - b,
y + h /
}; for (int i = ; i < labels.Length; i++)
{
labels[i].SetBounds(px[i], py[i], BarSize, BarSize);
}
}
}
}

调用实例:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace ControlTest
{
public partial class Form1 : Form
{
List<AddControlDragAttr> listcontrolsattr = new List<AddControlDragAttr>(); public Form1()
{
InitializeComponent();
} protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Controls.AddRange(new Control[] {
new UserControl1{ Name = "usercontrol1" },
new Button{ Name ="btn1", Size = new Size(,)
,Text= "马克思和恩格斯对社会主义发展出了他们的理论体系,亦认为社会主义社会是资本主义社会向共产主义社会过渡的社会形态" },
new PictureBox{ Name="pic1", Size = new Size(,), Location = new Point(,)
,BackgroundImage = Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "img.jpg"))
,BackgroundImageLayout = ImageLayout.Stretch}
}); foreach (Control control in this.Controls)
{
var controlattr = new AddControlDragAttr(control); //将其他控件的描点隐藏
controlattr.OtherLabelsVisibleEvent += (sender1,e1) => {
SetOtherLabelVisible(control);
}; listcontrolsattr.Add(controlattr);
}
} public void SetOtherLabelVisible(Control control)
{
foreach (AddControlDragAttr item in listcontrolsattr)
{
if (item.CurrControl.Name != control.Name)
item.LabsVisible = false;
}
} }
}
05-14 22:43