I'm trying to resize a custom control within another control (in this case the parent is called picturebox1), but it keeps going out bounds no matter which direction I resize it.
I'm trying to make this for a special paint app where the users can edit the size of multiple picture boxes on a single form.
The code for the three mouse events are listed below.
Additional Note:
I got this code from this link:
https://www.codeproject.com/Tips/709121/Move-and-Resize-Controls-on-a-Form-at-Runtime-With
Mouse Move event:
private void MoveControl(Object sender, MouseEventArgs e)
{
Control control = (Control)sender;
if (!_resizing && !_moving)
{
UpdateMouseEdgeProperties(control, new Point(e.X, e.Y));
UpdateMouseCursor(control);
}
if (_resizing)
{
pictureBox1.Invalidate();
if (MouseIsInLeftEdge)
{
if (MouseIsInTopEdge)
{
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
control.Height -= (e.Y - _cursorStartPoint.Y);
}
else if (MouseIsInBottomEdge)
{
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
}
else
{
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
}
}
else if (MouseIsInRightEdge)
{
if (MouseIsInTopEdge)
{
control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
control.Height -= (e.Y - _cursorStartPoint.Y);
control.Top += (e.Y - _cursorStartPoint.Y);
}
else if (MouseIsInBottomEdge)
{
control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
}
else
{
control.Width -= (e.X - _cursorStartPoint.X);
control.Left += (e.X - _cursorStartPoint.X);
}
}
else if (MouseIsInTopEdge)
{
control.Height -= (e.Y - _cursorStartPoint.Y);
control.Top += (e.Y - _cursorStartPoint.Y);
}
else if (MouseIsInBottomEdge)
{
control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
}
else
{
StopDragOrResizing(control);
}
}
else if (_moving)
{
_moveIsInterNal = !_moveIsInterNal;
if (!_moveIsInterNal)
{
int nx = Math.Min(Math.Max(control.Left + (e.X - start.X), 0), pictureBox1.Width - control.Width);
int ny = Math.Min(Math.Max(control.Top + (e.Y - start.Y), 0), pictureBox1.Height - control.Height);
control.Location = new Point(nx, ny);
}
}
}
Mouse down event:
private void StartMovingOrResizing(Object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
start = e.Location;
}
Control control = (Control)sender;
if (_moving || _resizing)
{
return;
}
if (WorkType != MoveOrResize.Move &&
(MouseIsInRightEdge || MouseIsInLeftEdge || MouseIsInTopEdge || MouseIsInBottomEdge))
{
_resizing = true;
_currentControlStartSize = control.Size;
}
else if (WorkType != MoveOrResize.Resize)
{
_moving = true;
control.Cursor = Cursors.Hand;
}
_cursorStartPoint = new Point(e.X, e.Y);
control.Capture = true;
DrawControlBorder(control);
control.Invalidate();
pictureBox1.Invalidate();
}
MouseUp
private void StopDragOrResizing(Object sender, MouseEventArgs e)
{
Control control = (Control)sender;
_resizing = false;
_moving = false;
control.Capture = false;
UpdateMouseCursor(control);
control.Invalidate();
DrawControlBorder(control);
}
I tried using various projects online, but none of them had what I was seeking and the "WndProc" command was causing me problems with other parts of my project.