Option Explicit On Option Strict Off Option Compare Text '============================================================================================================= ' ' modProgressBar.vb ' ----------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .NET ) ' ' Created On : Sept 11, 2002 ' Last Update : Sept 11, 2002 ' ' VB Versions : VB.NET 1.1 (VS.NET 2003) ' ' Requires : The .NET Framework v1.1 ' (Reference System.Drawing.dll) ' ' Description : This module takes any ordinary control (must support the "CreateGraphics" method) and turns it ' into a fully functional dynamic ProgressBar with percentage display, color control, and gradient ' background options. ' ' Example Use : ' ' Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click ' Dim intValue As Integer ' For intValue = 0 To 65000 ' ProgressBar(PictureBox1, 0, 32000, intValue, False, HorizontalAlignment.Center, True) ' Next ' End Sub ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Imports System Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Module modProgressBar #Region "Public Functions" ' (See last function for documentation) Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Single, _ ByVal Max As Single, _ ByVal Value As Single, _ ByVal ShowBorder As Boolean) Call ProgressBar(TheControl, CDbl(Min), CDbl(Max), CDbl(Value), , , , , ShowBorder, , False) End Sub ' (See last function for documentation) Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Single, _ ByVal Max As Single, _ ByVal Value As Single, _ ByVal ShowBorder As Boolean, _ ByVal CaptionAlignment As HorizontalAlignment) Call ProgressBar(TheControl, CDbl(Min), CDbl(Max), CDbl(Value), , , , , ShowBorder, , True, CaptionAlignment) End Sub ' (See last function for documentation) Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Single, _ ByVal Max As Single, _ ByVal Value As Single, _ ByVal ShowBorder As Boolean, _ ByVal GradientBackground As Boolean) Call ProgressBar(TheControl, CDbl(Min), CDbl(Max), CDbl(Value), , , , , ShowBorder, , False, , GradientBackground) End Sub ' (See last function for documentation) Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Single, _ ByVal Max As Single, _ ByVal Value As Single, _ ByVal ShowBorder As Boolean, _ ByVal CaptionAlignment As HorizontalAlignment, _ ByVal GradientBackground As Boolean) Call ProgressBar(TheControl, CDbl(Min), CDbl(Max), CDbl(Value), , , , , ShowBorder, , True, CaptionAlignment, GradientBackground) End Sub ' (See last function for documentation) Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Single, _ ByVal Max As Single, _ ByVal Value As Single, _ ByVal ForeColor As Color, _ ByVal BackColor1 As Color, _ ByVal BackColor2 As Color, _ ByVal FillColor As Color, _ ByVal ShowBorder As Boolean, _ ByVal BorderColor As Color, _ ByVal ShowProgressCaption As Boolean, _ ByVal CaptionAlignment As HorizontalAlignment, _ Optional ByVal GradientBackground As Boolean = False, _ Optional ByVal GradientDirection As LinearGradientMode = LinearGradientMode.Horizontal) Call ProgressBar(TheControl, CDbl(Min), CDbl(Max), CDbl(Value), ForeColor.ToArgb, BackColor1.ToArgb, BackColor2.ToArgb, FillColor.ToArgb, ShowBorder, BorderColor.ToArgb, ShowProgressCaption, CaptionAlignment, GradientBackground, GradientDirection) End Sub ' (See last function for documentation) Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Single, _ ByVal Max As Single, _ ByVal Value As Single, _ ByVal ForeColor As Integer, _ ByVal BackColor1 As Integer, _ ByVal BackColor2 As Integer, _ ByVal FillColor As Integer, _ ByVal ShowBorder As Boolean, _ ByVal BorderColor As Integer, _ ByVal ShowProgressCaption As Boolean, _ ByVal CaptionAlignment As HorizontalAlignment, _ Optional ByVal GradientBackground As Boolean = False, _ Optional ByVal GradientDirection As LinearGradientMode = LinearGradientMode.Horizontal) Call ProgressBar(TheControl, CDbl(Min), CDbl(Max), CDbl(Value), ForeColor, BackColor1, BackColor2, FillColor, ShowBorder, BorderColor, ShowProgressCaption, CaptionAlignment, GradientBackground, GradientDirection) End Sub '============================================================================================================= ' ProgressBar ' ' This function takes any VB.NET control that supports the "CreateGraphics" method and turns it into a fully ' functional ProgressBar supporting percentage display, color control, and gradient colors. ' ' Parameter: Use: ' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ' TheControl This is the the control that you wish to turn into a ProgressBar. ' Min This is the minimum value of the ProgressBar. ' Max This is the maximum value of the ProgressBar. ' Value This is the current value of the ProgressBar. ' ForeColor Optional. This specifies the color that will be used to print the ' percentage text display on the ProgressBar. If this is not specified ' or is set to (-1) then the default color is used. ' BackColor1 Optional. This is the main color of the ProgressBar. As the Value ' nears the Max, you see more of this color. ' BackColor2 Optional. This is the gradient color that is blended with "BackColor1" ' if the "GradientBackground" parameter is TRUE. ' FillColor Optional.This is the color you see when there's an absense of value. As ' the Value nears the Max, you see less of this color. ' ShowBorder Optional. If set to TRUE, a border will be drawn around the edge of the ' ProgressBar. ' BorderColor Optional. If "ShowBorder" is TRUE, this specifies the color of the border. ' ShowProgressCaption Optional. If set to TRUE, the current percentage of the ProgressBar is ' displayed to the user. ' CaptionAlignment Optional. If "ShowProgressCaption" is set to TRUE, this specifies where ' on the ProgressBar the text will be displayed. ' GradientBackground Optional. If TRUE, the value of "BackColor2" will be blended with ' "BackColor1" in a Gradient effect. ' GradientDirection Optional. If "GradientBackground" is TRUE, this determines what type of ' gradient effect will be used. ' ' Returns: ' ¯¯¯¯¯¯¯¯ ' Nothing ' '============================================================================================================= Public Overloads Sub ProgressBar(ByRef TheControl As Object, _ ByVal Min As Double, _ ByVal Max As Double, _ ByVal Value As Double, _ Optional ByVal ForeColor As Integer = -1, _ Optional ByVal BackColor1 As Integer = -1, _ Optional ByVal BackColor2 As Integer = -1, _ Optional ByVal FillColor As Integer = -1, _ Optional ByVal ShowBorder As Boolean = True, _ Optional ByVal BorderColor As Integer = -1, _ Optional ByVal ShowProgressCaption As Boolean = False, _ Optional ByVal CaptionAlignment As HorizontalAlignment = HorizontalAlignment.Center, _ Optional ByVal GradientBackground As Boolean = False, _ Optional ByVal GradientDirection As LinearGradientMode = LinearGradientMode.Horizontal) On Error GoTo ErrorTrap Dim DEFAULT_COLOR_FILL As Color = CType(SystemColors.Control, Color) Dim DEFAULT_COLOR_FORE As Color = Color.White Dim DEFAULT_COLOR_BACK1 As Color = Color.Blue Dim DEFAULT_COLOR_BACK2 As Color = Color.LightCyan Dim DEFAULT_COLOR_BORDER As Color = Color.Black Dim objGraphics As Graphics = Nothing Dim clrForeColor As Color Dim clrBackColor1 As Color Dim clrBackColor2 As Color Dim clrFillColor As Color Dim clrBorderColor As Color Dim TextFont As Font = New Font(FontFamily.GenericSansSerif, 12) Dim TextSize As SizeF Dim objBrush As LinearGradientBrush Dim TheCaption As String Dim RangeDiff As Double Dim sngDevide As Single Dim intModifier As Integer Dim sngX As Single Dim sngY As Single ' Make sure a PictureBox control was specified If TheControl Is Nothing Then Exit Sub ' Validate the colors to be used clrForeColor = DEFAULT_COLOR_FORE If ForeColor <> -1 Then clrForeColor = ColorTranslator.FromOle(ForeColor) clrBackColor1 = DEFAULT_COLOR_BACK1 If BackColor1 <> -1 Then clrBackColor1 = ColorTranslator.FromOle(BackColor1) clrBackColor2 = DEFAULT_COLOR_BACK2 If BackColor2 <> -1 Then clrBackColor2 = ColorTranslator.FromOle(BackColor2) clrFillColor = DEFAULT_COLOR_FILL If FillColor <> -1 Then clrFillColor = ColorTranslator.FromOle(FillColor) clrBorderColor = DEFAULT_COLOR_BORDER If BorderColor <> -1 Then clrBorderColor = ColorTranslator.FromOle(BorderColor) ' Calculate the percent of the progressbar RangeDiff = Max - Min TheCaption = Format((Value - Min) / RangeDiff, "0%") ' Create the "Grahpics" object to work with objGraphics = TheControl.CreateGraphics ' Get the dimentions we need sngDevide = CSng(((Value - Min) / RangeDiff) * TheControl.Width) intModifier = 0 If ShowBorder = True Then intModifier = 1 ' Draw the lines on the PictureBox that make it look like a ProgressBar If GradientBackground = True Then objBrush = New LinearGradientBrush(New Rectangle(intModifier, intModifier, TheControl.Width - (intModifier * 2), TheControl.Height - (intModifier * 2)), clrBackColor1, clrBackColor2, GradientDirection) objGraphics.FillRectangle(CType(objBrush, Brush), intModifier, intModifier, sngDevide, TheControl.Height - (intModifier * 2)) Else objGraphics.FillRectangle(New SolidBrush(clrBackColor1), intModifier, intModifier, sngDevide, TheControl.Height - (intModifier * 2)) End If objGraphics.FillRectangle(New SolidBrush(clrFillColor), sngDevide + intModifier, intModifier, (TheControl.Width - (intModifier * 2)) - sngDevide, TheControl.Height - (intModifier * 2)) ' Draw the caption If ShowProgressCaption = True Then ' Get the height and width of the string TextSize = objGraphics.MeasureString(TheCaption, TextFont) ' Set the text location depending on where the user specified Select Case CaptionAlignment Case HorizontalAlignment.Center : sngX = (TheControl.Width / 2 - TextSize.Width / 2) Case HorizontalAlignment.Left : sngX = 1 Case HorizontalAlignment.Right : sngX = (TheControl.Width - TextSize.Width) - 1 End Select sngY = (TheControl.Height - TextSize.Height) / 2 ' Draw the text to the progressbar objGraphics.DrawString(TheCaption, TextFont, New SolidBrush(clrForeColor), sngX, sngY) End If ' Draw a border around the progressbar If ShowBorder = True Then objGraphics.DrawRectangle(New Pen(clrBorderColor), 0, 0, TheControl.Width - 1, TheControl.Height - 1) Exit Sub ErrorTrap: Debug.WriteLine("ERROR " & CStr(Err.Number) & " [" & Err.Source & "] " & Err.Description) End Sub #End Region End Module