https://youtu.be/LuyRdCO7wuU
// groupBoxCc.cs
public class GroupBoxCc : GroupBox
{
private Color borderColor = Color.Blue;
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; this.Invalidate(); }
}
private Color titleColor = Color.Black;
public Color TitleColor
{
get { return titleColor; }
set { titleColor = value; this.Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// text Display
Brush textBrush = new SolidBrush(this.titleColor);
g.DrawString(" " + this.Text, this.Font, textBrush, new Point(0, 0));
// Draw border
Pen pen = new Pen(this.borderColor);
int startY = this.Font.Height / 2;
//Rectangle bounds = new Rectangle(0, startY, base.Width - 1, base.Height - 1 - startY);
//g.DrawRectangle(pen, bounds);
Rectangle bounds = new Rectangle(0, startY, base.Width - 1, base.Height - 1);
Size textSize = TextRenderer.MeasureText(" " + this.Text, this.Font);
g.DrawLine(pen, bounds.X + textSize.Width, bounds.Y, bounds.Width, bounds.Y); // - : top
g.DrawLine(pen, bounds.X, bounds.Y, bounds.X, bounds.Height); // | : left
g.DrawLine(pen, bounds.X, bounds.Height, bounds.Width, bounds.Height); // - : bottom
g.DrawLine(pen, bounds.Width, bounds.Y, bounds.Width, bounds.Height); // | : right
}
}
// UserControlForm.cs
public partial class UserControlFomr : Form
{
public UserControlFomr()
{
InitializeComponent();
}
private void UserControlFomr_Load(object sender, EventArgs e)
{
groupBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.groupBox_MouseDown);
groupBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.groupBox_MouseMove);
groupBoxCc1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.groupBox_MouseDown);
groupBoxCc1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.groupBox_MouseMove);
}
Point mousePoint;
Size controlSize;
private void groupBox_MouseDown(object sender, MouseEventArgs e)
{
Control con = sender as Control;
mousePoint = new Point(e.X, e.Y);
controlSize = con.Size;
}
private void groupBox_MouseMove(object sender, MouseEventArgs e)
{
Control con = sender as Control;
int xDiff = con.Size.Width - e.X;
int yDiff = con.Size.Height - e.Y;
if(xDiff < 20 && yDiff < 20)
{
Cursor.Current = Cursors.SizeNWSE;
}
if((e.Button & MouseButtons.Left) != MouseButtons.Left) return;
if(xDiff < 20 && yDiff < 20)
{
con.Size = new Size(controlSize.Width - (mousePoint.X - e.X),
controlSize.Height - (mousePoint.Y - e.Y));
return;
}
con.Location = new Point(con.Left - (mousePoint.X - e.X),
con.Top - (mousePoint.Y - e.Y));
}
}