|
-
May 26th, 2021, 04:25 PM
#1
Thread Starter
New Member
Change The Background Of Custom Progress Bar
Hello I've Took A Progress Bar From An Open Source Project, & I'm Trying To Change The Background For The Custom Control For My Personal Use, Currently The Color Is The Normal SystemControl Color For The Progress Bar, Any Help?
Code:
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.VisualStyles
Namespace UI
Public NotInheritable Class QualityProgressBar
Inherits Control
Public Sub New()
MyBase.New()
Me.DoubleBuffered = True
End Sub
Private m_nMinimum As Integer = 0
Function ColorToGrayscale(ByVal clr As Color) As Color
Dim l As Integer = CInt(((0.3F * clr.R) + (0.59F * clr.G) + (0.11F * clr.B)))
If l >= 256 Then l = 255
Return Color.FromArgb(l, l, l)
End Function
Function ColorMiddle(ByVal clrA As Color, ByVal clrB As Color) As Color
Return Color.FromArgb((CInt(clrA.A) + CInt(clrB.A)) / 2, (CInt(clrA.R) + CInt(clrB.R)) / 2, (CInt(clrA.G) + CInt(clrB.G)) / 2, (CInt(clrA.B) + CInt(clrB.B)) / 2)
End Function
<DefaultValue(0)>
Public Property Minimum As Integer
Get
Return m_nMinimum
End Get
Set(ByVal value As Integer)
m_nMinimum = value
Me.Invalidate()
End Set
End Property
Private m_nMaximum As Integer = 100
<DefaultValue(100)>
Public Property Maximum As Integer
Get
Return m_nMaximum
End Get
Set(ByVal value As Integer)
m_nMaximum = value
Me.Invalidate()
End Set
End Property
Private m_nPosition As Integer = 0
<DefaultValue(0)>
Public Property Value As Integer
Get
Return m_nPosition
End Get
Set(ByVal value As Integer)
m_nPosition = value
Me.Invalidate()
End Set
End Property
Private m_pbsStyle As ProgressBarStyle = ProgressBarStyle.Continuous
<Browsable(False)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property Style As ProgressBarStyle
Get
Return m_pbsStyle
End Get
Set(ByVal value As ProgressBarStyle)
m_pbsStyle = value
Me.Invalidate()
End Set
End Property
Public Function ShouldSerializeStyle() As Boolean
Return False
End Function
Private m_strText As String = String.Empty
<DefaultValue("")>
Public Property ProgressText As String
Get
Return m_strText
End Get
Set(ByVal value As String)
m_strText = value
Me.Invalidate()
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Try
PaintPriv(e)
Catch __unusedException1__ As Exception
Debug.Assert(False)
End Try
End Sub
Private Sub PaintPriv(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
If g Is Nothing Then
MyBase.OnPaint(e)
Return
End If
Dim nNormPos As Integer = m_nPosition - m_nMinimum
Dim nNormMax As Integer = m_nMaximum - m_nMinimum
If nNormMax <= 0 Then
Debug.Assert(False)
nNormMax = 100
End If
If nNormPos < 0 Then
Debug.Assert(False)
nNormPos = 0
End If
If nNormPos > nNormMax Then
Debug.Assert(False)
nNormPos = nNormMax
End If
Dim rectClient As Rectangle = Me.ClientRectangle
Dim rectDraw As Rectangle
Dim vse As VisualStyleElement = VisualStyleElement.ProgressBar.Bar.Normal
If VisualStyleRenderer.IsSupported AndAlso VisualStyleRenderer.IsElementDefined(vse) Then
Dim vsr As VisualStyleRenderer = New VisualStyleRenderer(vse)
If vsr.IsBackgroundPartiallyTransparent() Then vsr.DrawParentBackground(g, rectClient, Me)
vsr.DrawBackground(g, rectClient)
rectDraw = vsr.GetBackgroundContentRectangle(g, rectClient)
Else
g.FillRectangle(Brushes.White, rectClient)
Dim penGray As Pen = Pens.DarkGray
Dim penWhite As Pen = Pens.LightGray
g.DrawLine(penGray, 0, 0, rectClient.Width - 1, 0)
g.DrawLine(penGray, 0, 0, 0, rectClient.Height - 1)
g.DrawLine(penWhite, rectClient.Width - 1, 0, rectClient.Width - 1, rectClient.Height - 1)
g.DrawLine(penWhite, 0, rectClient.Height - 1, rectClient.Width - 1, rectClient.Height - 1)
rectDraw = New Rectangle(rectClient.X + 1, rectClient.Y + 1, rectClient.Width - 2, rectClient.Height - 2)
End If
Dim nDrawWidth As Integer = CInt((CSng(rectDraw.Width) * CSng(nNormPos) / CSng(nNormMax)))
Dim clrStart As Color = Color.FromArgb(255, 128, 0)
Dim clrEnd As Color = Color.FromArgb(0, 255, 0)
Dim clrMid As Color = Color.FromArgb(255, 255, 0)
If Not Me.Enabled Then
clrStart = ColorToGrayscale(Color.White)
clrEnd = ColorToGrayscale(Color.White)
clrMid = ColorMiddle(clrStart, clrEnd)
End If
Dim bRtl As Boolean = (Me.RightToLeft = RightToLeft.Yes)
If bRtl Then
Dim clrTemp As Color = clrStart
clrStart = clrEnd
clrEnd = clrTemp
End If
Dim rectGrad As Rectangle = New Rectangle(rectDraw.X, rectDraw.Y, rectDraw.Width, rectDraw.Height)
Using brush As LinearGradientBrush = New LinearGradientBrush(rectGrad, clrStart, clrEnd, LinearGradientMode.Horizontal)
Dim cb As ColorBlend = New ColorBlend()
cb.Colors = New Color(2) {clrStart, clrMid, clrEnd}
cb.Positions = New Single(2) {0.0F, 0.5F, 1.0F}
brush.InterpolationColors = cb
g.FillRectangle(brush, (If(bRtl, (rectDraw.Width - nDrawWidth + 1), rectDraw.Left)), rectDraw.Top, nDrawWidth, rectDraw.Height)
End Using
PaintText(g, rectDraw, bRtl)
End Sub
Private Sub PaintText(ByVal g As Graphics, ByVal rectDraw As Rectangle, ByVal bRtl As Boolean)
If String.IsNullOrEmpty(m_strText) Then Return
Dim f As Font = Me.Font
Dim clrFG As Color = ColorToGrayscale(Me.ForeColor)
Dim clrBG As Color = Color.FromArgb(clrFG.ToArgb() Xor &H20FFFFFF)
Dim dx As Integer = rectDraw.X
Dim dy As Integer = rectDraw.Y
Dim dw As Integer = rectDraw.Width
Dim dh As Integer = rectDraw.Height
Using br As SolidBrush = New SolidBrush(clrFG)
Dim sff As StringFormatFlags = (StringFormatFlags.FitBlackBox Or StringFormatFlags.NoClip)
If bRtl Then sff = sff Or StringFormatFlags.DirectionRightToLeft
Using sf As StringFormat = New StringFormat(sff)
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim rf As RectangleF = New RectangleF(dx, dy, dw, dh)
g.DrawString(m_strText, f, br, rf, sf)
End Using
End Using
End Sub
End Class
End Namespace
-
May 26th, 2021, 07:48 PM
#2
Re: Change The Background Of Custom Progress Bar
This seems to be the relevant part of the code:
Code:
Dim vse As VisualStyleElement = VisualStyleElement.ProgressBar.Bar.Normal
If VisualStyleRenderer.IsSupported AndAlso VisualStyleRenderer.IsElementDefined(vse) Then
Dim vsr As VisualStyleRenderer = New VisualStyleRenderer(vse)
If vsr.IsBackgroundPartiallyTransparent() Then vsr.DrawParentBackground(g, rectClient, Me)
vsr.DrawBackground(g, rectClient)
rectDraw = vsr.GetBackgroundContentRectangle(g, rectClient)
Else
g.FillRectangle(Brushes.White, rectClient)
Dim penGray As Pen = Pens.DarkGray
Dim penWhite As Pen = Pens.LightGray
g.DrawLine(penGray, 0, 0, rectClient.Width - 1, 0)
g.DrawLine(penGray, 0, 0, 0, rectClient.Height - 1)
g.DrawLine(penWhite, rectClient.Width - 1, 0, rectClient.Width - 1, rectClient.Height - 1)
g.DrawLine(penWhite, 0, rectClient.Height - 1, rectClient.Width - 1, rectClient.Height - 1)
rectDraw = New Rectangle(rectClient.X + 1, rectClient.Y + 1, rectClient.Width - 2, rectClient.Height - 2)
End If
The If block is using visual styles to draw like a standard ProgressBar. If you just comment out all but the contents of the Else block then you'll use that custom drawing code and you can modify that as you like.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|