Here's a typical example of enabling both moving, and re-sizing a WinForm that has no TitleBar, FormBorderStyle set to 'None, etc.
The strategy used here is:
1. define two "zones:" a zone where the mouse held down and moving will move the Form, and a zone where the mouse held down and moving will re-size the Form.
2. check in the MouseDown EventHandler for a hit in either of those two zones, and set boolean flags.
3. in the MouseMove EventHandler take appropriate action.
4. if the Form has been re-sized, reset the re-size zone in the Form Resize EventHandler:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ResizableMovableForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Rectangle MoveZone;
private Rectangle ResizeZone;
private const int HitZoneSize = 12;
private bool isMoving = false;
private bool isResizing = false;
private bool IsMouseUp = false;
private int startWidth, startHeight, startMouseX, startMouseY;
private int newWidth, newHeight;
private void Form1_Load(object sender, EventArgs e)
{
MoveZone = new Rectangle(0, 0, HitZoneSize, HitZoneSize);
ResizeZone = new Rectangle(Width - HitZoneSize, Height - HitZoneSize, HitZoneSize, HitZoneSize);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
IsMouseUp = true;
isMoving = false;
isResizing = false;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startMouseX = e.X;
startMouseY = e.Y;
startWidth = this.Width;
startHeight = this.Height;
if (MoveZone.Contains(e.Location))
{
isMoving = true;
IsMouseUp = false;
return;
}
if (ResizeZone.Contains(e.Location))
{
isResizing = true;
IsMouseUp = false;
return;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseUp) return;
if (isMoving)
{
this.Left += e.X - startMouseX;
this.Top += e.Y - startMouseY;
return;
}
else
{
if (isResizing)
{
newWidth = startWidth + e.X - startMouseX;
if (newWidth < this.MinimumSize.Width || newWidth > this.MaximumSize.Width) return;
this.Width = newWidth;
newHeight = startHeight + e.Y - startMouseY;
if (newHeight < this.MinimumSize.Height || newHeight > this.MaximumSize.Height) return;
this.Height = newHeight;
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
ResizeZone = new Rectangle(Width - HitZoneSize, Height - HitZoneSize, HitZoneSize, HitZoneSize);
}
}
}
Notes:
1. I strongly encourage you to set appropriate Minimum and Maximum sizes for your Form at design-time. The code above (for re-size)
depends on having some meaningful values for those properties.
2. Even with the Form's DoubleBuffer property set to 'true, there may be some graphic side-effects of re-sizing which cannot be controlled by using SuspendLayout and ResumeLayout.