Files
@ f2c2ba4ef3d4
Branch filter:
Location: seniordesign-ui/Demo.WindowsForms/BSE.Windows.Forms/ProgressBar/ProgressBar.cs
f2c2ba4ef3d4
19.7 KiB
text/x-csharp
Removed unneeded code and verified use of functions. project is essentially
complete except for some live testing and cache testing/experiments
complete except for some live testing and cache testing/experiments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;
using Demo.WindowsForms.Properties;
using System.Drawing.Drawing2D;
namespace BSE.Windows.Forms
{
/// <summary>
/// Represents a Windows progress bar control.
/// </summary>
/// <copyright>Copyright © 2008 Uwe Eichkorn
/// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
/// REMAINS UNCHANGED.
/// </copyright>
[ToolboxBitmap(typeof(System.Windows.Forms.ProgressBar))]
public partial class ProgressBar : Control
{
#region Events
/// <summary>
/// Occurs when the value of the BorderColor property changes.
/// </summary>
[Description("Occurs when the value of the BorderColor property is changed on the control.")]
public event EventHandler<EventArgs> BorderColorChanged;
/// <summary>
/// Occurs when the value of the BackgroundColor property changes.
/// </summary>
[Description("Occurs when the value of the BackgroundColor property is changed on the control.")]
public event EventHandler<EventArgs> BackgroundColorChanged;
/// <summary>
/// Occurs when the value of the ValueColor property changes.
/// </summary>
[Description("Occurs when the value of the ValueColor property is changed on the control.")]
public event EventHandler<EventArgs> ValueColorChanged;
#endregion
#region FieldsPrivate
private Color m_backgroundColor;
private Color m_valueColor;
private Color m_borderColor;
private int m_iMinimum;
private int m_iMaximum;
private int m_iValue;
#endregion
#region Properties
/// <summary>
/// Gets or sets the color used for the background rectangle of this control.
/// </summary>
/// <value>
/// Type: <see cref="System.Drawing.Color"/>
/// A Color used for the background rectangle of this control.
/// </value>
[Browsable(true)]
[Description("The color used for the background rectangle of this control.")]
public Color BackgroundColor
{
get { return this.m_backgroundColor; }
set
{
if (this.m_backgroundColor != value)
{
this.m_backgroundColor = value;
OnBackgroundColorChanged(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Gets or sets the color used for the value rectangle of this control.
/// </summary>
/// <value>
/// Type: <see cref="System.Drawing.Color"/>
/// A Color used for the value rectangle of this control.
/// </value>
[Browsable(true)]
[Description("The color used for the value rectangle of this control.")]
public Color ValueColor
{
get { return this.m_valueColor; }
set
{
if (this.m_valueColor != value)
{
this.m_valueColor = value;
OnValueColorChanged(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Gets or sets the border color for the control.
/// </summary>
/// <value>
/// Type: <see cref="System.Drawing.Color "/>
/// A Color that represents the border color of the control.
/// </value>
public Color BorderColor
{
get { return this.m_borderColor; }
set
{
if (this.m_borderColor != value)
{
this.m_borderColor = value;
OnBorderColorChanged(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Gets or sets the background color for the control.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
/// <summary>
/// Gets or sets the maximum value of the range of the control.
/// </summary>
/// <value>
/// Type: <see cref="System.Int32"/>
/// The maximum value of the range. The default is 100.
/// </value>
[Browsable(true)]
[Description("The upper bound of range this ProgressBar is working with.")]
public int Maximum
{
get { return this.m_iMaximum; }
set
{
if (this.m_iMaximum != value)
{
if (value < 0)
{
object[] args = new object[] { "Maximum", value.ToString(CultureInfo.CurrentCulture), "Maximum" };
throw new ArgumentOutOfRangeException("Maximum", string.Format(CultureInfo.InvariantCulture, Resources.IDS_InvalidLowBoundArgument, args));
}
if (this.m_iMinimum > value)
{
this.m_iMinimum = value;
}
this.m_iMaximum = value;
if (this.m_iValue > this.m_iMaximum)
{
this.m_iValue = this.m_iMaximum;
}
UpdatePos();
}
}
}
/// <summary>
/// Gets or sets the minimum value of the range of the control.
/// </summary>
/// <value>
/// Type: <see cref="System.Int32"/>
/// The minimum value of the range. The default is 0.
/// </value>
[Browsable(true)]
[Description("The lower bound of range this ProgressBar is working with.")]
public int Minimum
{
get { return this.m_iMinimum; }
set
{
if (this.m_iMinimum != value)
{
if (value < 0)
{
object[] args = new object[] { "Minimum", value.ToString(CultureInfo.CurrentCulture), "Minimum" };
throw new ArgumentOutOfRangeException("Minimum", string.Format(CultureInfo.InvariantCulture, Resources.IDS_InvalidLowBoundArgument, args));
}
if (this.m_iMaximum < value)
{
this.m_iMaximum = value;
}
this.m_iMinimum = value;
if (this.m_iValue < this.m_iMinimum)
{
this.m_iValue = this.m_iMinimum;
}
UpdatePos();
}
}
}
/// <summary>
/// Gets or sets the current position of the progress bar.
/// </summary>
/// <value>
/// Type: <see cref="System.Int32"/>
/// The position within the range of the progress bar. The default is 0.
/// </value>
[Browsable(true)]
[Description("The current value for the ProgressBar, in the range specified by the minimum and maximum properties.")]
public int Value
{
get { return this.m_iValue; }
set
{
if (this.m_iValue != value)
{
if ((value < this.m_iMinimum) || (value > this.m_iMaximum))
{
throw new ArgumentOutOfRangeException("Value", string.Format(CultureInfo.InvariantCulture, Resources.IDS_InvalidBoundArgument, new object[] { "Value", value.ToString(CultureInfo.CurrentCulture), "'minimum'", "'maximum'" }));
}
this.m_iValue = value;
UpdatePos();
}
}
}
#endregion
#region MethodsPublic
/// <summary>
/// Initializes a new instance of the ProgressBar class.
/// </summary>
public ProgressBar()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
this.m_iMaximum = 100;
this.m_backgroundColor = Color.FromArgb(20, 20, 255);
this.m_valueColor = Color.FromArgb(255, 0, 255);
this.m_borderColor = SystemColors.ActiveBorder;
this.BackColor = Color.Transparent;
}
#endregion
#region MethodsProtected
/// <summary>
/// Raises the Paint event.
/// </summary>
/// <param name="e">A PaintEventArgs that contains the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
using (UseAntiAlias antiAlias = new UseAntiAlias(e.Graphics))
{
Graphics graphics = e.Graphics;
DrawProgressBar(
graphics,
this.ClientRectangle,
this.m_backgroundColor,
this.m_valueColor,
this.m_borderColor,
this.RightToLeft,
this.Minimum,
this.Maximum,
this.Value);
if (string.IsNullOrEmpty(this.Text) == false)
{
using (UseClearTypeGridFit useClearTypeGridFit = new UseClearTypeGridFit(graphics))
{
using (SolidBrush textBrush = new SolidBrush(this.ForeColor))
{
using (StringFormat stringFormat = new StringFormat())
{
stringFormat.FormatFlags = StringFormatFlags.NoWrap;
if (this.RightToLeft == RightToLeft.Yes)
{
stringFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
stringFormat.Trimming = StringTrimming.EllipsisCharacter;
stringFormat.LineAlignment = StringAlignment.Center;
stringFormat.Alignment = StringAlignment.Center;
Rectangle stringRectangle = this.ClientRectangle;
graphics.DrawString(this.Text, this.Font, textBrush, stringRectangle, stringFormat);
}
}
}
}
}
}
/// <summary>
/// Raises the BorderColor changed event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A EventArgs that contains the event data.</param>
protected virtual void OnBorderColorChanged(object sender, EventArgs e)
{
this.Invalidate(true);
if (this.BorderColorChanged != null)
{
this.BorderColorChanged(sender, e);
}
}
/// <summary>
/// Raises the BackgroundColor changed event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A EventArgs that contains the event data.</param>
protected virtual void OnBackgroundColorChanged(object sender, EventArgs e)
{
Invalidate();
if (this.BackgroundColorChanged != null)
{
this.BackgroundColorChanged(sender, e);
}
}
/// <summary>
/// Raises the ValueColor changed event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A EventArgs that contains the event data.</param>
protected virtual void OnValueColorChanged(object sender, EventArgs e)
{
Invalidate(true);
if (this.ValueColorChanged != null)
{
this.ValueColorChanged(sender, e);
}
}
#endregion
#region MethodsPrivate
private void UpdatePos()
{
this.Invalidate(true);
}
private static void DrawProgressBar(
Graphics graphics,
Rectangle clientRectangle,
Color colorBackgroundEnd,
Color colorValueEnd,
Color borderColor,
RightToLeft rightToLeft,
int iMinimum,
int iMaximum,
int iValue)
{
Rectangle outerRectangle = GetRectangleBackground(clientRectangle);
using (GraphicsPath outerRectangleGraphicsPath = GetBackgroundPath(outerRectangle, 4))
{
if (outerRectangleGraphicsPath != null)
{
using (LinearGradientBrush gradientBrush = GetGradientBackBrush(outerRectangle, colorBackgroundEnd))
{
if (gradientBrush != null)
{
graphics.FillPath(gradientBrush, outerRectangleGraphicsPath);
}
}
// Draws the value rectangle
if (iValue > 0)
{
Rectangle valueRectangle = GetRectangleValue(outerRectangle, rightToLeft, iMinimum, iMaximum, iValue);
using (GraphicsPath valueGraphicsPath = GetValuePath(valueRectangle, rightToLeft, 5))
{
using (LinearGradientBrush gradientBrush = GetGradientBackBrush(valueRectangle, colorValueEnd))
{
if (gradientBrush != null)
{
graphics.FillPath(gradientBrush, valueGraphicsPath);
}
}
}
}
using (Pen borderPen = new Pen(borderColor))
{
graphics.DrawPath(borderPen, outerRectangleGraphicsPath);
}
}
}
}
private static Rectangle GetRectangleBackground(Rectangle clientRectangle)
{
Rectangle rectangleBackground = clientRectangle;
rectangleBackground.Inflate(-1, -1);
return rectangleBackground;
}
private static Rectangle GetRectangleValue(Rectangle backgroundRectangle, RightToLeft rightToLeft, int iMinimum, int iMaximum, int iValue)
{
Rectangle valueRectangle = backgroundRectangle;
int iProgressRange = iMaximum - iMinimum;
int iValueRange = iValue - iMinimum;
int iRange = (int)((float)iValueRange / (float)iProgressRange * backgroundRectangle.Width);
valueRectangle.Width = iRange;
if (rightToLeft == RightToLeft.Yes)
{
valueRectangle.X = backgroundRectangle.Width - valueRectangle.Width;
}
return valueRectangle;
}
private static GraphicsPath GetBackgroundPath(Rectangle bounds, int radius)
{
int x = bounds.X;
int y = bounds.Y;
int width = bounds.Width;
int height = bounds.Height;
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(x, y, radius, radius, 180, 90); //Upper left corner
graphicsPath.AddArc(x + width - radius, y, radius, radius, 270, 90); //Upper right corner
graphicsPath.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);//Lower right corner
graphicsPath.AddArc(x, y + height - radius, radius, radius, 90, 90); //Lower left corner
graphicsPath.CloseFigure();
return graphicsPath;
}
private static GraphicsPath GetValuePath(Rectangle bounds, RightToLeft rightToLeft, int radius)
{
int x = bounds.X;
int y = bounds.Y;
int width = bounds.Width;
int height = bounds.Height;
GraphicsPath graphicsPath = new GraphicsPath();
if (rightToLeft == RightToLeft.No)
{
graphicsPath.AddArc(x, y, radius, radius, 180, 90); //Upper left corner
graphicsPath.AddLine(x + radius, y, x + width, y); //Upper line
graphicsPath.AddLine(x + width, y, x + width, y + height); //Right line
graphicsPath.AddArc(x, y + height - radius, radius, radius, 90, 90); //Lower left corner
}
else
{
graphicsPath.AddLine(x, y, width - radius, y); //Upper Line
graphicsPath.AddArc(x + width - radius, y, radius, radius, 270, 90); // Upper right corner
graphicsPath.AddLine(x + width, y + radius, x + width, y + radius + height - (2 * radius)); // right line
graphicsPath.AddArc(x + width - radius, y + radius + height - (2 * radius), radius, radius, 360, 90); // Lower right corner
graphicsPath.AddLine(x + width - radius, y + height, x, y + height); // Lower line
}
graphicsPath.CloseFigure();
return graphicsPath;
}
private static LinearGradientBrush GetGradientBackBrush(Rectangle bounds, Color backColor)
{
if (IsZeroWidthOrHeight(bounds))
{
return null;
}
LinearGradientBrush linearGradientBrush = linearGradientBrush = new LinearGradientBrush(bounds, Color.White, backColor, LinearGradientMode.Vertical);
if (linearGradientBrush != null)
{
Blend blend = new Blend();
blend.Positions = new float[] { 0.0F, 0.2F, 0.3F, 0.5F, 0.6F, 0.8F, 1.0F };
blend.Factors = new float[] { 0.3F, 0.4F, 0.5F, 0.8F, 1.0F, 1.0F, 0.9F };
linearGradientBrush.Blend = blend;
}
return linearGradientBrush;
}
/// <summary>
/// Checks if the rectangle width or height is equal to 0.
/// </summary>
/// <param name="rectangle">the rectangle to check</param>
/// <returns>true if the with or height of the rectangle is 0 else false</returns>
private static bool IsZeroWidthOrHeight(Rectangle rectangle)
{
if (rectangle.Width != 0)
{
return (rectangle.Height == 0);
}
return true;
}
#endregion
}
}
|