c# 自定義按鈕,漸變顏色(含中心向四周漸變,單方向漸變)

廢話很少言,直接代碼:ide

public class RoundButton : Button
    {
        bool clickBool = false;
        //1.設置圓形
        //2.設置漸變色
        //3.設置tooltip
        //4.設置點擊以後漸變色,tooltip
        ToolTip toolTip = new ToolTip();
        public RoundButton()
        {
            toolTip.AutoPopDelay = 1000;
            toolTip.SetToolTip(this, "A: B分列選圖。");
        }
        [Browsable(true), DefaultValue(90), Description("按鈕主體顏色漸變方向,X軸順時針開始")]
        [Category("Appearance")]
        public int GradientAngle { get; set; }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            RectangleF rect = new RectangleF(0, 0, this.Width, this.Height);

            //兩種brush使用 LinearGradientBrush和PathGradientBrush
            //Graphics g = pevent.Graphics;
            //g.SmoothingMode = SmoothingMode.AntiAlias;//抗鋸齒
            //Color FColor = Color.Orange;
            //Color TColor = Color.White;
            //Brush b = new LinearGradientBrush(rect, FColor, TColor, 45, false);
            //g.FillEllipse(b, pevent.ClipRectangle);
            if (!clickBool)
            {
                Graphics g = pevent.Graphics;
                g.SmoothingMode = SmoothingMode.AntiAlias;//抗鋸齒
                GraphicsPath graphicsPath = new GraphicsPath();
                graphicsPath.AddEllipse(rect);
                PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
                pathGradientBrush.CenterColor = Color.White;
                pathGradientBrush.CenterPoint = new PointF(this.Width / 2, this.Height / 2);
                pathGradientBrush.SurroundColors = new Color[] { Color.Orange };
                g.FillPath(pathGradientBrush, graphicsPath);
            }
            else
            {
                Graphics g = pevent.Graphics;
                g.SmoothingMode = SmoothingMode.AntiAlias;//抗鋸齒
                GraphicsPath graphicsPath = new GraphicsPath();
                graphicsPath.AddEllipse(rect);
                PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
                pathGradientBrush.CenterColor = Color.White;
                pathGradientBrush.CenterPoint = new PointF(this.Width / 2, this.Height / 2);
                pathGradientBrush.SurroundColors = new Color[] { Color.Blue };
                g.FillPath(pathGradientBrush, graphicsPath);
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            clickBool = !clickBool;
            if (clickBool)
            {
                toolTip.SetToolTip(this, "4 幅圖任選。");
            }
            else
            {
                toolTip.SetToolTip(this, "A: B分列選圖。");
            }
        }
    }
相關文章
相關標籤/搜索