在PictureBox1父控件内调整自定义控件大小时越界问题求助
问题:控件调整尺寸时超出父容器边界
我正在开发一款特殊绘图应用,允许用户在单个窗体上编辑多个PictureBox的尺寸。目前尝试在名为picturebox1的父控件内调整自定义控件大小,但无论向哪个方向调整,控件都会超出父控件边界。
以下是三个鼠标事件的代码:
MouseMove事件
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); } } }
MouseDown事件
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); }
补充说明:
- 代码来自第三方教程
- 试过多个在线项目均不符合需求,且WndProc命令会与项目其他部分冲突
内容的提问来源于stack exchange,提问作者Bullish Programmer 117
相关产品推荐
相关产品推荐

