mercoledì 13 febbraio 2013

How to: GroupBox Add Style C# r2

Ed eccomi ancora.. Sapevo che ci sarei cascato in qualche modo..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace ColredPanel
{
    public partial class ColoredGroupBox : Panel
    {
        Color _c = Color.Lime;
        Pen _p = null;
        Pen _ps = null;
        bool _s = false;
        int _r = 20;

        public Color GetBoxborder
        {
            get { return _c;}
            set
            {
                _c = value;
                _p = new Pen(new SolidBrush(_c));
                _ps = new Pen(
                        new SolidBrush(Color.WhiteSmoke)
                        );
                this.Refresh();
            }
        }

        public int Radius
        {
            get { return _r; }
            set { _r = value; if (_r > 20) _r = 20; }
        }

        public bool DrawShaodows
        {
            get { return _s; }
            set { _s = value; }
        }

        public string Caption
        {
            get { return lblCaption.Text; }
            set { lblCaption.Text = value; this.Refresh(); }
        }

        public Color CaptionColor
        {
            get { return lblCaption.ForeColor; }
            set { lblCaption.ForeColor = value; }
        }

        public ColoredGroupBox()
        {
            InitializeComponent();
            _p = new Pen(new SolidBrush(_c));          
        }


        private void ColoredGroupBox_Paint(object sender, 
                PaintEventArgs e)
        {       
            RounderedSchema(e.Graphics);
        }


        private void RounderedSchema(Graphics g)
        {
            g.CompositingQuality = 
                    System.Drawing.Drawing2D.CompositingQuality.HighQuality;

            g.FillRectangle(new SolidBrush(this.BackColor)
                    ,0, 0, this.Width, this.Height);

            _p.Width = 1f;
            g.DrawArc(_p, 10, 10, _r, _r, 180, 90);
            g.DrawLine(_p, 18, 10, this.Width - 13, 10);
            g.DrawArc(_p, 10, this.Height - 25, _r, _r, 180, -90);
            g.DrawArc(_p, this.Width - 25, this.Height - 25, _r, _r, 0, 90);
            g.DrawLine(_p, 10, 18, 10, this.Height - 13);
            g.DrawArc(_p, this.Width -25, 10, _r, _r, 0,-90);
            g.DrawLine(_p, 18, this.Height - 5, this.Width - 13, this.Height - 5);
            g.DrawLine(_p, this.Width - 5, 18, this.Width -5, this.Height - 13);

          
            if (_s)
            {
                g.DrawLine(_ps, 18, 11, this.Width - 13, 11);
                g.DrawLine(_ps, 11, 18, 11, this.Height - 13);
            }

            g.FillEllipse(new SolidBrush(_c), 25, 3, _r - 2, _r - 2);
            g.FillEllipse(new SolidBrush(_c), lblCaption.Width +50 , 3, _r - 2, _r - 2);
            g.FillRectangle(new SolidBrush(_c), 35, 4, lblCaption.Width + 25, _r-3);
        }

        private void ColoredGroupBox_Resize(object sender, EventArgs e)
        {
            if (this.Handle != null)
            {
                RounderedSchema(Graphics.FromHwnd(this.Handle));
            }
        }
    }
}
Questa versione è come la precedente solo che è migliore.. ed è più stabile e funziona un po meglio.

Nessun commento: