To mimic the resize behavior of normal borders when using panels as borders with FormBorderStyle.None in C#, you can handle the mouse events and manually resize the form.
Here is an example of achieving this:
- Add the following variables to your form class:
private const int WM_NCHITTEST = 0x84;
private const int HT_LEFT = 10;
private const int HT_RIGHT = 11;
private const int HT_TOP = 12;
private const int HT_TOPLEFT = 13;
private const int HT_TOPRIGHT = 14;
private const int HT_BOTTOM = 15;
private const int HT_BOTTOMLEFT = 16;
private const int HT_BOTTOMRIGHT = 17;
private const int RESIZE_HANDLE_SIZE = 10;
private bool _resizing = false;
private Point _resizeStartPoint;
- Handle the following events for your form:
- MouseDown: Start resizing if the mouse is clicked on one of the resize handles.
- MouseMove: Resize the form if _resizing is true.
- MouseUp: Stop resizing.
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if (_resizing)
{
m.Result = (IntPtr)HT_CAPTION;
return;
}
var hitPoint = PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16));
if (TopLeft.Contains(hitPoint))
m.Result = (IntPtr)HT_TOPLEFT;
else if (TopRight.Contains(hitPoint))
m.Result = (IntPtr)HT_TOPRIGHT;
else if (BottomLeft.Contains(hitPoint))
m.Result = (IntPtr)HT_BOTTOMLEFT;
else if (BottomRight.Contains(hitPoint))
m.Result = (IntPtr)HT_BOTTOMRIGHT;
else if (Top.Contains(hitPoint))
m.Result = (IntPtr)HT_TOP;
else if (Left.Contains(hitPoint))
m.Result = (IntPtr)HT_LEFT;
else if (Right.Contains(hitPoint))
m.Result = (IntPtr)HT_RIGHT;
else if (Bottom.Contains(hitPoint))
m.Result = (IntPtr)HT_BOTTOM;
}
else
{
base.WndProc(ref m);
}
}
private Rectangle TopLeft => new Rectangle(0, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
private Rectangle TopRight => new Rectangle(Width - RESIZE_HANDLE_SIZE, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
private Rectangle BottomLeft => new Rectangle(0, Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
private Rectangle BottomRight => new Rectangle(Width - RESIZE_HANDLE_SIZE, Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
private Rectangle Top => new Rectangle(RESIZE_HANDLE_SIZE, 0, Width - 2 * RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
private Rectangle Left => new Rectangle(0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, Height - 2 * RESIZE_HANDLE_SIZE);
private Rectangle Right => new Rectangle(Width - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, Height - 2 * RESIZE_HANDLE_SIZE);
private Rectangle Bottom => new Rectangle(RESIZE_HANDLE_SIZE, Height - RESIZE_HANDLE_SIZE, Width - 2 * RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (TopLeft.Contains(e.Location))
{
_resizing = true;
_resizeStartPoint = e.Location;
return;
}
// Similarly, handle other resize areas
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_resizing)
{
int deltaX = e.Location.X - _resizeStartPoint.X;
int deltaY = e.Location.Y - _resizeStartPoint.Y;
// Resize the form here based on the delta values
// For example, you can use this.Resize or this.SetBounds methods
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (_resizing)
{
_resizing = false;
}
}
This code allows you to mimic the resize behavior by clicking and dragging on the appropriate panels acting as borders. You will need to handle resizing the form based on the delta values in the MouseMove event.