控制未在面板上绘制

时间:2015-05-18 08:12:09

标签: c# .net winforms

我正在尝试向Panel添加控件。在面板上的mouseDown处保存点,并在mouseUp保存该点。但在面板mouseUp没有绘制任何东西。怎么解决?

椭圆类:

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类

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

1 个答案:

答案 0 :(得分:2)

您从Control继承了您的Ellipse类,但实际上您并未使用它作为控件 - 您不是以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. 您实际上不需要x, y, width, height类的Ellipse属性以及getter / setter方法,因为它是Control,并且它有自己的Location }和WidthHeight公共属性。

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

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

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

    或者,如果它应该是一个单一的椭圆(现在你每次都要创建一个新的椭圆) - 在mouseUp处理程序之外创建这个椭圆并在处理程序内部改变它的大小和位置。