C# 矩形可以';在运行时,不能通过鼠标拖动在面板上绘制

C# 矩形可以';在运行时,不能通过鼠标拖动在面板上绘制,c#,.net,winforms,onpaint,C#,.net,Winforms,Onpaint,我试图在面板上画一个矩形,但什么也没画出来。下面是显示如何在面板上绘制矩形的代码。在我的代码中,SetSelectionRect()用于设置要绘制的矩形。对于这些,我使用以下方法 private void Panel_MouseDown(object sender, MouseEventArgs e) { Point point = new Point(); this.mouseDown = true; this.Panel.Send

我试图在面板上画一个矩形,但什么也没画出来。下面是显示如何在面板上绘制矩形的代码。在我的代码中,
SetSelectionRect()
用于设置要绘制的矩形。对于这些,我使用以下方法

   private void Panel_MouseDown(object sender, MouseEventArgs e)
    {
        Point point = new Point();
        this.mouseDown = true;
        this.Panel.SendToBack();

        point = this.Panel.PointToClient(Cursor.Position);
        point.X = e.X;
        point.Y = e.Y;
        this.selectionStart.X = point.X;
        this.selectionStart.Y = point.Y;
    }

    private void Panel_MouseMove(object sender, MouseEventArgs e)
    {
        if (!this.mouseDown)
        {
            return;
        }
        else
        {
            this.mouseMove = true;
            Point point = this.Panel.PointToClient(Cursor.Position);
            point.X = e.X;
            point.Y = e.Y;
            this.selectionEnd.X = point.X;
            this.selectionEnd.Y = point.Y;
            this.SetSelectionRect();
            ////this.Panel.Invalidate();
            ////this.Invalidate();
        }
    }

    private void Panel_MouseUp(object sender, MouseEventArgs e)
    {
        SetSelectionRect();
        this.GetSelectedControls();
        this.mouseDown = false;
        this.mouseMove = false;
        ////this.Panel.Invalidate();
        ////this.Invalidate();
        this.Panel.Refresh();
    }

    private void Panel_Paint(object sender, PaintEventArgs e)
    {
        ////base.OnPaint(e); drawRect = true when RectangleToolStripMenuItem is Clicked.
        if (this.drawRect)
        {
            using (Pen pen = new Pen(Color.Black, 1F))
            {

                    this.rectangle = new RectangleShape();
                    this.Panel.SendToBack();
                    this.shapeContainer1.Shapes.Add(this.rectangle);
                    this.rectangle.Location = this.selection.Location;
                    this.rectangle.Size = this.selection.Size;
                    this.rectangle.Name = "rectShape";
                    this.shapeContainer1.Size = this.Panel.Size;
                    this.shapeContainer1.Location = this.Panel.Location;
                    this.rectangle.Enabled = false;
                    this.rectangle.MouseClick += new MouseEventHandler(this.mouseclick);
                    this.rectangle.MouseMove += new MouseEventHandler(this.mouseMove);
                    this.rectangle.MouseDown += new MouseEventHandler(this.mouseDown);
                    this.rectangle.MouseUp += new MouseEventHandler(this.mouseUp);
                    this.drawRect = false; 
            }
        }
    }

    protected override void OnPaint (PaintEventArgs e)
    {
        base.OnPaint(e);
    }

代码有什么问题?

问题是
base.OnPaint(e)
正在引发一个
Paint
事件,您将
base.OnPaint(e)放置在该事件中,所以它一次又一次地调用自己

private void panel_Paint(object sender, PaintEventArgs e)
{
    //base.OnPaint(e); // remove it from here
    // something to do.
}
base.OnPaint(e)

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
}

我试过了。但问题是我需要base.OnPaint(e),因为我可以在面板运行时(通过鼠标拖动)添加矩形形状。没有base.OnPaint(e),我无法绘制形状。使用覆盖的方法OnPaint,查看我的更新答案。太好了。还有一件事。见编辑后的问题。矩形没有绘制。为什么?这实际上应该是一个新问题。在评论中很难回答这个问题。但简而言之,原因是您没有调用
Invalidate
方法。当您的表单需要重新绘制时,调用方法
OnPaint
,当您只需将鼠标移到表单上或单击时,该方法不会发生。我是否应该使
面板
表单
无效?