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));
}
}
}
}
}
}
1条答案
按热度按时间axzmvihb1#
您可以实现自己的进度条来满足您的需要。这里有一个例子。找到下面的评论。
此控件提供以下输出: