各位大侠好!
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim datas As String() = sData
Dim rc As RectangleF = New RectangleF(e.Bounds.X + 3, e.Bounds.Y + 5, e.Bounds.Width - 5, e.Bounds.Height - 3)
Dim rc1 As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 5, e.Bounds.Height - 3)
Dim myBrush As Brush = Brushes.Black
e.DrawBackground()
'在选定项目上绘制选定背景色
'e.DrawFocusRectangle ( )
Dim sf As StringFormat = New StringFormat()
sf.Alignment = StringAlignment.Near
'设定 ListBox 中项目名称的在矩形区域的排列对齐方式
'e.Graphics.DrawRectangle(New Pen(New SolidBrush(System.Drawing.Color.White), 1), rc1)
'以线条粗度为1,黑色画笔对绘制ListBox中的每一个项目的边框
e.Graphics.DrawString(sData(e.Index), New Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), New SolidBrush(sColorData(e.Index)), rc, sf) Select Case ListBox1.Items(e.Index.ToString)
Case "关于"
myBrush = Brushes.White
Case "界面背景"
myBrush = Brushes.White
Case Else
myBrush = Brushes.Black
End Select e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, New Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), myBrush, rc, sf)
e.DrawFocusRectangle()
' 以指定的字体大小、类型、颜色绘制 ListBox 项目名称
end sub
结果成这样子:
主要的就是颜色改变不对。
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim datas As String() = sData
Dim rc As RectangleF = New RectangleF(e.Bounds.X + 3, e.Bounds.Y + 5, e.Bounds.Width - 5, e.Bounds.Height - 3)
Dim rc1 As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 5, e.Bounds.Height - 3)
Dim myBrush As Brush = Brushes.Black
e.DrawBackground()
'在选定项目上绘制选定背景色
'e.DrawFocusRectangle ( )
Dim sf As StringFormat = New StringFormat()
sf.Alignment = StringAlignment.Near
'设定 ListBox 中项目名称的在矩形区域的排列对齐方式
'e.Graphics.DrawRectangle(New Pen(New SolidBrush(System.Drawing.Color.White), 1), rc1)
'以线条粗度为1,黑色画笔对绘制ListBox中的每一个项目的边框
e.Graphics.DrawString(sData(e.Index), New Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), New SolidBrush(sColorData(e.Index)), rc, sf) Select Case ListBox1.Items(e.Index.ToString)
Case "关于"
myBrush = Brushes.White
Case "界面背景"
myBrush = Brushes.White
Case Else
myBrush = Brushes.Black
End Select e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, New Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), myBrush, rc, sf)
e.DrawFocusRectangle()
' 以指定的字体大小、类型、颜色绘制 ListBox 项目名称
end sub
结果成这样子:
主要的就是颜色改变不对。
Private sColorData() As Object = {System.Drawing.Color.Black, System.Drawing.Color.Black}
Private sColorData2() As Object = {System.Drawing.Color.White, System.Drawing.Color.White}
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
if (!selected)
{
lgb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);
}
else
{
lgb = new LinearGradientBrush(r, Color.Cyan, Color.White, LinearGradientMode.Horizontal);
}
加上这一句就好了!