通过鼠标拖动运行时无法在面板上绘制矩形

时间:2013-12-26 06:46:26

标签: c# .net winforms onpaint

我正在尝试在面板上绘制一个矩形,但没有绘制任何内容。下面是显示我如何在面板上绘制矩形的代码。在我的代码中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);
    }

代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

问题是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.
}
应该在overriden方法中调用

base.OnPaint(e);

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