Is it possible to change the color of a GIVEN entry in a ListView?
Or only set the color of ALL text in that control?
Has anyone subclassed it for this?
Posted on 2003-04-01 01:49:00 by Homer
You may possibly use the NMCUSTOMDRAW notifications for such things.
Posted on 2003-04-01 02:16:59 by japheth
Posted on 2003-04-01 06:42:27 by wizzra
guy posted this source code a while back. it will change the the color of one entry of a listview.

.586

.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\comctl32.inc
include \masm32\include\gdi32.inc
include listVCdraw.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\gdi32.lib


WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD
WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
TopXY PROTO :DWORD,:DWORD

return MACRO arg
mov eax, arg
ret
ENDM


InsertColumn MACRO handle,txt,width
LOCAL lbl
LOCAL strng
jmp lbl
strng db txt,0
lbl:
mov col.imask,LVCF_TEXT or LVCF_WIDTH
mov col.fmt,0
mov col.lx,width
mov col.pszText,offset strng
invoke lstrlen,offset strng
mov col.cchTextMax,eax
invoke SendMessage,handle,LVM_INSERTCOLUMN,0,addr col
ENDM

.data
ClassName db "Starting Template",0
CaptionText db "Adding an item and subitems to a listview",0
LvClassName db "SysListView32",0
text1 db "item 1",0
text2 db "subitem 1",0
text3 db "subitem 2",0
text4 db "subtiem 3",0


.const
LVM_GETSUBITEMRECT equ LVM_FIRST + 56

.data?
hWnd HANDLE ?
hInstance HANDLE ?
hListView HANDLE ?
col LVCOLUMN <>
item LVITEM <>
hdc HDC ?
ps PAINTSTRUCT <>
rect RECT <>

.code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke WinMain,hInstance,NULL,NULL,SW_SHOWDEFAULT
invoke ExitProcess,eax
invoke InitCommonControls

; ######################################

WinMain proc hInst :DWORD,
hPrevInst :DWORD,
CmdLine :DWORD,
CmdShow :DWORD

;====================
; Put LOCALs on stack
;====================

LOCAL wc :WNDCLASSEX
LOCAL msg :MSG
LOCAL Wwd :DWORD
LOCAL Wht :DWORD
LOCAL Wtx :DWORD
LOCAL Wty :DWORD

;==================================================
; Fill WNDCLASSEX structure with required variables
;==================================================

mov wc.cbSize, SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW + CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra, NULL
mov wc.cbWndExtra, NULL
mov eax, hInstance
mov wc.hInstance, eax
invoke LoadIcon, NULL,IDI_APPLICATION
mov wc.hIcon, eax
mov wc.hIconSm, eax
invoke LoadCursor, NULL,IDC_ARROW
mov wc.hCursor, eax
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName, NULL
mov wc.lpszClassName,OFFSET ClassName


invoke RegisterClassEx, addr wc

;================================
; Centre window at following size
;================================

mov Wwd, 500
mov Wht, 350

invoke GetSystemMetrics,SM_CXSCREEN
invoke TopXY,Wwd,eax
mov Wtx, eax

invoke GetSystemMetrics,SM_CYSCREEN
invoke TopXY,Wht,eax
mov Wty, eax

invoke CreateWindowEx,NULL,
ADDR ClassName,
ADDR CaptionText,
WS_OVERLAPPEDWINDOW,
Wtx,Wty,Wwd,Wht,
NULL,
NULL,
hInst,
NULL
mov hWnd,eax

invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd

;===================================
; Loop until PostQuitMessage is sent
;===================================

StartLoop:
invoke GetMessage,ADDR msg,NULL,0,0
cmp eax, 0
je ExitLoop
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
jmp StartLoop
ExitLoop:

mov eax,msg.wParam
ret

WinMain endp

; ###################################

WndProc proc hWin :HWND,
uMsg :UINT,
wParam :WPARAM,
lParam :LPARAM


.IF uMsg==WM_DESTROY
invoke PostQuitMessage,NULL

.ELSEIF uMsg==WM_CREATE

