如题。感谢回复。

解决方案 »

  1.   

    ListBox中恐怕无法实现你说的功能,建议用ListView来实现,如:
    Dim lstItem As ListItem
    With ListView1
            .ListItems.Add , "Key1", "111"
            .ListItems.Add , "Key2", "222"
            .ListItems.Add , "Key3", "333"
            .ListItems.Add , "Key4", "444"
        End With
        
        For Each lstItem In ListView1.ListItems
            If lstItem.Text = "111" Then
                lstItem.ForeColor = vbRed
                lstItem.Bold = True
            End If
        Next
      

  2.   

    用owner draw,好像网上有比较多这样例子的
      

  3.   

    'code in form
    Option Explicit
    Private Sub Form_Load()
        Dim I As Integer
        For I = 0 To 15
            'Load a List of 0 to 15 with the Item Data
            'Set to the QBColors 0 - 15
            List1.AddItem "Color " & I
            List1.itemData(List1.NewIndex) = QBColor(I)
        Next
        'Subclass the "Form", to Capture the Listbox Notification Messages
        SubLists hWnd
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        'Release the SubClassing, Very Import to Prevent Crashing!
        RemoveSubLists hWnd
    End Sub
      

  4.   

    'code in module
    Option ExplicitPrivate Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End TypePrivate Type DRAWITEMSTRUCT
            CtlType As Long
            CtlID As Long
            itemID As Long
            itemAction As Long
            itemState As Long
            hwndItem As Long
            hdc As Long
            rcItem As RECT
            itemData As Long
    End TypePrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
    Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As LongPrivate Const COLOR_HIGHLIGHT = 13
    Private Const COLOR_HIGHLIGHTTEXT = 14
    Private Const COLOR_WINDOW = 5
    Private Const COLOR_WINDOWTEXT = 8
    Private Const LB_GETTEXT = &H189
    Private Const WM_DRAWITEM = &H2B
    Private Const GWL_WNDPROC = (-4)
    Private Const ODS_FOCUS = &H10
    Private Const ODT_LISTBOX = 2Private lPrevWndProc As LongPrivate Function SubClassedList(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tItem As DRAWITEMSTRUCT
        Dim sBuff As String * 255
        Dim sItem As String
        Dim lBack As Long
        
        If Msg = WM_DRAWITEM Then
        
            'Redraw the listbox
            'This function only passes the Address of the DrawItem Structure, so we need to
            'use the CopyMemory API to Get a Copy into the Variable we setup:
            Call CopyMemory(tItem, ByVal lParam, Len(tItem))
            
            'Make sure we're dealing with a Listbox
            If tItem.CtlType = ODT_LISTBOX Then
            
                'Get the Item Text
                Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
                
                sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
                If (tItem.itemState And ODS_FOCUS) Then
                
                    'Item has Focus, Highlight it, I'm using the Default Focus
                    'Colors for this example.
                    lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
                    Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                    DrawFocusRect tItem.hdc, tItem.rcItem
                Else
                
                    'Item Doesn't Have Focus
                    'Create a Brush using the Color of the Listbox Window
                    lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW))
                    
                    'Paint the Item Area
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    
                    'Set the Text Colors, using the ForeColor specified in the ItemData of the Item
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
                    Call SetTextColor(tItem.hdc, tItem.itemData)
                    
                    'Display the Item Text
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                End If
                Call DeleteObject(lBack)
                
                'Don't Need to Pass a Value on as we've just handled the Message ourselves
                SubClassedList = 0
                Exit Function
                        
            End If
                
        End If
        SubClassedList = CallWindowProc(lPrevWndProc, hWnd, Msg, wParam, lParam)
    End FunctionPublic Sub SubLists(ByVal hWnd As Long)
        lPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClassedList)
    End SubPublic Sub RemoveSubLists(ByVal hWnd As Long)
        Call SetWindowLong(hWnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
      

  5.   

    'code in form
    Option Explicit
    Private Sub Form_Load()
        Dim I As Integer
        For I = 0 To 15
            'Load a List of 0 to 15 with the Item Data
            'Set to the QBColors 0 - 15
            List1.AddItem "Color " & I
            List1.itemData(List1.NewIndex) = QBColor(I)
        Next
        'Subclass the "Form", to Capture the Listbox Notification Messages
        SubLists hWnd
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        'Release the SubClassing, Very Import to Prevent Crashing!
        RemoveSubLists hWnd
    End Sub'code in module
    Option ExplicitPrivate Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End TypePrivate Type DRAWITEMSTRUCT
            CtlType As Long
            CtlID As Long
            itemID As Long
            itemAction As Long
            itemState As Long
            hwndItem As Long
            hdc As Long
            rcItem As RECT
            itemData As Long
    End TypePrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
    Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As LongPrivate Const COLOR_HIGHLIGHT = 13
    Private Const COLOR_HIGHLIGHTTEXT = 14
    Private Const COLOR_WINDOW = 5
    Private Const COLOR_WINDOWTEXT = 8
    Private Const LB_GETTEXT = &H189
    Private Const WM_DRAWITEM = &H2B
    Private Const GWL_WNDPROC = (-4)
    Private Const ODS_FOCUS = &H10
    Private Const ODT_LISTBOX = 2Private lPrevWndProc As LongPrivate Function SubClassedList(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tItem As DRAWITEMSTRUCT
        Dim sBuff As String * 255
        Dim sItem As String
        Dim lBack As Long
        
        If Msg = WM_DRAWITEM Then
        
            'Redraw the listbox
            'This function only passes the Address of the DrawItem Structure, so we need to
            'use the CopyMemory API to Get a Copy into the Variable we setup:
            Call CopyMemory(tItem, ByVal lParam, Len(tItem))
            
            'Make sure we're dealing with a Listbox
            If tItem.CtlType = ODT_LISTBOX Then
            
                'Get the Item Text
                Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
                
                sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
                If (tItem.itemState And ODS_FOCUS) Then
                
                    'Item has Focus, Highlight it, I'm using the Default Focus
                    'Colors for this example.
                    lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
                    Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                    DrawFocusRect tItem.hdc, tItem.rcItem
                Else
                
                    'Item Doesn't Have Focus
                    'Create a Brush using the Color of the Listbox Window
                    lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW))
                    
                    'Paint the Item Area
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    
                    'Set the Text Colors, using the ForeColor specified in the ItemData of the Item
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
                    Call SetTextColor(tItem.hdc, tItem.itemData)
                    
                    'Display the Item Text
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                End If
                Call DeleteObject(lBack)
                
                'Don't Need to Pass a Value on as we've just handled the Message ourselves
                SubClassedList = 0
                Exit Function
                        
            End If
                
        End If
        SubClassedList = CallWindowProc(lPrevWndProc, hWnd, Msg, wParam, lParam)
    End FunctionPublic Sub SubLists(ByVal hWnd As Long)
        lPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClassedList)
    End SubPublic Sub RemoveSubLists(ByVal hWnd As Long)
        Call SetWindowLong(hWnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
      

  6.   

    leolan(史留香) :    测试了一下你贴的代码,listbox的外观没有改变,还是白底黑字的。
      

  7.   

    94,94
    另外,何时才会触发WM_DRAWITEM消息?
      

  8.   

    lstItem.ForeColor = vbRed
                lstItem.Bold = True
      

  9.   

    To linjimu(ekek):
    lstItem.Bold = True???何解?有这属性吗?(VB5)
      

  10.   

    回复人: supergreenbean(超级绿豆) ( ) 信誉:100 用owner draw,好像网上有比较多这样例子的
    '-----------------------------------------------------同意,用自画控件的方法....
      

  11.   


        最好是能直接用API的方法解决。换控件的工作量太大了。
      

  12.   

    不用换控件,我们说的是用API写个钩子钩住那个控件的消息,然后自已处理像WM_Paint这样的消息的过程就OK了...