根据百分比设置按钮的背景颜色

时间:2021-01-30 20:54:37

标签: c# windows winforms button background-color

在 c#、windows 窗体中,我想根据输入的双精度值(介于 1 和 100 之间)来更改按钮背景颜色。例如

  • 如果百分比为 100,整个按钮颜色应为黄色。
  • 如果例如75%,那么按钮的 75% 应为黄色,25% 为蓝色,水平方向,即按钮 25% 的左侧部分为蓝色,按钮的右侧部分为黄色。

2 个答案:

答案 0 :(得分:3)

您可以创建自定义按钮控件并使用其背景图像绘制进度。通过这种方式,您可以保持按钮的外观和感觉(包括视觉样式)。所有的绘制逻辑都在控件中,应用代码如下:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    percentButton1.Percent = (int)numericUpDown1.Value;
    percentButton1.Text = $"{ percentButton1.Percent}%";
}

这是你看到的结果:

enter image description here

在以下示例中,我为 PercentButton 创建了 3 个属性:

  • 百分比
  • 百分比完成颜色
  • PercentRemainingColor

代码如下:

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class PercentButton : Button
{
    public PercentButton()
    {
        BackgroundImageLayout = ImageLayout.Stretch;
    }

    Color percentCompletedColor = Color.Yellow;
    [DefaultValue(typeof(Color), "Yellow")]
    public Color PercentCompletedColor
    {
        get { return percentCompletedColor; }
        set
        {
            if (percentCompletedColor != value)
            {
                percentCompletedColor = value;
                Invalidate();
            }
        }
    }

    Color percentRemainingColor = Color.Blue;
    [DefaultValue(typeof(Color), "Blue")]
    public Color PercentRemainingColor
    {
        get { return percentRemainingColor; }
        set
        {
            if (percentRemainingColor != value)
            {
                percentCompletedColor = value;
                Invalidate();
            }
        }
    }

    private int percent = 50;
    [DefaultValue(50)]
    public int Percent
    {
        get { return percent; }
        set
        {
            if (value < 0) value = 0;
            if (value > 100) value = 100;
            if (percent != value)
            {
                percent = value;
                Invalidate();
            }
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Image BackgroundImage
    {
        get { return base.BackgroundImage; }
        set { base.BackgroundImage = value; }
    }

    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override ImageLayout BackgroundImageLayout
    {
        get { return base.BackgroundImageLayout; }
        set { base.BackgroundImageLayout = value; }
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        if (BackgroundImage == null)
            BackgroundImage = new Bitmap(100, 10);
        using (var g = Graphics.FromImage(BackgroundImage))
        {
            if (Percent > 0)
                using (var b1 = new SolidBrush(PercentCompletedColor))
                    g.FillRectangle(b1, new Rectangle(0, 0, Percent, 10));
            if (Percent < 100)
                using (var b1 = new SolidBrush(PercentRemainingColor))
                    g.FillRectangle(b1, new Rectangle(Percent, 0, Percent, 10));
        }
        base.OnPaint(pevent);
    }
}

答案 1 :(得分:1)

您不能同时拥有具有背景颜色属性的两种颜色的按钮。 首先你必须创建一个组件按钮并自定义。 右键单击>添加新项目>从窗体中选择CustomControl>输入名称>确定

之后,编辑代码就足够了。该类继承自按钮类而不是控件类

 public partial class TwoColorButton : Button
    {

        Color clr1, clr2;
        private Color color1 = Color.DodgerBlue;
        private Color color2 = Color.MidnightBlue;
        private int angle = 90;
        private int textX = 100;
        private int textY = 25;
        private String text = "";


        public int ButtonAngle
        {
            get { return angle; }
            set { angle = value; Invalidate(); }
        }
        public String ButtonText
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }
        public Color StartColor
        {
            get { return color1; }
            set { color1 = value; Invalidate(); }
        }
        public Color EndColor
        {
            get { return color2; }
            set { color2 = value; Invalidate(); }
        }
        

        public int GradientAngle
        {
            get { return angle; }
            set { angle = value; Invalidate(); }
        }

        public int TextLocation_X
        {
            get { return textX; }
            set { textX = value; Invalidate(); }
        }
        public int TextLocation_Y
        {
            get { return textY; }
            set { textY = value; Invalidate(); }
        }      
        public TwoColorButton()
        {
            this.Size = new Size(100, 40);
            this.BackColor = Color.Transparent;
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.FlatAppearance.MouseOverBackColor = Color.Transparent;
            this.FlatAppearance.MouseDownBackColor = Color.Transparent;
            text = this.Text;
        }

       
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            color1 = clr1;
            color2 = clr2;
            this.Invalidate();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            textX = (int)((this.Width / 3) - 1);
            textY = (int)((this.Height / 3) + 5);
        }


        //draw circular button function  
       
        //draw rectangular button function  
        void DrawRectangularButton(Graphics g)
        {
            Color c1 = Color.FromArgb(250, color1);
            Color c2 = Color.FromArgb(250, color2);


            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
            g.FillRectangle(b, 0, 0, this.Width, this.Height);


            for (int i = 0; i < 2; i++)
            {
                g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), this.Width - i, 0, this.Width - i, this.Height);
                g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), 0, this.Height - i, this.Width, this.Height - i);

                g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), 0 + i, 0, 0 + i, this.Height);
                g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))), 0, 0 + i, this.Width, i);
            }           
                Point p = new Point(textX, textY);
                SolidBrush frcolor = new SolidBrush(this.ForeColor);
                g.DrawString(text, this.Font, frcolor, p);           

            b.Dispose();
        }




        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.DrawRectangularButton(e.Graphics);            
        }



    }

enter image description here

More read about this

相关问题