invoke CreateWindowEx,NULL,
ADDR LvClassName,
NULL,
WS_CHILD + WS_VISIBLE + LVS_REPORT+LVS_SHOWSELALWAYS+LVS_SINGLESEL,
0,0,491,250,
hWin,
NULL,
hInstance,
NULL
mov hListView,eax
invoke SendMessage,hListView,LVM_SETTEXTCOLOR,0,0


invoke SendMessage,hListView,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_GRIDLINES

InsertColumn hListView, "Column 4" ,122
InsertColumn hListView, "Column 3" ,122
InsertColumn hListView, "Column 2" ,122
InsertColumn hListView, "Column 1" ,122
mov item.iItem,0
mov item.iSubItem,0
mov item.imask,LVIF_TEXT
mov item.pszText,offset text1
mov item.cchTextMax,sizeof text1
invoke SendMessage,hListView,LVM_INSERTITEM,0,addr item

mov item.imask,LVIF_TEXT
mov item.iSubItem,1
mov item.pszText,offset text2
mov item.cchTextMax,sizeof text2
invoke SendMessage,hListView,LVM_SETITEM,0,addr item

mov item.imask,LVIF_TEXT
mov item.iSubItem,2
mov item.pszText,offset text3
mov item.cchTextMax,sizeof text3
invoke SendMessage,hListView,LVM_SETITEM,0,addr item

mov item.imask,LVIF_TEXT
mov item.iSubItem,3
mov item.pszText,offset text4
mov item.cchTextMax,sizeof text4
invoke SendMessage,hListView,LVM_SETITEM,0,addr item


.elseif uMsg==WM_NOTIFY
mov edi,lParam
assume edi:ptr NMHDR
mov eax,[edi].hwndFrom
.if eax==hListView
.if [edi].code == LVN_ITEMCHANGED

assume edi:ptr NMHDR

.elseif [edi].code==NM_CUSTOMDRAW
assume edi:ptr NMLVCUSTOMDRAW
.if CDDS_SUBITEM || CDDS_ITEMPREPAINT
.if [edi].iSubItem == 2
mov [edi].clrTextBk,0080FFFFh ;Background text = yellow
mov [edi].clrText,000000FFh ; text color = red
.else
mov [edi].clrTextBk,0FFFFFFh ;Background text = white
mov [edi].clrText,00h ; text color = black
.endif
.endif
return 32
.endif
.endif
return 0



.ELSE
invoke DefWindowProc,hWin,uMsg,wParam,lParam
ret

.ENDIF
xor eax,eax
ret

WndProc endp


; ################################

TopXY proc wDim:DWORD, sDim:DWORD

shr sDim, 1
shr wDim, 1
mov eax, wDim
sub sDim, eax
mov eax,sDim
ret

TopXY endp

; ################################

end start
Posted on 2003-04-01 07:34:02 by smurf
Thanks guys :)
Posted on 2003-04-01 19:17:54 by Homer
So far I have colored columns.
I had to source the following constructs (missing from masm includes),
which might be handy for someone else...

NM_CUSTOMDRAW equ NM_FIRST-12
CDDS_PREPAINT equ 1
CDDS_ITEM equ 10000h
CDDS_ITEMPREPAINT equ (CDDS_ITEM or CDDS_PREPAINT)
CDDS_SUBITEM equ 20000h

NMCUSTOMDRAW STRUCT
hdr NMHDR <>
dwDrawStage DWORD ?
hdc HDC ?
rc RECT <>
dwItemSpec DWORD ?
uItemState DWORD ?
lItemlParam LPARAM ?
NMCUSTOMDRAW ENDS
LPNMCUSTOMDRAW typedef ptr NMCUSTOMDRAW

NMLVCUSTOMDRAW STRUCT
nmcd NMCUSTOMDRAW <>
clrText COLORREF <>
clrTextBk COLORREF <>
iSubItem DWORD ?
dwItemType DWORD ?
clrFace COLORREF <>
iIconEffect DWORD ?
iIconPhase DWORD ?
iPartId DWORD ?
iStateId DWORD ?
rcText RECT <>
uAlign DWORD ?
NMLVCUSTOMDRAW ENDS
LPNMLVCUSTOMDRAW typedef ptr NMLVCUSTOMDRAW

cya.
Posted on 2003-04-02 01:30:09 by Homer