• c# 自定义按钮,渐变颜色(含中心向四周渐变,单方向渐变)


    废话不多言,直接代码:

    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分列选图。");
                }
            }
        }
    

      

  • 相关阅读:
    第2课 有符号与无符号
    第1课 基本数据类型
    HDU 5821 Ball
    Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
    HDU 5810 Balls and Boxes
    HDU 5818 Joint Stacks
    HDU 5813 Elegant Construction
    Codeforces Round #357 (Div. 2)C. Heap Operations
    Codeforces Round #364 (Div. 2) C. They Are Everywhere
    HDU5806 NanoApe Loves Sequence Ⅱ
  • 原文地址:https://www.cnblogs.com/gaara-zhang/p/11211829.html
Copyright © 2020-2023  润新知