I am implementing resizable controls on mouse drag. The problem is that the control that is panel in my case is resizing to the minimum size I have set which is good but when it is resized to the minimum size it begins moving to the side the mouse goes.
My question is how to prevent the panel from moving on resize when it reaches the minimum size that is set to.
Problem: The panel moves after the mouse when it reaches the minimum size.
I am using part of this code https://www.codeproject.com/Tips/709121/Move-and-resize-controls-on-a-form-at-runtime-with
If you set minimum size to some of the panels in this example you can reproduce the problem I have.
The file with the source code in the article is made in Visual studio 2010 It says No Licence when I try to open it. I edited the .sln file in notepad and changed # Visual Studio 2010 to # Visual Studio 2019 and it can be opened in visual studio 2019.
The modified Class from the Article:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Move_Resize_Controls
{
class Move_Resize
{
// Event Sensors
private static bool _moving;
private static bool _startMoveOrResize;
private static bool _resizing;
private static Point _cursorStartPoint;
private static Point _last_resize;
private static Point _last_resize_location;
private static Size _currentControlStartSize;
// Edge Sensors
private static bool MouseIsInLeftEdge { get; set; }
private static bool MouseIsInRightEdge { get; set; }
private static bool MouseIsInTopEdge { get; set; }
private static bool MouseIsInBottomEdge { get; set; }
// Control Settings
internal static void Init(Control control, Control container)
{
_moving = false;
_resizing = false;
_cursorStartPoint = Point.Empty;
MouseIsInLeftEdge = false;
MouseIsInLeftEdge = false;
MouseIsInRightEdge = false;
MouseIsInTopEdge = false;
MouseIsInBottomEdge = false;
control.MouseMove += (sender, e) => MoveControl(container, e);
control.MouseUp += (sender, e) => MouseUP_Control(container, e);
control.MouseDown += (sender, e) => MouseDown_Control(container, e);
}
//:-:-:-: Events :-:-:-: ::START::----------------------------------------------------
// ::Move::
private static void MoveControl(Control control, MouseEventArgs e)
{
if(_startMoveOrResize != true)
{
//Edge Properties
Update_Edge_Properties(control, new Point(e.X, e.Y)); // Check if cursor is in some of the Resize Region
UpdateCursor(control); // Update the cursor Type "Cursor image"
}
else // If MouseDown == true
{
//Moving
if ((MouseIsInLeftEdge || MouseIsInRightEdge || MouseIsInTopEdge || MouseIsInBottomEdge) == false ) // If mouse is not in resize area
{
_moving = true; // Moving ís true
// Moving panel on Mouse Move
int X = (e.X - _cursorStartPoint.X) + control.Left;
int Y = (e.Y - _cursorStartPoint.Y) + control.Top;
control.Location = new Point(X,Y);
}
// Resize
else // if Mouse is down and it is in the resize area "Resize panel on mouse move"
{
//control.Size == control.MinimumSize && control.Size.Width /*_currentControlStartSize.Width < control.Size.Width && _currentControlStartSize.Height < control.Size.Height &&*/
if (MouseIsInLeftEdge) // Left Side
{
if (MouseIsInTopEdge) // "Top Left Corner" Left Side and Top Side are only intersecting with eachother in the top left
{
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
control.Height -= (e.Y - _cursorStartPoint.Y);
control.Top += (e.Y - _cursorStartPoint.Y);
}
else if (MouseIsInBottomEdge) // Bottom Left Corner
{
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
}
else // Left
{
if (control.Size.Width == control.MinimumSize.Width)
{
_last_resize_location = control.Location;
}
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
//Down
if (e.X < _last_resize.X && control.Size.Width == control.MinimumSize.Width)
{
control.BackColor = Color.Red;
}
else if (e.X > _last_resize.X)
{
control.BackColor = Color.Blue;
}
_last_resize = new Point(e.X, e.Y);
}
}
else if (MouseIsInRightEdge) //Right
{
if (MouseIsInTopEdge) // Top Right Corner
{
control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
control.Height -= (e.Y - _cursorStartPoint.Y);
control.Top += (e.Y - _cursorStartPoint.Y);
}
else if (MouseIsInBottomEdge) //Bottom Right Corner
{
control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
}
else // Right
{
control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
}
}
else if (MouseIsInTopEdge) // Top
{
control.Height -= (e.Y - _cursorStartPoint.Y);
control.Top += (e.Y - _cursorStartPoint.Y);
}
else if (MouseIsInBottomEdge) // Bottom
{
control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
}
}
}
}
// ::MouseUp::
private static void MouseUP_Control(Control control, MouseEventArgs e)
{
_moving = false;
_resizing = false;
_startMoveOrResize = false;
}
// ::MouseDown::
private static void MouseDown_Control(Control control, MouseEventArgs e)
{
_currentControlStartSize = control.Size;
_cursorStartPoint = e.Location;
_startMoveOrResize = true;
}
//:-:-:-: Events :-:-:-: ::END::----------------------------------------------------
// Update Edge Properties
private static void Update_Edge_Properties(Control control, Point mouseLocationInControl)
{
if(_moving == true)
{
return;
}
MouseIsInLeftEdge = Math.Abs(mouseLocationInControl.X) <= 10; // Left
MouseIsInRightEdge = Math.Abs(mouseLocationInControl.X - control.Width) <= 10; // Right
MouseIsInTopEdge = Math.Abs(mouseLocationInControl.Y) <= 10; // Top
MouseIsInBottomEdge = Math.Abs(mouseLocationInControl.Y - control.Height) <= 10; // Bottom
}
// Currsor Icon
private static void UpdateCursor(Control control)
{
// If Moving "Return" // Dont check for changing cursor image
if(_moving == true)
{
return;
}
if (MouseIsInLeftEdge)
{
if (MouseIsInTopEdge)
{
control.Cursor = Cursors.SizeNWSE; // Change Currsor type to Diagonal with two arrows
}
else if (MouseIsInBottomEdge)
{
control.Cursor = Cursors.SizeNESW;
}
else
{
control.Cursor = Cursors.SizeWE;
}
}
else if (MouseIsInRightEdge)
{
if (MouseIsInTopEdge)
{
control.Cursor = Cursors.SizeNESW;
}
else if (MouseIsInBottomEdge)
{
control.Cursor = Cursors.SizeNWSE;
}
else
{
control.Cursor = Cursors.SizeWE;
}
}
else if (MouseIsInTopEdge || MouseIsInBottomEdge)
{
control.Cursor = Cursors.SizeNS;
}
else
{
control.Cursor = Cursors.Default;
}
}
}
}