1 Attachment(s)
[RESOLVED] Blurry Text From DrawItem Event in ListBox
Hello,
I've successfully changed the color of a Listbox using the DrawItem event. The text is blurry, however, like it was stretched to fill the rectangle. I attached a pic with the listbox on the left using DrawMode as OwnerFixed(therefore firing the DrawItem event) and the one on the right with DrawMode set to Normal. The right text is crisper and and more attractive while the left has that stretched out look to it. I don't believe the image shoes my issue well, because the effect isn't startling, just annoying.
Attachment 177572
Here is my code for the DrawItem event for the left Listbox.
Code:
Private Sub lbRosterLeft_DrawItem(sender As Object, e As DrawItemEventArgs) Handles lbRosterLeft.DrawItem
If e.Index = -1 Then Return
Dim LB as Listbox = Directcast(sender, Listbox)
e.DrawBackground()
Dim myBackColor As Color = Color.White
Dim myForeColor As Color = Color.Black
Dim Player As ClPlayer = m_League.GetProPlayerFromKey(m_StoredPlayerKeys(e.Index), -1)
If Player.Injury.Count > 0 Then
myForeColor = Color.Red
For Each I As ClInjury In Player.Injury
If I.InjuredReserveDays > 0 Then myForeColor = Color.DarkRed
Next
ElseIf Player.OffFieldIssue.CurrentlySuspended = True Then
myForeColor = Color.Gainsboro
Else : myForeColor = Color.Black
End If
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
myBackColor = Color.CornflowerBlue
myForeColor = Color.White
End If
Dim myBackgroundBrush = New SolidBrush(myBackColor)
e.Graphics.FillRectangle(myBackgroundBrush, e.Bounds)
Dim myForegroundBrush = New SolidBrush(myForeColor)
e.Graphics.DrawString(LB.Items(e.Index).ToString(), e.Font, myForegroundBrush, e.Bounds.X, e.Bounds.Y)
e.DrawFocusRectangle()
End Sub
Re: Blurry Text From DrawItem Event in ListBox
Try setting the Graphics.TextRenderingHint property to AntiAliasGridFit or ClearTypeGridfit before calling DrawString. You may also wish to experiment with the Graphics.TextContrast property (value 0 to 12, default 4) according to the black or white background; but I haven't tried it myself.
BB
Re: Blurry Text From DrawItem Event in ListBox
Thank you!
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit made it 90% better. I'll try the TextContrast too to perhaps make it even better.
I appreciate your help!
Eric
Re: [RESOLVED] Blurry Text From DrawItem Event in ListBox