Hi @ all,
ich habe mich kürzlich mal mit dem Thema GDI/GDI+ beschäftigt und mir testweise eine neue Progressbar geschrieben, diese funktioniert soweit ich dies beeurteilen kann sehr gut, aber naja ich Anfänger, Ihr Profis
daher wollte ich nur Fragen / darum bitten ob Ihr euch meinen Code einmal anschauen/tesen wollt, und mir Verbesserungsvorschläge gebt, also in die Richtung das könnt eman besser gestalten oder das ist schlecht gelöst,
damit ich weiss was ich zukünftig daran verbessern könnte.
Danke und Greets
Spoiler anzeigen
ich habe mich kürzlich mal mit dem Thema GDI/GDI+ beschäftigt und mir testweise eine neue Progressbar geschrieben, diese funktioniert soweit ich dies beeurteilen kann sehr gut, aber naja ich Anfänger, Ihr Profis
daher wollte ich nur Fragen / darum bitten ob Ihr euch meinen Code einmal anschauen/tesen wollt, und mir Verbesserungsvorschläge gebt, also in die Richtung das könnt eman besser gestalten oder das ist schlecht gelöst,
damit ich weiss was ich zukünftig daran verbessern könnte.
Danke und Greets
Quellcode
- Public Class DKProgress
- Public Sub New()
- InitializeComponent()
- Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
- Me.UpdateStyles()
- End Sub
- #Region "Deklaration"
- Public Event BarValueChanged()
- Public Event BarValueMinReached()
- Public Event BarValueMaxReached()
- Private BarValueVal As Double = 0
- Private BarValueMinVal As Double = 0
- Private BarValueMaxVal As Double = 100
- Private BarRightToLeftVal As Boolean = False
- Private BarEffectBorderVal As Boolean = True
- Private BarEffectGlassVal As Boolean = False
- Public Enum BarColorVersionEnum : Singlecolor : Gradient : End Enum
- Private BarColorVersionVal As BarColorVersionEnum = BarColorVersionEnum.Singlecolor
- Private BarColorSingleVal As Color = Color.DarkRed
- Private BarColorGradient1Val As Color = Color.LimeGreen
- Private BarColorGradient2Val As Color = Color.MidnightBlue
- Private BarColorBorderVal As Color = Color.Black
- Private BarColorGradientModeVal As LinearGradientMode = LinearGradientMode.Vertical
- Private BarColorGradientXOffsetVal As Integer = 0
- Private BarColorGradientYOffsetVal As Integer = 0
- Public BarQualitySmoothingModeVal As SmoothingMode = SmoothingMode.HighQuality
- Private BarQualityInterpolationModeVal As InterpolationMode = InterpolationMode.HighQualityBicubic
- Private BarQualityCompositingVal As CompositingQuality = CompositingQuality.HighQuality
- Private BarImageBarVal As Boolean = False
- Private BarImageVal As Bitmap
- Private LabelVal As Boolean = True
- Private LabelShowUpperValueVal As Boolean = False
- Private LabelDropShadowVal As Boolean = False
- Private LabelDropShadowXOffsetVal As Integer = 1
- Private LabelDropShadowYOffsetVal As Integer = 1
- Private LabelTextFontVal As Font = Me.Font
- Private LabelTextXOffsetVal As Integer = 0
- Private LabelTextYOffsetVal As Integer = 0
- Private LabelTextAdditionalVal As String = " %"
- Private LabelTextDelimeterVal As String = " / "
- Public Enum LabelTextAlignEnum : Left : Center : Right : End Enum
- Private LabelTextAlignVal As LabelTextAlignEnum = LabelTextAlignEnum.Center
- Public Enum LabelTextVersionEnum : Percent : Value : End Enum
- Private LabelTextVersionVal As LabelTextVersionEnum = LabelTextVersionEnum.Percent
- Private LabelColorSingleVal As Color = Color.Black
- Private LabelColorGradient1Val As Color = Color.LimeGreen
- Private LabelColorGradient2Val As Color = Color.MidnightBlue
- Private LabelColorDropShadowVal As Color = Color.Black
- Private LabelColorGradientModeVal As LinearGradientMode = LinearGradientMode.Vertical
- Public Enum LabelColorVersionEnum : Singlecolor : Gradient : End Enum
- Private LabelColorVersionVal As LabelColorVersionEnum = LabelColorVersionEnum.Singlecolor
- Private LabelTextQualityVal As TextRenderingHint = TextRenderingHint.ClearTypeGridFit
- #End Region
- #Region "Property get-set"
- Public Property BarValue As Double
- Get
- Return BarValueVal
- End Get
- Set(value As Double)
- If value < BarValueMinVal Then value = BarValueMinVal Else If value > BarValueMaxVal Then value = BarValueMaxVal
- BarValueVal = value
- RaiseEvent BarValueChanged()
- If BarValueVal = BarValueMinVal Then RaiseEvent BarValueMinReached() Else If BarValueVal = BarValueMaxVal Then RaiseEvent BarValueMaxReached()
- Me.Invalidate()
- End Set
- End Property
- Public Property BarValueMinimum As Double
- Get
- Return BarValueMinVal
- End Get
- Set(value As Double)
- If value > BarValueMaxVal Then value = BarValueMaxVal - 1
- BarValueMinVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarValueMaximum As Double
- Get
- Return BarValueMaxVal
- End Get
- Set(value As Double)
- If value < BarValueMinVal Then value = BarValueMinVal + 1
- BarValueMaxVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarRightToLeft As Boolean
- Get
- Return BarRightToLeftVal
- End Get
- Set(value As Boolean)
- BarRightToLeftVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarEffectBorder As Boolean
- Get
- Return BarEffectBorderVal
- End Get
- Set(value As Boolean)
- BarEffectBorderVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarEffectGlass As Boolean
- Get
- Return BarEffectGlassVal
- End Get
- Set(value As Boolean)
- BarEffectGlassVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorSingle As Color
- Get
- Return BarColorSingleVal
- End Get
- Set(value As Color)
- BarColorSingleVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorGradient1 As Color
- Get
- Return BarColorGradient1Val
- End Get
- Set(value As Color)
- BarColorGradient1Val = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorGradient2 As Color
- Get
- Return BarColorGradient2Val
- End Get
- Set(value As Color)
- BarColorGradient2Val = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorBorder As Color
- Get
- Return BarColorBorderVal
- End Get
- Set(value As Color)
- BarColorBorderVal = value
- BarEffectBorder = True
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorVersion() As BarColorVersionEnum
- Get
- Return BarColorVersionVal
- End Get
- Set(value As BarColorVersionEnum)
- BarColorVersionVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorGradientMode() As LinearGradientMode
- Get
- Return BarColorGradientModeVal
- End Get
- Set(value As LinearGradientMode)
- BarColorGradientModeVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorGradientXOffset As Integer
- Get
- Return BarColorGradientXOffsetVal
- End Get
- Set(value As Integer)
- BarColorGradientXOffsetVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarColorGradientYOffset As Integer
- Get
- Return BarColorGradientYOffsetVal
- End Get
- Set(value As Integer)
- BarColorGradientYOffsetVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarQualityInterpolationMode As InterpolationMode
- Get
- Return BarQualityInterpolationModeVal
- End Get
- Set(value As InterpolationMode)
- BarQualityInterpolationModeVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarQualityCompositing As CompositingQuality
- Get
- Return BarQualityCompositingVal
- End Get
- Set(value As CompositingQuality)
- BarQualityCompositingVal = value
- Me.Invalidate()
- End Set
- End Property
- Private Property BarWithImage As Boolean
- Get
- Return BarImageBarVal
- End Get
- Set(value As Boolean)
- BarImageBarVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property BarImage As Bitmap
- Get
- Return BarImageVal
- End Get
- Set(value As Bitmap)
- If Not value Is Nothing Then
- BarImageVal = value
- Me.Invalidate()
- BarWithImage = True
- Else
- BarImageVal = Nothing
- Me.Invalidate()
- BarWithImage = False
- End If
- End Set
- End Property
- Public Property Label As Boolean
- Get
- Return LabelVal
- End Get
- Set(value As Boolean)
- LabelVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelShowUpperValue As Boolean
- Get
- Return LabelShowUpperValueVal
- End Get
- Set(value As Boolean)
- LabelShowUpperValueVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelDropShadow As Boolean
- Get
- Return LabelDropShadowVal
- End Get
- Set(value As Boolean)
- LabelDropShadowVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelDropShadowXOffset As Integer
- Get
- Return LabelDropShadowXOffsetVal
- End Get
- Set(value As Integer)
- LabelDropShadowXOffsetVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelDropShadowYOffset As Integer
- Get
- Return LabelDropShadowYOffsetVal
- End Get
- Set(value As Integer)
- LabelDropShadowYOffsetVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelColorVersion() As LabelColorVersionEnum
- Get
- Return LabelColorVersionVal
- End Get
- Set(value As LabelColorVersionEnum)
- LabelColorVersionVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelColorSingle As Color
- Get
- Return LabelColorSingleVal
- End Get
- Set(value As Color)
- LabelColorSingleVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelColorGradient1 As Color
- Get
- Return LabelColorGradient1Val
- End Get
- Set(value As Color)
- LabelColorGradient1Val = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelColorGradient2 As Color
- Get
- Return LabelColorGradient2Val
- End Get
- Set(value As Color)
- LabelColorGradient2Val = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelColorDropShadow As Color
- Get
- Return LabelColorDropShadowVal
- End Get
- Set(value As Color)
- LabelColorDropShadowVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelColorGradientMode() As LinearGradientMode
- Get
- Return LabelColorGradientModeVal
- End Get
- Set(value As LinearGradientMode)
- LabelColorGradientModeVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextXOffset As Integer
- Get
- Return LabelTextXOffsetVal
- End Get
- Set(value As Integer)
- LabelTextXOffsetVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextYOffset As Integer
- Get
- Return LabelTextYOffsetVal
- End Get
- Set(value As Integer)
- LabelTextYOffsetVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextDelimeter As String
- Get
- Return LabelTextDelimeterVal
- End Get
- Set(value As String)
- LabelTextDelimeterVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextAdditional As String
- Get
- Return LabelTextAdditionalVal
- End Get
- Set(value As String)
- LabelTextAdditionalVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextFont As Font
- Get
- Return LabelTextFontVal
- End Get
- Set(value As Font)
- LabelTextFontVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextAlign() As LabelTextAlignEnum
- Get
- Return LabelTextAlignVal
- End Get
- Set(value As LabelTextAlignEnum)
- LabelTextAlignVal = value
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextVersion() As LabelTextVersionEnum
- Get
- Return LabelTextVersionVal
- End Get
- Set(value As LabelTextVersionEnum)
- LabelTextVersionVal = value
- If value = LabelTextVersionEnum.Value AndAlso LabelTextAdditional = " %" Then LabelTextAdditional = " V"
- If value = LabelTextVersionEnum.Percent AndAlso LabelTextAdditional = " V" Then LabelTextAdditional = " %"
- Me.Invalidate()
- End Set
- End Property
- Public Property LabelTextQuality() As TextRenderingHint
- Get
- Return LabelTextQualityVal
- End Get
- Set(value As TextRenderingHint)
- LabelTextQualityVal = value
- Me.Invalidate()
- End Set
- End Property
- #End Region
- #Region "Drawing"
- Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
- With e.Graphics
- .TextRenderingHint = LabelTextQualityVal
- .SmoothingMode = BarQualitySmoothingModeVal
- .InterpolationMode = BarQualityInterpolationModeVal
- .CompositingQuality = BarQualityCompositingVal
- End With
- Dim Rect As New Rectangle(-1, -1, Me.Width + 1, Me.Height + 1)
- Dim percent As Decimal = CDec((BarValue - BarValueMinVal) / (BarValueMaxVal - BarValueMinVal))
- Rect.Width = CInt(Rect.Width * percent)
- If BarWithImage Then
- Dim TBrush As New TextureBrush(BarImageVal)
- If BarRightToLeftVal Then
- e.Graphics.FillRectangle(TBrush, New Rectangle(Me.Width - Rect.Width, Rect.Y, Rect.Width, Rect.Height))
- Else
- e.Graphics.FillRectangle(TBrush, New Rectangle(Rect.X, Rect.Y, Rect.Width, Rect.Height))
- End If
- TBrush.Dispose()
- Else
- If BarColorVersionVal = LabelColorVersionEnum.Gradient Then
- Dim LGBBrush As New LinearGradientBrush(New Rectangle(0, 0, Me.Width + BarColorGradientXOffsetVal, Me.Height + BarColorGradientYOffsetVal), BarColorGradient1Val, BarColorGradient2Val, BarColorGradientModeVal)
- If BarRightToLeftVal Then
- e.Graphics.FillRectangle(LGBBrush, New Rectangle(Me.Width - Rect.Width, Rect.Y, Rect.Width, Rect.Height))
- Else
- e.Graphics.FillRectangle(LGBBrush, Rect)
- End If
- LGBBrush.Dispose()
- Else
- Dim SCBrush As New SolidBrush(BarColorSingleVal)
- If BarRightToLeftVal Then
- e.Graphics.FillRectangle(SCBrush, New Rectangle(Me.Width - Rect.Width, Rect.Y, Rect.Width, Rect.Height))
- Else
- e.Graphics.FillRectangle(SCBrush, Rect)
- End If
- SCBrush.Dispose()
- End If
- End If
- If BarEffectGlassVal Then
- Dim BGEBrush As New LinearGradientBrush(New Rectangle(0, 0, Me.Width, CInt(Me.Height / 2)), Color.FromArgb(200, 255, 255, 255), Color.FromArgb(30, 255, 255, 255), LinearGradientMode.Vertical)
- If BarRightToLeftVal Then
- e.Graphics.FillRectangle(BGEBrush, New Rectangle(Me.Width - Rect.Width, Rect.Y, Rect.Width, CInt(Rect.Height / 2)))
- Else
- e.Graphics.FillRectangle(BGEBrush, New Rectangle(Rect.X, Rect.Y, Rect.Width, CInt(Rect.Height / 2)))
- End If
- BGEBrush.Dispose()
- End If
- If LabelVal Then
- Dim LabelString As String = GetFinalLabelString()
- Dim LabelStringSize As SizeF = e.Graphics.MeasureString(LabelString, LabelTextFontVal)
- Dim LabelPosition As PointF = GetFinalLabelPosition(LabelStringSize)
- If LabelColorVersionVal = LabelColorVersionEnum.Gradient Then
- If LabelDropShadowVal Then
- Dim LSCShadowBrush As New SolidBrush(LabelColorDropShadowVal)
- e.Graphics.DrawString(LabelString, LabelTextFontVal, LSCShadowBrush, LabelPosition.X + LabelDropShadowXOffsetVal, LabelPosition.Y + LabelDropShadowYOffsetVal, StringFormat.GenericTypographic)
- LSCShadowBrush.Dispose()
- End If
- Dim LLGBBrush As New LinearGradientBrush(New Rectangle(0, 0, CInt(LabelStringSize.Width), Me.Height), LabelColorGradient1Val, LabelColorGradient2Val, LabelColorGradientModeVal)
- e.Graphics.DrawString(LabelString, LabelTextFontVal, LLGBBrush, LabelPosition, StringFormat.GenericTypographic)
- LLGBBrush.Dispose()
- Else
- If LabelDropShadowVal Then
- Dim LSCShadowBrush As New SolidBrush(LabelColorDropShadowVal)
- e.Graphics.DrawString(LabelString, LabelTextFontVal, LSCShadowBrush, LabelPosition.X + LabelDropShadowXOffsetVal, LabelPosition.Y + LabelDropShadowYOffsetVal, StringFormat.GenericTypographic)
- LSCShadowBrush.Dispose()
- End If
- Dim LSCBrush As New SolidBrush(LabelColorSingleVal)
- e.Graphics.DrawString(LabelString, LabelTextFontVal, LSCBrush, LabelPosition, StringFormat.GenericTypographic)
- LSCBrush.Dispose()
- End If
- End If
- If BarEffectBorderVal Then
- Dim BBBrush As New SolidBrush(BarColorBorderVal)
- Dim BBPen As New Pen(BBBrush)
- e.Graphics.DrawRectangle(BBPen, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
- BBPen.Dispose()
- BBBrush.Dispose()
- End If
- End Sub
- #End Region
- #Region "Private Subs/Functions"
- Private Function GetFinalLabelString() As String
- Dim LabelString As String = ""
- If LabelTextVersionVal = LabelTextVersionEnum.Percent Then
- If LabelShowUpperValueVal Then
- LabelString = CInt(CDec((BarValue - BarValueMinVal) / (BarValueMaxVal - BarValueMinVal)) * 100).ToString & LabelTextDelimeterVal & "100" & LabelTextAdditionalVal
- Else
- LabelString = CInt(CDec((BarValue - BarValueMinVal) / (BarValueMaxVal - BarValueMinVal)) * 100).ToString & LabelTextAdditionalVal
- End If
- Else
- If LabelShowUpperValueVal Then
- LabelString = BarValue.ToString & LabelTextDelimeterVal & BarValueMaximum & LabelTextAdditionalVal
- Else
- LabelString = BarValue.ToString & LabelTextAdditionalVal
- End If
- End If
- Return LabelString
- End Function
- Private Function GetFinalLabelPosition(ByVal LabelStringSize As SizeF) As PointF
- Dim LabelPosition As PointF
- If LabelTextAlignVal = LabelTextAlignEnum.Left Then
- LabelPosition = New PointF(5 + LabelTextXOffsetVal, CInt(((Me.Height / 2) - (LabelStringSize.Height / 2))) + LabelTextYOffsetVal)
- ElseIf LabelTextAlignVal = LabelTextAlignEnum.Center Then
- LabelPosition = New PointF(CInt(((Me.Width / 2) - (LabelStringSize.Width / 2)) + LabelTextXOffsetVal), CInt(((Me.Height / 2) - (LabelStringSize.Height / 2))) + LabelTextYOffsetVal)
- Else ' Right
- LabelPosition = New PointF(((Me.Width - 5) - LabelStringSize.Width) + LabelTextXOffsetVal, CInt(((Me.Height / 2) - (LabelStringSize.Height / 2))) + LabelTextYOffsetVal)
- End If
- Return LabelPosition
- End Function
- #End Region
- #Region "Public Subs/functions"
- ''' <summary>
- ''' Adds the Value to the ProgressValue
- ''' </summary>
- ''' <param name="Value">As Double, added to the ProgressValue</param>
- Public Sub PerformValueStep(ByVal Value As Double)
- Me.BarValue += Value
- End Sub
- ''' <summary>
- ''' Calculates how much have to be Added to the ProgressValue based on how much Percent you want to be added.
- ''' and adds this to the ProgressValue
- ''' </summary>
- ''' <param name="Value">As Double - Percent</param>
- Public Sub PerformPercentStep(ByVal Value As Double)
- Me.BarValue += ((BarValueMaxVal / 100) * Value)
- End Sub
- #End Region
- End Class
If Energy = Low Then
Drink(aHugeCoffee)
Else
Drink(aHugeCoffeeToo)
End If
Drink(aHugeCoffee)
Else
Drink(aHugeCoffeeToo)
End If
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „asusdk“ ()