我要求改变一些组合框的背面颜色。
为实现这一目标,我做了以下事情:
Private Sub cbo_DrawItem(sender As Object, e As DrawItemEventArgs) Handles cboRooms.DrawItem
Try
If e.Index = 0 Then
e.DrawBackground()
' Get the item text
Dim text As String = DirectCast(sender, ComboBox).Items(e.Index).ToString()
Dim brush As Brush
brush = Brushes.Black
e.Graphics.DrawString(text, (DirectCast(sender, Control)).Font, brush, e.Bounds.X, e.Bounds.Y)
Else
If (e.Index > 0 AndAlso DirectCast(sender, ComboBox).Items(e.Index) IsNot Nothing) Then
' Draw the background
e.DrawBackground()
Dim combo As ComboBox = DirectCast(sender, ComboBox)
' Get the item text
Dim text As String = combo.Items(e.Index).ToString()
Dim brush As Brush
If (DirectCast(DirectCast(sender, ComboBox).Items(e.Index), GenericListItem(Of objName)).Value.IsExists) Then
brush = Brushes.Yellow
e.Graphics.FillRectangle(brush, e.Bounds)
e.Graphics.DrawString(text, (DirectCast(sender, Control)).Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
Else
brush = Brushes.Black
e.Graphics.DrawString(text, (DirectCast(sender, Control)).Font, brush, e.Bounds.X, e.Bounds.Y)
End If
' Determine the forecolor based on whether or not the item is selected
'If (e.Index = combo.SelectedIndex) Then
' brush = Brushes.White
' 'e.Graphics.FillRectangle(brush, e.Bounds)
' e.Graphics.DrawString(text, (DirectCast(sender, Control)).Font, brush, e.Bounds.X, e.Bounds.Y)
'End If
' Draw the text
End If
End If
Catch ex As Exception
End Try
问题:
我能通过上面的东西把颜色变回黄色。但是,当我选择/鼠标悬停在任何项目上时,它的前色不会变为白色,而是保持黑色。因此,由于突出显示的颜色(鼠标悬停/所选项目)为蓝色,无法清楚地读取组合框项目。
此外,对于背面颜色发生变化的项目,当鼠标悬停/选择项目时没有任何效果,它只显示黄色背面颜色而没有应用焦点颜色。
任何人都可以为我提供以上几点的解决方案吗?
谢谢
答案 0 :(得分:0)
不是100%确定问题是什么,但听起来您只需要在鼠标当前突出显示项目时绘制突出显示的文本:
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds.Location, _
SystemColors.HighlightText)
Else
If DirectCast(sender, ComboBox).Items(e.Index).ToString = "ccc" Then
e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds)
TextRenderer.DrawText(e.Graphics, text, e.Font, _
e.Bounds.Location, Color.Black)
Else
e.Graphics.FillRectangle(Brushes.Black, e.Bounds)
TextRenderer.DrawText(e.Graphics, text, e.Font, _
e.Bounds.Location, Color.White)
End If
End If