模块: Option ExplicitPublic Const NM_CUSTOMDRAW = (-12&) Public Const WM_NOTIFY As Long = &H4E&Public Const CDDS_PREPAINT As Long = &H1& Public Const CDRF_NOTIFYITEMDRAW As Long = &H20& Public Const CDDS_ITEM As Long = &H10000 Public Const CDDS_ITEMPREPAINT As Long = CDDS_ITEM Or CDDS_PREPAINT Public Const CDRF_NEWFONT As Long = &H2&Public Type NMHDR hWndFrom As Long ' Window handle of control sending message idFrom As Long ' Identifier of control sending message code As Long ' Specifies the notification code End Type' sub struct of the NMCUSTOMDRAW struct Public Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type' generic customdraw struct Public Type NMCUSTOMDRAW hdr As NMHDR dwDrawStage As Long hDC As Long rc As RECT dwItemSpec As Long uItemState As Long lItemlParam As Long End Type' listview specific customdraw struct Public Type NMLVCUSTOMDRAW nmcd As NMCUSTOMDRAW clrText As Long clrTextBk As Long ' if IE >= 4.0 this member of the struct can be used 'iSubItem As Integer End TypePublic g_addProcOld As Long Public g_MaxItems As Long
Public 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 Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Public Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case iMsg Case WM_NOTIFY Dim udtNMHDR As NMHDR CopyMemory udtNMHDR, ByVal lParam, 12& With udtNMHDR If .code = NM_CUSTOMDRAW Then Dim udtNMLVCUSTOMDRAW As NMLVCUSTOMDRAW CopyMemory udtNMLVCUSTOMDRAW, ByVal lParam, Len(udtNMLVCUSTOMDRAW) With udtNMLVCUSTOMDRAW.nmcd Select Case .dwDrawStage Case CDDS_PREPAINT WindowProc = CDRF_NOTIFYITEMDRAW Exit Function Case CDDS_ITEMPREPAINT If Val(Form1.ListView1.ListItems(.dwItemSpec + 1).Text) > 100 Then udtNMLVCUSTOMDRAW.clrText = vbBlack Else udtNMLVCUSTOMDRAW.clrText = vbRed End If 'I used Listitem.Tag property to set color, though you can use text etc. udtNMLVCUSTOMDRAW.clrTextBk = Val(Form1.ListView1.ListItems(.dwItemSpec + 1).Tag) CopyMemory ByVal lParam, udtNMLVCUSTOMDRAW, Len(udtNMLVCUSTOMDRAW) WindowProc = CDRF_NEWFONT Exit Function End Select End With End If End With End Select WindowProc = CallWindowProc(g_addProcOld, hWnd, iMsg, wParam, lParam) End Function窗体: Option Explicit Private Const GWL_WNDPROC As Long = (-4&) Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Sub Form_Load() With ListView1 .FullRowSelect = True .View = lvwReport .ColumnHeaders.Add , , "Item Column" .ColumnHeaders.Add , , "Subitem 1" .ColumnHeaders.Add , , "Subitem 2" Dim i& For i = 1 To 30 With .ListItems.Add(, , CStr(Int(200 * Rnd))) .SubItems(1) = "Subitem 1" .SubItems(2) = "Subitem 2" .Tag = CStr(QBColor(i Mod 15)) End With Next g_MaxItems = .ListItems.Count - 1 End With g_addProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc) End SubPrivate Sub Form_Unload(Cancel As Integer) Call SetWindowLong(hWnd, GWL_WNDPROC, g_addProcOld) End Sub
還沒有好好看的。明天再來答﹐這上面不少listview的例
Option ExplicitPublic Const NM_CUSTOMDRAW = (-12&)
Public Const WM_NOTIFY As Long = &H4E&Public Const CDDS_PREPAINT As Long = &H1&
Public Const CDRF_NOTIFYITEMDRAW As Long = &H20&
Public Const CDDS_ITEM As Long = &H10000
Public Const CDDS_ITEMPREPAINT As Long = CDDS_ITEM Or CDDS_PREPAINT
Public Const CDRF_NEWFONT As Long = &H2&Public Type NMHDR
hWndFrom As Long ' Window handle of control sending message
idFrom As Long ' Identifier of control sending message
code As Long ' Specifies the notification code
End Type' sub struct of the NMCUSTOMDRAW struct
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type' generic customdraw struct
Public Type NMCUSTOMDRAW
hdr As NMHDR
dwDrawStage As Long
hDC As Long
rc As RECT
dwItemSpec As Long
uItemState As Long
lItemlParam As Long
End Type' listview specific customdraw struct
Public Type NMLVCUSTOMDRAW
nmcd As NMCUSTOMDRAW
clrText As Long
clrTextBk As Long
' if IE >= 4.0 this member of the struct can be used
'iSubItem As Integer
End TypePublic g_addProcOld As Long
Public g_MaxItems As Long
Public 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
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case iMsg
Case WM_NOTIFY
Dim udtNMHDR As NMHDR
CopyMemory udtNMHDR, ByVal lParam, 12&
With udtNMHDR
If .code = NM_CUSTOMDRAW Then
Dim udtNMLVCUSTOMDRAW As NMLVCUSTOMDRAW
CopyMemory udtNMLVCUSTOMDRAW, ByVal lParam, Len(udtNMLVCUSTOMDRAW)
With udtNMLVCUSTOMDRAW.nmcd
Select Case .dwDrawStage
Case CDDS_PREPAINT
WindowProc = CDRF_NOTIFYITEMDRAW
Exit Function
Case CDDS_ITEMPREPAINT
If Val(Form1.ListView1.ListItems(.dwItemSpec + 1).Text) > 100 Then
udtNMLVCUSTOMDRAW.clrText = vbBlack
Else
udtNMLVCUSTOMDRAW.clrText = vbRed
End If
'I used Listitem.Tag property to set color, though you can use text etc.
udtNMLVCUSTOMDRAW.clrTextBk = Val(Form1.ListView1.ListItems(.dwItemSpec + 1).Tag)
CopyMemory ByVal lParam, udtNMLVCUSTOMDRAW, Len(udtNMLVCUSTOMDRAW)
WindowProc = CDRF_NEWFONT
Exit Function
End Select
End With
End If
End With
End Select
WindowProc = CallWindowProc(g_addProcOld, hWnd, iMsg, wParam, lParam)
End Function窗体:
Option Explicit
Private Const GWL_WNDPROC As Long = (-4&)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Sub Form_Load()
With ListView1
.FullRowSelect = True
.View = lvwReport
.ColumnHeaders.Add , , "Item Column"
.ColumnHeaders.Add , , "Subitem 1"
.ColumnHeaders.Add , , "Subitem 2"
Dim i&
For i = 1 To 30
With .ListItems.Add(, , CStr(Int(200 * Rnd)))
.SubItems(1) = "Subitem 1"
.SubItems(2) = "Subitem 2"
.Tag = CStr(QBColor(i Mod 15))
End With
Next
g_MaxItems = .ListItems.Count - 1
End With
g_addProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End SubPrivate Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(hWnd, GWL_WNDPROC, g_addProcOld)
End Sub
谢谢你的代码。这个代码很好,非常感谢!:)
不过,我需要的是listView中被选中时的蓝底色变色。不是背景色。
还有那个选中的蓝底色在失去焦点的颜色,不是变成了灰色了吗?怎样改变这个灰色呢?
或者你在CDDS_ITEMPOSTPAINT这个阶段自己DrawText也可以啊