Jump to content

Changing ListView Column Headers color.


Recommended Posts

I feel like I am missing the obvious (again - sigh) but I can't find how to change the color of a ListView Column Header.

I'm trying to change the whole header not just one cell, although that would be fine too.

Thanks

Edited by JAFN
[size="2"]The second mouse gets the cheese[/size]
Link to comment
Share on other sites

I feel like I am missing the obvious (again - sigh) but I can't find how to change the color of a ListView Column Header.

I'm trying to change the whole header not just one cell, although that would be fine too.

Thanks

Not as straightforward as the usual custom drawing.

The ListView must be subclassed to get at the NM_CUSTOMDRAW notification sent to the ListView

from the child SysHeader32 control.

The ListViews header control Theme/Visual Style must be turned off in order to paint the columns.

;coded by rover 2k11
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>
#include <Constants.au3>

Opt('MustDeclareVars', 1)

Global $hGUI, $cListView, $hListView
Global $iDllGDI = DllOpen("gdi32.dll")
Global $iDllUSER32 = DllOpen("user32.dll")

;Three column colours
Global $aCol[3][2] = [[0xFFFF00, 0x494E49],[0xFFFF00, 0x0000FF],[0xFFFFFF, 0x0D0E0F]]

;One colour to rule them all
;blow off array and just set SetTextColor() / SetBkColor() with one colour
;Global $aCol[3][2] = [[0xFFFF00, 0x0000FF], [0xFFFF00, 0x0000FF], [0xFFFF00, 0x0000FF]]


;Convert RBG to BGR for SetText/BkColor()
For $i = 0 To UBound($aCol)-1
    $aCol[$i][0] = _BGR2RGB($aCol[$i][0])
    $aCol[$i][1] = _BGR2RGB($aCol[$i][1])
Next

$hGUI = GUICreate("Set Listview Header Colour ", 500, 300)
GUISetBkColor(0xFFFFFF)


$cListView = GUICtrlCreateListView("Items List|SubItems1|SubItems2", 10, 10, 480, 280)
;get ListView handle from control ID
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

GUICtrlSetFont(-1, 9, 800, 0, "Comic Sans MS", $ANTIALIASED_QUALITY)

;get handle to child SysHeader32 control of ListView
Global $hHeader = HWnd(GUICtrlSendMsg($cListView, $LVM_GETHEADER, 0, 0))
;Turn off theme for header
DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", $hHeader, "wstr", "", "wstr", "")
;subclass ListView to get at NM_CUSTOMDRAW notification sent to ListView
Global $wProcNew = DllCallbackRegister("_LVWndProc", "ptr", "hwnd;uint;wparam;lparam")
Global $wProcOld = _WinAPI_SetWindowLong($hListView, $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))

;Optional: Flat Header - remove header 3D button effect
Global $iStyle = _WinAPI_GetWindowLong($hHeader, $GWL_STYLE)
_WinAPI_SetWindowLong($hHeader, $GWL_STYLE, BitOR($iStyle, $HDS_FLAT))


For $i = 1 To 15
    _GUICtrlListView_AddItem($hListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 2)
Next

_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 2, $LVSCW_AUTOSIZE_USEHEADER)

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
If $wProcOld Then _WinAPI_SetWindowLong($hListView, $GWL_WNDPROC, $wProcOld)
; Delete callback function
If $wProcNew Then DllCallbackFree($wProcNew)
GUIDelete()
Exit

