Rechercher dans ce blog

mercredi 29 décembre 2021

PorgressbarInverser

 Voici comment inverser de couleur par rapport au texte dans un progressbar.


vous pouvez changer de couleur dans backcolor et forecolor.

ma classes :

Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Runtime.InteropServices

Public Class ProgressbarInvertText : Inherits ProgressBar

    Public _MyFont As Font
    Public Property MyFont As Font
        Get
            Return _MyFont
        End Get
        Set(value As Font)
            _MyFont = value
            MyBase.Invalidate()
        End Set
    End Property

    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
        Me.Size = New Size(100, 21)
        Me.ForeColor = Color.Lime
        Me.BackColor = Color.Black
        Me._MyFont = New Font("Times New Roman", 12, FontStyle.Regular)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics = e.Graphics
        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        Dim r As Rectangle = Me.ClientRectangle
        Using b As New SolidBrush(Me.BackColor)
            g.FillRectangle(b, r)
        End Using
        Dim sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
        g.DrawString(Value & " %", _MyFont, New SolidBrush(Me.ForeColor), Me.ClientRectangle, sf)
        Dim hdc As Integer = g.GetHdc()
        Dim hBrush As IntPtr = CreateSolidBrush(ColorTranslator.ToWin32(Me.ForeColor))
        Dim phbrush As IntPtr = SelectObject(hdc, hBrush)
        PatBlt(hdc, r.Left, r.Y, CInt(Value * r.Width / Maximum - Minimum), r.Height, PATINVERT)
        SelectObject(hdc, phbrush)
        DeleteObject(hBrush)
        g.ReleaseHdc(hdc)
    End Sub

    Public Const PATINVERT As Integer = &H5A0049
    <DllImport("gdi32.dll")>
    Public Shared Function PatBlt(ByVal hdc As IntPtr, ByVal nXLeft As Integer, ByVal nYLeft As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal dwRop As Integer) As Boolean
    End Function
    <DllImport("gdi32.dll")>
    Public Shared Function SelectObject(ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
    End Function
    <DllImport("gdi32.dll", EntryPoint:="DeleteObject")>
    Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    End Function
    <DllImport("gdi32.dll")>
    Public Shared Function CreateSolidBrush(ByVal crColor As Integer) As IntPtr
    End Function
End Class

Aucun commentaire:

Enregistrer un commentaire