Excel风格配置页-列表控件(继承自RadioButton)

最近在封装微软的Chart控件,想模仿Excel的配置风格

思路如下:

  左边为分类,邮编为对应的配置项;

  选择继承了radiobutton,作为左边的内容列表项,设置Dock为Top,自动排列

存在问题:

  this.AutoSize = false;//无效,不知道为什么

  this.Size = new Size(130, 25);//无效,不知道为什么

  在构造函数中设置初始化了AutoSize为false,但是使用是发现,默认的还是True,搞不清楚为什么,若有哪位发现问题,欢迎留言,谢谢!

效果图如下:

 

    public partial class ChartCategory : RadioButton
    {
        public ChartCategory()
        {
            this.AutoSize = false;//无效,不知道为什么
            this.Size = new Size(130, 25);//无效,不知道为什么
            this.Dock = DockStyle.Top;
            this.BackColor = Color.White;
            this.Appearance = System.Windows.Forms.Appearance.Button;
            this.TextAlign = ContentAlignment.MiddleLeft;
        }
        /// <summary>
        /// 圆弧半径
        /// </summary>
        private int cornerRadius = 5;

        /// <summary>
        /// 边框颜色
        /// </summary>
        private Color borderCorlor = Color.FromArgb(229, 195, 101);

        private Color highCorlor = Color.FromArgb(255, 251, 237);

        private Color lowCorlor = Color.FromArgb(255, 236, 181);

        private bool isActive = false;

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Parent.BackColor), e.ClipRectangle);
            if (this.Checked)
            {
                highCorlor = Color.FromArgb(255, 255, 200);
                GraphicsPath Rect = RoundRect(ClientRectangle, cornerRadius, cornerRadius, cornerRadius, cornerRadius);
                LinearGradientBrush brush = new LinearGradientBrush(Rect.GetBounds(), highCorlor, lowCorlor, LinearGradientMode.Vertical);
                e.Graphics.FillPath(brush, Rect);
                e.Graphics.DrawPath(new Pen(borderCorlor), Rect);
            }
            if (isActive)
            {
                GraphicsPath Rect = RoundRect(ClientRectangle, cornerRadius, cornerRadius, cornerRadius, cornerRadius);
                LinearGradientBrush brush = new LinearGradientBrush(Rect.GetBounds(), highCorlor, lowCorlor, LinearGradientMode.Vertical);
                e.Graphics.FillPath(brush, Rect);
                e.Graphics.DrawPath(new Pen(borderCorlor), Rect);
            }
            if (!string.IsNullOrEmpty(Text))
            {
                StringFormat sf = GetStringFormat();
                Rectangle rect = ClientRectangle;
                rect.Location = new Point(5, 0);
                rect.Size = new Size(Size.Width - 5, Size.Height);
                e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rect, sf);
            }
        }

        private StringFormat GetStringFormat()
        {
            StringFormat sf = new StringFormat();
            switch (TextAlign)
            {
                case ContentAlignment.BottomCenter:
                    {
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Far;
                    }
                    break;
                case ContentAlignment.BottomLeft:
                    {
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Far;
                    }
                    break;
                case ContentAlignment.BottomRight:
                    {
                        sf.Alignment = StringAlignment.Far;
                        sf.LineAlignment = StringAlignment.Far;
                    }
                    break;
                case ContentAlignment.MiddleCenter:
                    {
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                    }
                    break;
                case ContentAlignment.MiddleLeft:
                    {
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Center;
                    }
                    break;
                case ContentAlignment.MiddleRight:
                    {
                        sf.Alignment = StringAlignment.Far;
                        sf.LineAlignment = StringAlignment.Center;
                    }
                    break;
                case ContentAlignment.TopCenter:
                    {
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Near;
                    }
                    break;
                case ContentAlignment.TopLeft:
                    {
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Near;
                    }
                    break;
                case ContentAlignment.TopRight:
                    {
                        sf.Alignment = StringAlignment.Far;
                        sf.LineAlignment = StringAlignment.Near;
                    }
                    break;
                default:
                    break;
            }
            return sf;
        }

        protected override void OnMouseEnter(EventArgs eventargs)
        {
            base.OnMouseEnter(eventargs);
            highCorlor = Color.FromArgb(255, 251, 237);
            lowCorlor = Color.FromArgb(255, 236, 181);
            isActive = true;
            this.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs eventargs)
        {
            base.OnMouseLeave(eventargs);
            isActive = false;
            this.Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            highCorlor = Color.FromArgb(255, 236, 181);
            lowCorlor = Color.FromArgb(255, 251, 237);
            isActive = true;
            this.Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            highCorlor = Color.FromArgb(255, 251, 237);
            lowCorlor = Color.FromArgb(255, 236, 181);
            isActive = true;
            this.Checked = true;
            this.Invalidate();
        }

        /// <summary>
        /// 通过贝赛尔曲线画出带有弧度的按钮,绘制轮廓
        /// </summary>
        private GraphicsPath RoundRect(RectangleF r, float r1, float r2, float r3, float r4)
        {
            float x = r.X, y = r.Y, w = r.Width - 1, h = r.Height - 1;
            GraphicsPath rr = new GraphicsPath();
            rr.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y);
            rr.AddLine(x + r1, y, x + w - r2, y);
            rr.AddBezier(x + w - r2, y, x + w, y, x + w, y + r2, x + w, y + r2);
            rr.AddLine(x + w, y + r2, x + w, y + h - r3);
            rr.AddBezier(x + w, y + h - r3, x + w, y + h, x + w - r3, y + h, x + w - r3, y + h);
            rr.AddLine(x + w - r3, y + h, x + r4, y + h);
            rr.AddBezier(x + r4, y + h, x, y + h, x, y + h - r4, x, y + h - r4);
            rr.AddLine(x, y + h - r4, x, y + r1);
            return rr;
        }
    }

 

 

posted @ 2013-07-23 17:32  呆瓜瓜  阅读(558)  评论(0)    收藏  举报