When I try to run this code the behavior of the output is unexpected and I think the code is correct. If anyone got some insight please help me to resolve this problem.
I've tried to adjust the code to solve the problem in different ways without luck. The main problem should be in the MouseMove event but I cannot see the fault.
namespace Personal.Forms
{
public partial class NewMainForm : Form
{
private const int BorderWidth = 5;
private bool isResizing = false;
private Point lastMousePosition;
private Size originalSize;
public NewMainForm()
{
InitializeComponent();
InitializeFormEvents();
}
private void InitializeFormEvents()
{
this.MouseDown += MainForm_MouseDown;
this.MouseMove += MainForm_MouseMove;
this.MouseUp += MainForm_MouseUp;
}
private void MainForm_MouseDown(object? sender, MouseEventArgs e)
{
if (IsOnBorder(e.Location))
{
isResizing = true;
lastMousePosition = e.Location;
originalSize = this.Size;
}
}
private void MainForm_MouseMove(object? sender, MouseEventArgs e)
{
if (isResizing)
{
int deltaX = e.X - lastMousePosition.X;
int deltaY = e.Y - lastMousePosition.Y;
if (lastMousePosition.X < BorderWidth)
{
// Adjust left side
this.Width = Math.Max(originalSize.Width - deltaX, this.MinimumSize.Width);
this.Left += originalSize.Width - this.Width;
}
else if (lastMousePosition.X > this.Width - BorderWidth)
{
// Adjust right side
this.Width = Math.Max(originalSize.Width + deltaX, this.MinimumSize.Width);
}
if (lastMousePosition.Y < BorderWidth)
{
// Adjust top side
this.Height = Math.Max(originalSize.Height - deltaY, this.MinimumSize.Height);
this.Top += originalSize.Height - this.Height;
}
else if (lastMousePosition.Y > this.Height - BorderWidth)
{
// Adjust bottom side
this.Height = Math.Max(originalSize.Height + deltaY, this.MinimumSize.Height);
}
lastMousePosition = e.Location;
}
else if (IsOnBorder(e.Location))
{
this.Cursor = Cursors.SizeAll;
}
else
{
this.Cursor = Cursors.Default;
}
}
private void MainForm_MouseUp(object? sender, MouseEventArgs e)
{
isResizing = false;
}
private bool IsOnBorder(Point point)
{
return point.X < BorderWidth || point.X > this.Width - BorderWidth ||
point.Y < BorderWidth || point.Y > this.Height - BorderWidth;
}
}
}
I suggest a slightly different approach. Let's add the following declaration to the form:
The
Borderenum indicates which border is affected. We determine it in theMouseDownevent:Note that I am storing the distance of the mouse cursor to the non-moving border. I use the screen coordinates given by
MousePositionfor this. It yields more stable positions than the local mouse coordinates which are affected by movements of our form. The distance we get is by itself an absurd number, but this doesn't matter, as we only need the relative measurements.Moving the mouse becomes: