鼠标事件在父控件的边界内调整控件的大小

问题描述 投票:0回答:0

我试图在另一个控件中调整自定义控件的大小(在本例中,父控件称为 picturebox1),但无论我向哪个方向调整它的大小,它都会超出范围。

我正在尝试为一个特殊的绘画应用程序制作这个,用户可以在一个表单上编辑多个图片框的大小。

三个鼠标事件的代码如下所示

补充说明:

我从这个链接得到这个代码:

https://www.codeproject.com/Tips/709121/Move-and-Resize-Controls-on-a-Form-at-Runtime-With

鼠标移动事件:

    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);

            }
        }
    }

鼠标按下事件:

    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”命令导致我的项目其他部分出现问题。

c# winforms resize mouseevent
© www.soinside.com 2019 - 2024. All rights reserved.