Func _LVWndProc($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    If $iMsg = $WM_NOTIFY Then
        Local $tNMHDR, $hWndFrom, $iCode, $iItem, $hDC
        $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iCode = DllStructGetData($tNMHDR, "Code")
        ;Local $IDFrom = DllStructGetData($tNMHDR, "IDFrom")

        Switch $hWndFrom
            Case $hHeader
                Switch $iCode
                    Case $NM_CUSTOMDRAW
                        Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                        Switch DllStructGetData($tCustDraw, "dwDrawStage")
                            Case $CDDS_PREPAINT
                                Return $CDRF_NOTIFYITEMDRAW
                            Case $CDDS_ITEMPREPAINT
                                $hDC = DllStructGetData($tCustDraw, "hDC")
                                $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                                DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", $aCol[$iItem][0])
                                DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", $aCol[$iItem][1])
                                Return $CDRF_NEWFONT
                                Return $CDRF_SKIPDEFAULT
                        EndSwitch
                EndSwitch
        EndSwitch
    EndIf
    ;pass the unhandled messages to default WindowProc
    Local $aResult = DllCall($iDllUSER32, "lresult", "CallWindowProcW", "ptr", $wProcOld, _
            "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
    If @error Then Return -1
    Return $aResult[0]
EndFunc   ;==>_LVWndProc

Func _BGR2RGB($iColor)
    ;Author: Wraithdu
    Return BitOR(BitShift(BitAND($iColor, 0x0000FF), -16), BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc   ;==>_BGR2RGB

I see fascists...

Link to comment
Share on other sites

Not as straightforward as the usual custom drawing.

The ListView must be subclassed to get at the NM_CUSTOMDRAW notification sent to the ListView

from the child SysHeader32 control.

The ListViews header control Theme/Visual Style must be turned off in order to paint the columns.

;coded by rover 2k11
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>
#include <Constants.au3>

Opt('MustDeclareVars', 1)

Global $hGUI, $cListView, $hListView
Global $iDllGDI = DllOpen("gdi32.dll")
Global $iDllUSER32 = DllOpen("user32.dll")

;Three column colours
Global $aCol[3][2] = [[0xFFFF00, 0x494E49],[0xFFFF00, 0x0000FF],[0xFFFFFF, 0x0D0E0F]]

;One colour to rule them all
;blow off array and just set SetTextColor() / SetBkColor() with one colour
;Global $aCol[3][2] = [[0xFFFF00, 0x0000FF], [0xFFFF00, 0x0000FF], [0xFFFF00, 0x0000FF]]


;Convert RBG to BGR for SetText/BkColor()
For $i = 0 To UBound($aCol)-1
    $aCol[$i][0] = _BGR2RGB($aCol[$i][0])
    $aCol[$i][1] = _BGR2RGB($aCol[$i][1])
Next

$hGUI = GUICreate("Set Listview Header Colour ", 500, 300)
GUISetBkColor(0xFFFFFF)


$cListView = GUICtrlCreateListView("Items List|SubItems1|SubItems2", 10, 10, 480, 280)
;get ListView handle from control ID
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

GUICtrlSetFont(-1, 9, 800, 0, "Comic Sans MS", $ANTIALIASED_QUALITY)

;get handle to child SysHeader32 control of ListView
Global $hHeader = HWnd(GUICtrlSendMsg($cListView, $LVM_GETHEADER, 0, 0))
;Turn off theme for header
DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", $hHeader, "wstr", "", "wstr", "")
;subclass ListView to get at NM_CUSTOMDRAW notification sent to ListView
Global $wProcNew = DllCallbackRegister("_LVWndProc", "ptr", "hwnd;uint;wparam;lparam")
Global $wProcOld = _WinAPI_SetWindowLong($hListView, $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))

;Optional: Flat Header - remove header 3D button effect
Global $iStyle = _WinAPI_GetWindowLong($hHeader, $GWL_STYLE)
_WinAPI_SetWindowLong($hHeader, $GWL_STYLE, BitOR($iStyle, $HDS_FLAT))


For $i = 1 To 15
    _GUICtrlListView_AddItem($hListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 2)
Next

_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 2, $LVSCW_AUTOSIZE_USEHEADER)

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
If $wProcOld Then _WinAPI_SetWindowLong($hListView, $GWL_WNDPROC, $wProcOld)
; Delete callback function
If $wProcNew Then DllCallbackFree($wProcNew)
GUIDelete()
Exit

Func _LVWndProc($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    If $iMsg = $WM_NOTIFY Then
        Local $tNMHDR, $hWndFrom, $iCode, $iItem, $hDC
        $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iCode = DllStructGetData($tNMHDR, "Code")
        ;Local $IDFrom = DllStructGetData($tNMHDR, "IDFrom")

        Switch $hWndFrom
            Case $hHeader
                Switch $iCode
                    Case $NM_CUSTOMDRAW
                        Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                        Switch DllStructGetData($tCustDraw, "dwDrawStage")
                            Case $CDDS_PREPAINT
                                Return $CDRF_NOTIFYITEMDRAW
                            Case $CDDS_ITEMPREPAINT
                                $hDC = DllStructGetData($tCustDraw, "hDC")
                                $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                                DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", $aCol[$iItem][0])
                                DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", $aCol[$iItem][1])
                                Return $CDRF_NEWFONT
                                Return $CDRF_SKIPDEFAULT
                        EndSwitch
                EndSwitch
        EndSwitch
    EndIf
    ;pass the unhandled messages to default WindowProc
    Local $aResult = DllCall($iDllUSER32, "lresult", "CallWindowProcW", "ptr", $wProcOld, _
            "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
    If @error Then Return -1
    Return $aResult[0]
EndFunc   ;==>_LVWndProc

Func _BGR2RGB($iColor)
    ;Author: Wraithdu
    Return BitOR(BitShift(BitAND($iColor, 0x0000FF), -16), BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc   ;==>_BGR2RGB

With all the things you can do to the header you'd think a basic color setting would be a built in feature. Maybe in a future version.

I find I am understanding more and more but this is well beyond me.

I think I'll chock this one up to to convoluted for the non-importance to the project.

Thank you for your time.

[size="2"]The second mouse gets the cheese[/size]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...