简体   繁体   中英

Control not drawn on panel

I'm trying to add a control to my Panel. At mouseDown on the panel the point is saved and at mouseUp the point is saved. But at panel mouseUp nothing is drawn. How to solve it?

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

Form1 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;
        }
    }

You are inheriting your Ellipse class from Control, but in fact you're not using it as a control - you're not adding it in Controls collection of form, so in fact it is invisible, inactive and not receiving any events from form.

Also painting the control from outer code looks like a bad design. Control should paint itself, and you should set it bounds from outer code.

Here is snippet to drive you to the right way:

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

And in the form you should create it like:

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

Update

Based on your updated code, I can see a couple of issues:

  1. You actually don't need x, y, width, height properties of your Ellipse class and according getter/setter methods, since it's Control , and it has its own Location and Width , Height public properties.

  2. You are drawing your ellipse incorrectly. Assuming it should fill all the area, painting should be e.Graphics.FillEllipse(Brushes.Black,0,0,Width,Height) (here I assuming using Control.Width instead of your width and so on). Otherwise you're additionally shifting your painted ellipse.

  3. Code in panel_MouseUp concerning ellipse creation should be something like

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

Or, if it should be one single ellipse (right now you're creating new one each time) - create this one outside of mouseUp handler and inside of handler just change it's size and location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM