很漂亮的按钮

    public class XButton : RadioButton
    {
        public XButton():base()
        {
            Cursor = Cursors.Hand;
            AutoSize = false;
            SetStyle(ControlStyles.ResizeRedraw, true);
        }
        bool mouseEnter = false;
        Color color1 = System.Drawing.Color.LightGreen;
        Color color2 = System.Drawing.Color.Yellow;
        Color color3 = System.Drawing.Color.Gray;
        Color color4 = System.Drawing.Color.DarkGreen;
        Color color5 = System.Drawing.Color.Orange;
        Color color6 = System.Drawing.Color.Black;
        /// <summary>
        /// 第一个颜色设置
        /// </summary>
        public Color Color1
        {
            get { return color1; }
            set { color1 = value; Invalidate(); }
        }
        public Color Color2
        {
            get { return color2; }
            set { color2 = value; Invalidate(); }
        }
        public Color Color3
        {
            get { return color3; }
            set { color3 = value; Invalidate(); }
        }
        public Color Color4
        {
            get { return color4; }
            set { color4 = value; Invalidate(); }
        }
        public Color Color5
        {
            get { return color5; }
            set { color5 = value; Invalidate(); }
        }
        public Color Color6
        {
            get { return color6; }
            set { color6 = value; Invalidate(); }
        }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            System.Drawing.Brush bs = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, Height),
                Checked ? Color1 : mouseEnter ? Color2 : Color3,
                Checked ? Color4 : mouseEnter ? Color5 : Color6);

            pevent.Graphics.FillRectangle(bs, new System.Drawing.Rectangle(0, 0, Width, Height));

            System.Drawing.StringFormat sf = new System.Drawing.StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;

            pevent.Graphics.DrawString(Text, Font, mouseEnter && !Checked ? System.Drawing.Brushes.Blue : System.Drawing.Brushes.White, new System.Drawing.RectangleF(0, 0, Width, Height), sf);
        }

        protected override void OnMouseEnter(EventArgs eventargs)
        {
            mouseEnter = true;
            base.OnMouseEnter(eventargs);
        }

        protected override void OnMouseLeave(EventArgs eventargs)
        {
            mouseEnter = false;
            base.OnMouseLeave(eventargs);
        }
    }

posted @ 2009-12-10 20:58  王伟晔  阅读(291)  评论(0编辑  收藏  举报