簡體   English   中英

控件未在面板上繪制

[英]Control not drawn on 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;
        }
    }

您正在從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. 實際上,您不需要Ellipse類的x, y, width, height屬性以及相應的getter / setter方法,因為它是Control ,並且具有自己的LocationWidthHeight公共屬性。

  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處理程序外部和處理程序內部創建一個橢圓形,只需更改其大小和位置即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM