winforms 在C# Windows窗体应用程序中自定义ProgressBar颜色

t9aqgxwy  于 2023-10-23  发布在  C#
关注(0)|答案(1)|浏览(250)

如何使用C#自定义Windows窗体应用程序中ProgressBar的颜色?解释根据特定条件更改ProgressBar的颜色或创建视觉上吸引人的用户界面的步骤或方法。

progressBar.ForeColor = Color.Blue;

自定义ProgressBar更改颜色以指示C# Windows窗体应用程序中的密码强度。

axzmvihb

axzmvihb1#

您可以实现自己的进度条来满足您的需要。这里有一个例子。找到下面的评论。

using System.Drawing;
using System.Windows.Forms;

namespace Test
{
    /// <summary>
    /// Enumeration of password types.
    /// </summary>
    public enum PasswordStrength
    {
        None = 0,
        Weak = 1,
        Moderate = 2,
        Strong = 3
    }

    /// <summary>
    /// Implements a custom control that visualizes the password strengthness.
    /// </summary>
    public class PasswordStrengthControl : Control
    {
        private PasswordStrength _strength;
        protected Color[] Colors { get; set; } 

        /// <summary>
        /// Gets or sets the password strengthness.
        /// </summary>
        public PasswordStrength Strength
        {
            get { return _strength; }
            set
            {
                _strength = value;
                Invalidate();
            }
        }

        public PasswordStrengthControl()
        {
            Colors = new[] { Color.Red, Color.Orange, Color.Green };
        }

        /// <summary>
        /// Visualize the strengthness.
        /// </summary>
        /// <param name="args">Event arguments.</param>
        protected override void OnPaintBackground(PaintEventArgs args)
        {
            // Fill the background normally.
            base.OnPaintBackground(args);

            // Spacing between the bars.
            var spacing = 5;

            // Total width of the drawing surface.
            var w = Width - Padding.Left - Padding.Right - (spacing * Colors.Length - 1);

            // Total height of the drawing surface.
            var h = Height - Padding.Top - Padding.Bottom;

            // Bar width.
            var width = w / Colors.Length;

            // Color of the bars depending of the password strengthness.
            var color = Strength > PasswordStrength.None ? Colors[(int)Strength - 1] : Color.Transparent;

            using (var borderBrush = new SolidBrush(SystemColors.ActiveBorder))
            using (var borderPen = new Pen(borderBrush, 1))
            using (var fillBrush = new SolidBrush(color))
            {
                // Go through all bars.
                for (var i = 0; i < Colors.Length; i++)
                {
                    // Find the position of each bar.
                    var x = i * width + Padding.Left;
                    if (i > 0)
                    {
                        x += (spacing * i);
                    }

                    // Draw the borders around each bar.
                    args.Graphics.DrawRectangle(borderPen, x, Padding.Top, width - 1, h - 1);

                    if (i < (int)Strength)
                    {
                        // Fill the bars with the respective color depending on the password strengthness.
                        args.Graphics.FillRectangle(fillBrush, new Rectangle(x + 1, Padding.Top + 1, width - 2, h - 2));
                    }
                }
            }
        }
    }
}

此控件提供以下输出:

相关问题