ListBox 如何改变某行的字体颜色
Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type DRAWITEMSTRUCT CtlType As Long '控件类型 CtlID As Long '控件ID itemID As Long '菜单项、列表框或组合框中某一项的索引值 itemAction As Long '控件行为 itemState As Long '控件状态 hwndItem As Long '父窗口句柄或菜单句柄 hdc As Long '控件对应的绘图设备句柄 rcItem As RECT '控件所占据的矩形区域 itemData As Long '列表框或组合框中某一项的值 End Type Private 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 Long Private 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 = 2 Private lPrevWndProc As Long Private 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 '绘制菜单消息 Call CopyMemory(tItem, ByVal lParam, Len(tItem)) If tItem.CtlType = ODT_LISTBOX Then '只处理控件类型为listbox的控件 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 '判断某项是否具有焦点 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 '如果没有焦点,则 lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW)) Call FillRect(tItem.hdc, tItem.rcItem, lBack) Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW)) Call SetTextColor(tItem.hdc, tItem.itemData) TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem) End If Call DeleteObject(lBack) SubClassedList = 0 Exit Function End If End If SubClassedList = CallWindowProc(lPrevWndProc, hWnd, Msg, wParam, lParam) End Function Public Sub SubLists(ByVal hWnd As Long) lPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClassedList) End Sub Public Sub RemoveSubLists(ByVal hWnd As Long) Call SetWindowLong(hWnd, GWL_WNDPROC, lPrevWndProc) End Sub '窗体中 : Private Sub Form_Load() Dim I As Integer For I = 0 To 15 List1.AddItem "Color " & I List2.AddItem "FDSF" Next SubLists hWnd End Sub Private Sub Form_Unload(Cancel As Integer) RemoveSubLists hWnd End Sub '如果你想让list1的第五行的字体颜色为红色,则 List1.itemData(4) = RGB(255, 0, 0) List1.Refresh 'listbox的style设置为checkbox sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1) '改成Trim(sBuff)即可显示全部中文。 Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW)) Call SetTextColor(tItem.hdc, tItem.itemData) '这里是改背景和字体颜色的地方。