简体   繁体   English

控件未在面板上绘制

[英]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. 在面板上的mouseDown下,该点被保存,而在mouseUp上,该点被保存。 But at panel mouseUp nothing is drawn. 但是在面板mouseUp上没有绘制任何内容。 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 Form1班

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. 您正在从Control继承Ellipse类,但实际上您没有将其用作控件-您没有将其添加到Controls集合中,因此实际上它是不可见的,不活动的,并且没有从表单接收任何事件。

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. 实际上,您不需要Ellipse类的x, y, width, height属性以及相应的getter / setter方法,因为它是Control ,并且具有自己的LocationWidthHeight公共属性。

  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). 假设它应填满所有区域,则绘画应为e.Graphics.FillEllipse(Brushes.Black,0,0,Width,Height) (此处我假设使用Control.Width而不是您的width ,依此类推)。 Otherwise you're additionally shifting your painted ellipse. 否则,您将另外移动绘制的椭圆。

  3. Code in panel_MouseUp concerning ellipse creation should be something like panel_MouseUp有关椭圆创建的代码应类似于

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. 或者,如果它应该是一个椭圆(现在每次都在创建一个新椭圆)-在mouseUp处理程序外部和处理程序内部创建一个椭圆形,只需更改其大小和位置即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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