请问怎么改变 ListView 中指定行(能细化到列最好)的字体和背景颜色,就是可以在填写完数据后,使用函数来改变指定行的颜色?
解决方案 »
- 求一段vb代码,望高手赐教!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 关于读取网页源文件的问题
- 初学VB,请大家帮个忙.
- 压缩控件 //////////////////////////////////////////// 在线等待
- VB打包的程序安装到最后出现"路径/文件访问错误",是咋回事。
- vb写入的Txt文件为什么C#不能读
- 一个小小的问题。。。
- 一个关于API的问题!
- 怎么把注册控件这样的过程作成程序?给高分!
- 哪一位高手知道ActiveReport有打印一个或者全部页面终了时触发的事件吗?
- 当系统设置隐藏扩展名时,如何取得文件的扩展名?
- 小弟公司進來一位新人,略懂一點兒VB,老大想要我給他進行一下培訓,各位大哥有沒有這方面的培訓資料啊?
http://community.csdn.net/Expert/topic/3470/3470020.xml?temp=.4551355
http://community.csdn.net/Expert/topic/3295/3295916.xml?temp=.8193018
Option Explicit
' demo project showing how to custom draw single items in a listview
' by Bryan Stafford of New Vision Software?- [email protected]
' this demo is released into the public domain "as is" without
' warranty or guaranty of any kind. In other words, use at
' your own risk.
' See the comments at the end of this module for a brief explaination of
' what subclassing is.
' Generic WM_NOTIFY notification codes for common controls
Public Enum WinNotifications
NM_FIRST = (-0&) ' (0U- 0U) ' // generic to all controls
NM_LAST = (-99&) ' (0U- 99U)
NM_OUTOFMEMORY = (NM_FIRST - 1&)
NM_CLICK = (NM_FIRST - 2&)
NM_DBLCLK = (NM_FIRST - 3&)
NM_RETURN = (NM_FIRST - 4&)
NM_RCLICK = (NM_FIRST - 5&)
NM_RDBLCLK = (NM_FIRST - 6&)
NM_SETFOCUS = (NM_FIRST - 7&)
NM_KILLFOCUS = (NM_FIRST - 8&)
NM_CUSTOMDRAW = (NM_FIRST - 12&)
NM_HOVER = (NM_FIRST - 13&)
End Enum
Public Const WM_NOTIFY As Long = &H4E&
' constants used for customdraw routine
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&
' The NMHDR structure contains information about a notification message. The pointer
' to this structure is specified as the lParam member of a WM_NOTIFY message.
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 Type
' function used to manipulate memory data
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes&)
' gdi function used to select bold font into the hDC passed in the prepaint message
Public Declare Function SelectObject Lib "gdi32" (ByVal hDC&, ByVal hObject&) As Long
' handle to the bold font set for the form. this is used to set the bold font for the listview items
Public g_hBoldFont As Long ' this var will hold a pointer to the original message handler so we MUST
' save it so that it can be restored before we exit the app. if we don't
' restore it.... CRASH!!!!
Public g_addProcOld As Long ' function used to call the next window proc in the "chain" for the subclassed window
Public Declare Function CallWindowProc& Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc&, _
ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&)
'WARNING!!!! WARNING!!!! WARNING!!!! WARNING!!!! WARNING!!!! WARNING!!!!
'
' Do NOT try to step through this function in debug mode!!!!
' You WILL crash!!! Also, do NOT set any break points in this function!!!
' You WILL crash!!! Subclassing is non-trivial and should be handled with
' EXTREAME care!!!
'
' There are ways to use a "Debug" dll to allow you to set breakpoints in
' subclassed code in the IDE but this was not implimented for this demo.
'
'WARNING!!!! WARNING!!!! WARNING!!!! WARNING!!!! WARNING!!!! WARNING!!!!
Public Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long ' this is *our* implimentation of the message handling routine
' determine which message was recieved
Select Case iMsg
Case WM_NOTIFY
' if it's a WM_NOTIFY message copy the data from the address pointed to
' by lParam into a NMHDR struct
Dim udtNMHDR As NMHDR
CopyMemory udtNMHDR, ByVal lParam, 12&
With udtNMHDR
If .code = NM_CUSTOMDRAW Then
' if the code member of the struct is NM_CUSTOMDRAW, copy the data
' pointed to by lParam into a NMLVCUSTOMDRAW struct
Dim udtNMLVCUSTOMDRAW As NMLVCUSTOMDRAW
' this is now OUR copy of the struct
CopyMemory udtNMLVCUSTOMDRAW, ByVal lParam, Len(udtNMLVCUSTOMDRAW)
With udtNMLVCUSTOMDRAW.nmcd
' determine whether or not this is one of the messages we are interested in
Select Case .dwDrawStage
' if it's a prepaint message, tell windows WE want first dibs
' on painting for each item and then exit without letting VB get this message
Case CDDS_PREPAINT
WindowProc = CDRF_NOTIFYITEMDRAW
Exit Function
' if it's time to paint an item, check to see if it's divisible by 3.
' if it is, select the bold font that we borrowed from the form into the
' hDC of the listview and set the text color to something *different*.
' then tell windows that we changed the font for this item.
Case CDDS_ITEMPREPAINT
If (.dwItemSpec Mod 3) = 0 Then
Call SelectObject(.hDC, g_hBoldFont)
' we can also set the color for items in the listview.
' we will set the color for every third item....
If (.dwItemSpec Mod 9) = 0 Then
udtNMLVCUSTOMDRAW.clrText = RGB(255, 75, 150)
udtNMLVCUSTOMDRAW.clrTextBk = RGB(255, 255, 255)
' copy OUR copy of the struct back to the memory address pointed to by lParam
CopyMemory ByVal lParam, udtNMLVCUSTOMDRAW, Len(udtNMLVCUSTOMDRAW)
End If
' tell windows that we changed the font and do not allow VB to get this message
WindowProc = CDRF_NEWFONT
Exit Function
End If
End Select
End With
End If
End With
End Select
' pass all messages on to VB and then return the value to windows
WindowProc = CallWindowProc(g_addProcOld, hWnd, iMsg, wParam, lParam)End Function