このクラスは、System.Windows.Forms.Label(このコントロールに最適な機能セット)を継承します。
コントロールのサイズは、基本クラスのAutoSizeプロパティによって設定されます(十分である必要があります)が、DrawStringは、使用するフォントに応じて、少し広くまたは狭く描画します。フォントのサイズが大きいと、高さも間違っている可能性があります。
LadelとGraphicsは異なるレンダリングモードを使用していると思いますが、この違いを理解することはできません。
または、コードに問題がありますか?
Public Class LabelProgressBar
Inherits Label
Private dProgress As Double = 0.0
Private nBackAlpha As Byte = 64
Private stBarColor0 As Color = Color.Maroon
Private stBarColor1 As Color = Color.ForestGreen
Public Property BackAlpha As Byte
Get
Return nBackAlpha
End Get
Set(value As Byte)
If value <> nBackAlpha Then
nBackAlpha = value
Invalidate()
End If
End Set
End Property
Public Property BarColor0 As Color
Get
Return stBarColor0
End Get
Set(value As Color)
If value <> stBarColor0 Then
stBarColor0 = value
Invalidate()
End If
End Set
End Property
Public Property BarColor1 As Color
Get
Return stBarColor1
End Get
Set(value As Color)
If value <> stBarColor1 Then
stBarColor1 = value
Invalidate()
End If
End Set
End Property
Public Property Progress As Double
Get
Return dProgress
End Get
Set(value As Double)
If value <> dProgress Then
Dim fOld = InnerProgress
dProgress = value
If InnerProgress <> fOld Then Invalidate()
End If
End Set
End Property
Private ReadOnly Property InnerProgress As Single
Get
If dProgress < 0.0 Then Return 0.0
If dProgress > 1.0 Then Return 1.0
Return CSng(Progress)
End Get
End Property
Private Sub LabelProgressBar_PaddingChanged(sender As Object, e As EventArgs) Handles Me.PaddingChanged
Invalidate()
End Sub
Private Sub LabelProgressBar_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If Width - Padding.Left - Padding.Right > 0 AndAlso Height - Padding.Top - Padding.Bottom > 0 Then
e.Graphics.CompositingQuality = CompositingQuality.HighQuality
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
e.Graphics.Clear(BackColor)
PaintGradient(e.Graphics, e.ClipRectangle, 1.0, nBackAlpha)
PaintGradient(e.Graphics, e.ClipRectangle, InnerProgress, 255)
Dim stNonPadded = New RectangleF(e.ClipRectangle.Location, e.ClipRectangle.Size)
stNonPadded.X = Padding.Left
stNonPadded.Width -= Padding.Left + Padding.Right
stNonPadded.Y = Padding.Top
stNonPadded.Height -= Padding.Top + Padding.Bottom
Using objBrush = New SolidBrush(ForeColor)
Using objFormat = New StringFormat()
Select Case TextAlign
Case ContentAlignment.TopLeft, ContentAlignment.MiddleLeft, ContentAlignment.BottomLeft
objFormat.Alignment = StringAlignment.Near
Case ContentAlignment.TopCenter, ContentAlignment.MiddleCenter, ContentAlignment.BottomCenter
objFormat.Alignment = StringAlignment.Center
Case Else
objFormat.Alignment = StringAlignment.Far
End Select
objFormat.Trimming = If(AutoEllipsis, StringTrimming.EllipsisWord, StringTrimming.Character)
Select Case TextAlign
Case ContentAlignment.MiddleLeft, ContentAlignment.MiddleCenter, ContentAlignment.MiddleRight
Dim stDrawSize = e.Graphics.MeasureString(Text, Font, stNonPadded.Size, objFormat)
stNonPadded.Y += (stNonPadded.Height - stDrawSize.Height) / 2
Case ContentAlignment.BottomLeft, ContentAlignment.BottomCenter, ContentAlignment.BottomRight
Dim stDrawSize = e.Graphics.MeasureString(Text, Font, stNonPadded.Size, objFormat)
stNonPadded.Y += stNonPadded.Height - stDrawSize.Height
End Select
e.Graphics.DrawString(Text, Font, objBrush, stNonPadded, objFormat)
End Using
End Using
End If
End Sub
Private Sub LabelProgressBar_TextAlignChanged(sender As Object, e As EventArgs) Handles Me.TextAlignChanged
Invalidate()
End Sub
Private Sub PaintGradient(surface As Graphics, bounds As Rectangle, progress As Single, alpha As Byte)
Dim stColor0 = Color.FromArgb(alpha, stBarColor0)
Dim stColor1 = Color.FromArgb(alpha, stBarColor1)
Using objBrush = New LinearGradientBrush(bounds, stColor0, stColor1, LinearGradientMode.Horizontal)
surface.FillRectangle(objBrush, New RectangleF(bounds.Left, bounds.Top, bounds.Width * progress, bounds.Height))
End Using
End Sub
End Class
ラベルに巾着の仕事をさせてみませんか?
Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
MyBase.OnPaintBackground(e)
If Width - Padding.Left - Padding.Right > 0 AndAlso Height - Padding.Top - Padding.Bottom > 0 Then
e.Graphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
e.Graphics.Clear(BackColor)
PaintGradient(e.Graphics, e.ClipRectangle, 1.0, nBackAlpha)
PaintGradient(e.Graphics, e.ClipRectangle, InnerProgress, 255)
End If
End Sub
MeasureString / DrawStringは、予想よりも少し複雑です。長方形の引数を持つDrawStringを使用すると、最後の文字が削除された(レンダリングされていない)ことがわかります。この行を使用することでこれを回避できます
e.Graphics.DrawString(Text, Font, objBrush, stNonPadded.Location, objFormat)
しかし、あなたの説明があまり明確ではなかったので、それがあなたの問題であるかどうかはわかりません。
また、ClipRectangleに従って整列することは、フォームが移動しているとき、非表示になっているとき、部分的に非表示になっているとき、画面の外に移動しているときなど、コントロールの半分になる可能性があるため、お勧めできません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加