自定义面板作为附图

时间:2015-10-08 16:56:08

标签: c# winforms

我正在使用VS2013并且我想绘制附加的形状。建议

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以使用DrawRectangle对象的FillRectangleGraphics方法绘制此类绘图:

private void DrawShapes(Graphics g)
{
    var p1= new Point(0,0);
    var topSize = new Size(450, 25);
    var bottomSize = new Size(450,350);

    var topRectangle= new Rectangle(p1.X, p1.Y, topSize.Width,  topSize.Height);
    var bottomRectangle= new Rectangle(p1.X, p1.Y + topSize.Height,  bottomSize.Width, bottomSize.Height);
    g.FillRectangle(Brushes.Blue, topRectangle);
    g.DrawRectangle(Pens.Lime, topRectangle);
    g.FillRectangle(Brushes.Red, bottomRectangle);
    g.DrawRectangle(Pens.Lime, bottomRectangle);
}

例如,在面板的Paint事件中,您可以这样使用它:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    this.DrawShapes(e.Graphics);
}

结果将是:

enter image description here

答案 1 :(得分:2)

根据我正在使用的内容进行修改:

public class CustomPanel : Panel
{
    private Color _ColorBorder = Color.Lime;
    private Padding _BordersThickness = new Padding(1);
    private Color _HeaderColor = Color.Blue;
    private int _HeaderHeight = 25;
    private int _SeperatorWidth = 1;

    public CustomPanel()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
        //or simply this.ResizeRedraw = true; as IvanStoev pointed out. I'm not sure here
        typeof(Control).GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, true, null);
        //same as above
        typeof(Panel).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, true, null);
        this.Width = 450;
        this.Height = 375;
        this.Padding = _BordersThickness;
        this.BackColor = Color.Red;
        this.DoubleBuffered = true;
        SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //draw header
        using (SolidBrush brush = new SolidBrush(HeaderColor))
        {
            e.Graphics.FillRectangle(brush, 0, 0, this.Width, _HeaderHeight);
        }
        //draw border
        if (_BordersThickness.Left > 0)
            using (Pen p = new Pen(_ColorBorder, _BordersThickness.Left * 2))
            {
                e.Graphics.DrawLine(p, 0, 0, 0, this.Height);
            }
        if (_BordersThickness.Top > 0)
            using (Pen p = new Pen(_ColorBorder, _BordersThickness.Top * 2))
            {
                e.Graphics.DrawLine(p, 0, 0, this.Width, 0);
            }
        if (_BordersThickness.Right > 0)
            using (Pen p = new Pen(_ColorBorder, _BordersThickness.Right * 2))
            {
                e.Graphics.DrawLine(p, this.Width, 0, this.Width, this.Height);
            }
        if (_BordersThickness.Bottom > 0)
            using (Pen p = new Pen(_ColorBorder, _BordersThickness.Bottom * 2))
            {
                e.Graphics.DrawLine(p, 0, this.Height, this.Width, this.Height);
            }
        //draw seperator
        if (_SeperatorWidth > 0)
            using (Pen p = new Pen(_ColorBorder, _SeperatorWidth))
            {
                e.Graphics.DrawLine(p, 0, _HeaderHeight, this.Width, _HeaderHeight);
            }
    }

    public int SeperatorWidth
    {
        get
        {
            return _SeperatorWidth;
        }
        set
        {
            _SeperatorWidth = value;
            this.Invalidate();
        }
    }

    public int HeaderHeight
    {
        get
        {
            return _HeaderHeight;
        }
        set
        {
            _HeaderHeight = value;
            this.Invalidate();
        }
    }

    public Color HeaderColor
    {
        get
        {
            return _HeaderColor;
        }
        set
        {
            _HeaderColor = value;
            this.Invalidate();
        }
    }

    public Color BorderColor
    {
        get
        {
            return _ColorBorder;
        }
        set
        {
            _ColorBorder = value;
            this.Invalidate();
        }
    }

    public Padding BordersThickness
    {
        get
        {
            return _BordersThickness;
        }
        set
        {
            _BordersThickness = value;
            this.Padding = _BordersThickness;
            this.Invalidate();
        }
    }
}

如果需要,您可以添加或删除一些属性。