hiimray-co-uk-www-posts/2020/11/BorderlessForm.cs
2023-08-05 00:01:48 +01:00

275 lines
9.4 KiB
C#

using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public class BorderlessForm : Form
{
protected bool isDragging = false;
protected Rectangle lastRectangle = new Rectangle();
public BorderlessForm() : base()
{
this.FormBorderStyle = FormBorderStyle.None;
initialiseFormEdge();
}
protected void initialiseFormEdge()
{
int resizeWidth = 5;
this.MouseDown += new MouseEventHandler(form_MouseDown);
this.MouseMove += new MouseEventHandler(form_MouseMove);
this.MouseUp += delegate (object sender, MouseEventArgs e)
{
isDragging = false;
};
// bottom
UserControl uc1 = new UserControl()
{
Anchor = (AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right),
Height = resizeWidth,
Width = this.DisplayRectangle.Width - (resizeWidth * 2),
Left = resizeWidth,
Top = this.DisplayRectangle.Height - resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNS
};
uc1.MouseDown += form_MouseDown;
uc1.MouseUp += form_MouseUp;
uc1.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size(lastRectangle.Width, e.Y - lastRectangle.Y + this.Height);
}
};
uc1.BringToFront();
this.Controls.Add(uc1);
// right
UserControl uc2 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom),
Height = this.DisplayRectangle.Height - (resizeWidth * 2),
Width = resizeWidth,
Left = this.DisplayRectangle.Width - resizeWidth,
Top = resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeWE
};
uc2.MouseDown += form_MouseDown;
uc2.MouseUp += form_MouseUp;
uc2.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size(e.X - lastRectangle.X + this.Width, lastRectangle.Height);
}
};
uc2.BringToFront();
this.Controls.Add(uc2);
// bottom-right
UserControl uc3 = new UserControl()
{
Anchor = (AnchorStyles.Bottom | AnchorStyles.Right),
Height = resizeWidth,
Width = resizeWidth,
Left = this.DisplayRectangle.Width - resizeWidth,
Top = this.DisplayRectangle.Height - resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNWSE
};
uc3.MouseDown += form_MouseDown;
uc3.MouseUp += form_MouseUp;
uc3.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size((e.X - lastRectangle.X + this.Width), (e.Y - lastRectangle.Y + this.Height));
}
};
uc3.BringToFront();
this.Controls.Add(uc3);
// top-right
UserControl uc4 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Right),
Height = resizeWidth,
Width = resizeWidth,
Left = this.DisplayRectangle.Width - resizeWidth,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNESW
};
uc4.MouseDown += form_MouseDown;
uc4.MouseUp += form_MouseUp;
uc4.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.Y - lastRectangle.Y);
int y = (this.Location.Y + diff);
this.Location = new Point(this.Location.X, y);
this.Size = new Size(e.X - lastRectangle.X + this.Width, (this.Height + (diff * -1)));
}
};
uc4.BringToFront();
//uc4.BackColor = Color.Firebrick;
this.Controls.Add(uc4);
// top
UserControl uc5 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right),
Height = resizeWidth,
Width = this.DisplayRectangle.Width - (resizeWidth * 2),
Left = resizeWidth,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNS
};
uc5.MouseDown += form_MouseDown;
uc5.MouseUp += form_MouseUp;
uc5.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.Y - lastRectangle.Y);
int y = (this.Location.Y + diff);
this.Location = new Point(this.Location.X, y);
this.Size = new Size(lastRectangle.Width, (this.Height + (diff * -1)));
}
};
uc5.BringToFront();
this.Controls.Add(uc5);
// left
UserControl uc6 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom),
Height = this.DisplayRectangle.Height - (resizeWidth * 2),
Width = resizeWidth,
Left = 0,
Top = resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeWE
};
uc6.MouseDown += form_MouseDown;
uc6.MouseUp += form_MouseUp;
uc6.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.X - lastRectangle.X);
int x = (this.Location.X + diff);
this.Location = new Point(x, this.Location.Y);
this.Size = new Size((this.Width + (diff * -1)), this.Height);
}
};
uc6.BringToFront();
this.Controls.Add(uc6);
// bottom-left
UserControl uc7 = new UserControl()
{
Anchor = (AnchorStyles.Bottom | AnchorStyles.Left),
Height = resizeWidth,
Width = resizeWidth,
Left = 0,
Top = this.DisplayRectangle.Height - resizeWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNESW
};
uc7.MouseDown += form_MouseDown;
uc7.MouseUp += form_MouseUp;
uc7.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int diff = (e.Location.X - lastRectangle.X);
int x = (this.Location.X + diff);
this.Location = new Point(x, this.Location.Y);
this.Size = new Size((this.Width + (diff * -1)), (e.Y - lastRectangle.Y + this.Height));
}
};
uc7.BringToFront();
this.Controls.Add(uc7);
// bottom-left
UserControl uc8 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Left),
Height = resizeWidth,
Width = resizeWidth,
Left = 0,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNWSE
};
uc8.MouseDown += form_MouseDown;
uc8.MouseUp += form_MouseUp;
uc8.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
int dX = (e.Location.X - lastRectangle.X);
int dY = (e.Location.Y - lastRectangle.Y);
int x = (this.Location.X + dX);
int y = (this.Location.Y + dY);
this.Location = new Point(x, y);
this.Size = new Size((this.Width + (dX * -1)), (this.Height + (dY * -1)));
}
};
uc8.BringToFront();
this.Controls.Add(uc8);
}
private void form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastRectangle = new Rectangle(e.Location.X, e.Location.Y, this.Width, this.Height);
}
}
private void form_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
int x = (this.Location.X + (e.Location.X - lastRectangle.X));
int y = (this.Location.Y + (e.Location.Y - lastRectangle.Y));
this.Location = new Point(x, y);
}
}
private void form_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
}
}