面板上未绘制控件

Control not drawn on panel

我正在尝试向我的面板添加一个控件。在面板上的 mouseDown 点被保存,在 mouseUp 点被保存。但是在 mouseUp 面板上没有绘制任何东西。如何解决?

椭圆class:

class Ellipse : Control
{    

    private int x;
    private int y;
    private int width;
    private int height;

    public Ellipse(int x, int y, int width, int height)
    {
        setY(y);
        setX(x);
        setWidth(width);
        setHeight(height);
    }

    public int getX() { return x;}
    public int getY() { return y; }
    public int getWidth() { return width; }
    public int getHeight() { return height; }
    public void setX(int newx) { x = newx; }
    public void setY(int newy) { y = newy; }
    public void setWidth(int newwidth) { width = newwidth; }
    public void setHeight(int newheight) { height = newheight; }


    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        // Call methods of the System.Drawing.Graphics object.
        // Declare and instantiate a new pen.
        System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Aqua);

        // Draw an aqua rectangle in the rectangle represented by the control.
        e.Graphics.FillEllipse(Brushes.Black,x,y,width,height);
    }    
}

表格 1 class

private void panel_MouseDown(object sender, MouseEventArgs e)
    {
        draw = true;
        x = e.X;
        y = e.Y;
    }

private void panel_MouseUp(object sender, MouseEventArgs e)
    {
        draw = false;
        xe = e.X;
        ye = e.Y;

        Item item; 
        Enum.TryParse<Item>(menuComboBoxShape.ComboBox.SelectedValue.ToString(), out item);

        switch (item)
        {

            case Item.Pencil:
                using (Graphics g = panel.CreateGraphics())
                    using (var pen = new Pen(System.Drawing.Color.Black))     //Create the pen used to draw the line (using statement makes sure the pen is disposed)
                    {
                        g.DrawLine(pen,new Point(x, y), new Point(xe, ye));
                    }
                break;
            case Item.Rectangle:
                break;
            case Item.Ellipse:
                Ellipse el = new Ellipse(x,y,xe-x,ye-y);
                panel.Controls.Add(el);

                break;
            default:
                break;
        }
    }

您从 Control 继承了 Ellipse class,但实际上您没有将它用作控件 - 您没有将它添加到 Controls 表单集合中, 所以实际上它是不可见的、不活动的并且没有从表单接收任何事件。

从外部代码绘制控件看起来也是个糟糕的设计。控件应该绘制自身,并且您应该从外部代码设置它的边界。

这里是引导您走向正确道路的片段:

class Ellipse : Control
{
    Point mDown { get; set; }

    public Ellipse()
    {
        MouseDown += shape_MouseDown;
        MouseMove += shape_MouseMove;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillEllipse(Brushes.Black, this.Bounds);
    }

    private void shape_MouseDown(object sender, MouseEventArgs e)
    {
        mDown = e.Location;
    }

    private void shape_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Location = new Point(e.X + Left - mDown.X, e.Y + Top - mDown.Y);
        }
    }
}

并且在表单中你应该创建它:

el = new Ellipse();
el.Bounds = new Rectangle(0, 0, 100, 100);
Controls.Add(el);

更新

根据您更新后的代码,我发现了几个问题:

  1. 您实际上不需要 Ellipse class 的 x, y, width, height 属性和 getter/setter 方法,因为它是 Control , 它有自己的 LocationWidth, Height public 属性。

  2. 您画错了椭圆。假设它应该填满所有区域,绘画应该是 e.Graphics.FillEllipse(Brushes.Black,0,0,Width,Height)(这里我假设使用 Control.Width 而不是你的 width 等等)。否则,您还要移动绘制的椭圆。

  3. panel_MouseUp 中关于创建椭圆的代码应该类似于

var el = new Ellipse();
panel.Controls.Add(el);
el.Location = new Point(x, y);
el.Width = (xe - x);
el.Height = (ye - y);

或者,如果它应该是一个椭圆(现在您每次都在创建一个新的椭圆)- 在 mouseUp 处理程序外部和处理程序内部创建这个,只需更改它的大小和位置即可。