简体   繁体   中英

Custom panel as attached picture

I'm using VS2013 and I want to draw the attached shape. Advice please

在此处输入图片说明

You can use DrawRectangle and FillRectangle methods of a Graphics object, to draw such drawing:

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

For example in a Paint event of a panel, you can use it this way:

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

And the result will be:

在此处输入图片说明

Modified from what i'm using:

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

You can add or remove some properties if needed.

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