Jump to content

[solved] Set HeaderColumn Color in ListView


Recommended Posts

  • Moderators

BBs19,

Alas, no.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 2 years later...
On 10/29/2017 at 7:44 PM, BBs19 said:

Hi Melba, do you also know a way to color the background that is behind the headers? If I resize the headers, the background of the main ListView seems to be white :(

 

LV.PNG

One trick is to add an empty column at the end and resize it dynamically:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>

Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _
                                ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct"

;                        Title    TextCol   BkCol         - colours in BGR
Global $aHdrData[][] = [["Tom",   0x000000, 0x00FFFF], _
                        ["Dick",  0x00FFFF, 0x0000FF], _
                        ["Harry", 0xFF0000, 0xFFCCCC], _
                        ["", 0xFF0000, 0xFFCCCC] _ ;"fill" column
                        ]
Global $colCount = UBound($aHdrData)


$hGUI = GUICreate("Set Listview Header Colour ", 500, 300)
$cListView = GUICtrlCreateListView(_ArrayToString(_ArrayExtract($aHdrData, 0, $colCount-2, 0, 0)), 10, 10, 480, 280)
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

;Get header handle
Global $hHeader = _GUICtrlListView_GetHeader($cListView)

; Get the font of the Header control (credit KaFu)
Local $hDC = _WinAPI_GetDC($hHeader)
Local $hFont = _SendMessage($hHeader, $WM_GETFONT)
Local $hObject = _WinAPI_SelectObject($hDC, $hFont)
Local $tLogFont = DllStructCreate($tagLOGFONT)

_WinAPI_GetObject($hFont, DllStructGetSize($tLogFont), DllStructGetPtr($tLogFont))
_WinAPI_SelectObject($hDC, $hObject)
_WinAPI_ReleaseDC($hHeader, $hDC)

Local $iWeight = DllStructGetData( $tLogFont, "Weight" )               ; Bold
DllStructSetData( $tLogFont, "Weight", BitOR( $iWeight, $FW_BOLD ) )
$hHdrFont = _WinAPI_CreateFontIndirect( $tLogFont )

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

For $i = 0 To  $colCount - 2
    _GUICtrlListView_SetColumnWidth($cListView, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next

_GUICtrlListView_AddColumn($cListView, "") ;this will prevent last visible column from filling up the remaining width
_GUICtrlListView_SetColumnWidth($cListView, $colCount - 1, 0)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam
    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return

    Local $tNMHEADER = DllStructCreate($tagNMHEADER, $lParam)
    If HWnd( DllStructGetData($tNMHEADER, "hWndFrom")) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Switch $iCode

            Case $HDN_BEGINTRACKW
                Local $iCol = DllStructGetData($tNMHEADER, "Item")
                If $iCol = $colCount - 1 Then
                    ; Prevent resizing of last (empty) column
                    Return True
                Else
                    ; Allow resizing
                    Return False
                EndIf

            Case $HDN_TRACKW, $HDN_ENDTRACKW
                Local $pos = ControlGetPos($hGUI, "", $cListView)
                Local Static $scrollbarWidth = _WinAPI_GetSystemMetrics($SM_CXVSCROLL)
                Local $rectFirst = _GUICtrlListView_GetItemRect($hListView, 0)
                Local $rectLast = _GUICtrlListView_GetSubItemRect($hListView, _GUICtrlListView_GetItemCount($cListView) - 1, $colCount - 2, 0) ;rect of last subitem of last item 
                Local $sw = ($rectFirst[1] < 0 Or $rectLast[3] > $pos[3]) ? $scrollbarWidth : 0 ;scrollbar shown?
                Local $w = $pos[2] - $rectLast[2] - $sw - 4 ;not sure what the 4 is for
                GUICtrlSendMsg( $cListView, $LVM_SETCOLUMNWIDTH, $colCount-1, $w > 0 ? $w : 0);resize last (empty) column

            Case $NM_CUSTOMDRAW

                Local $tNMCustomDraw = DllStructCreate($tagNMCUSTOMDRAW, $lParam)
                Local $dwDrawStage = DllStructGetData($tNMCustomDraw, "dwDrawStage")
                Switch $dwDrawStage ; Holds a value that specifies the drawing stage
                    Case $CDDS_PREPAINT ; Before the paint cycle begins
                        Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations

                    Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background)
                        Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations

                    Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts)
                        Local $tRECT = DllStructCreate($tagRECT)
                        Local $iIndex = DllStructGetData($tNMCustomDraw, "dwItemSpec") ; Item index
                        Local $hDC = DllStructGetData($tNMCustomDraw, "hdc") ; Device context
                        _WinAPI_SelectObject($hDC, $hHdrFont) ; Set text font
                        _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; Transparent background
                        _WinAPI_SetTextColor( $hDC, $aHdrData[$iIndex][1]) ; Set text colour
                        ; Get header section size
                        DllStructSetData($tRECT, 1, DllStructGetData($tNMCustomDraw, 6) + 1)
                        DllStructSetData($tRECT, 2, DllStructGetData($tNMCustomDraw, 7) + 1)
                        DllStructSetData($tRECT, 3, DllStructGetData($tNMCustomDraw, 8) - ($iIndex = $colCount - 1 ? 0 : 2))
                        DllStructSetData($tRECT, 4, DllStructGetData($tNMCustomDraw, 9) - 2)
                        ; Set and draw back colour
                        Local $hBrush = _WinAPI_CreateSolidBrush($aHdrData[$iIndex][2])
                        _WinAPI_FillRect($hDC, $tRECT, $hBrush)
                        ; Write text
                        If $iIndex < _GUICtrlListView_GetColumnCount($cListView) Then
                            _WinAPI_DrawText ( $hDC, $aHdrData[$iIndex][0], $tRECT, $DT_CENTER + $DT_VCENTER )
                        EndIf
                        Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors
                EndSwitch

        EndSwitch
    EndIf

EndFunc   ;==>_WM_NOTIFY

Or disallow resizing last column to the size smaller than the width of the list

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>

Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _
                                ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct"

;                        Title    TextCol   BkCol         - colours in BGR
Global $aHdrData[][] = [["Tom",   0x000000, 0x00FFFF], _
                        ["Dick",  0x00FFFF, 0x0000FF], _
                        ["Harry", 0xFF0000, 0xFFCCCC] _
                        ]
Global $colCount = UBound($aHdrData)


$hGUI = GUICreate("Set Listview Header Colour ", 500, 300)
$cListView = GUICtrlCreateListView(_ArrayToString(_ArrayExtract($aHdrData, 0, $colCount-1, 0, 0)), 10, 10, 480, 280)
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

;Get header handle
Global $hHeader = _GUICtrlListView_GetHeader($cListView)

; Get the font of the Header control (credit KaFu)
Local $hDC = _WinAPI_GetDC($hHeader)
Local $hFont = _SendMessage($hHeader, $WM_GETFONT)
Local $hObject = _WinAPI_SelectObject($hDC, $hFont)
Local $tLogFont = DllStructCreate($tagLOGFONT)

_WinAPI_GetObject($hFont, DllStructGetSize($tLogFont), DllStructGetPtr($tLogFont))
_WinAPI_SelectObject($hDC, $hObject)
_WinAPI_ReleaseDC($hHeader, $hDC)

Local $iWeight = DllStructGetData( $tLogFont, "Weight" )               ; Bold
DllStructSetData( $tLogFont, "Weight", BitOR( $iWeight, $FW_BOLD ) )
$hHdrFont = _WinAPI_CreateFontIndirect( $tLogFont )

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

For $i = 0 To  $colCount - 1
    _GUICtrlListView_SetColumnWidth($cListView, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam
    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return

    Local $tNMHEADER = DllStructCreate($tagNMHEADER, $lParam)
    If HWnd( DllStructGetData($tNMHEADER, "hWndFrom")) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Local Static $lastRect = 0
        Switch $iCode

            Case $HDN_TRACKW, $HDN_ENDTRACKW
                Local $pos = ControlGetPos($hGUI, "", $cListView)
                Local $rectLast = _GUICtrlListView_GetSubItemRect($hListView, _GUICtrlListView_GetItemCount($cListView) - 1, $colCount - 1, 0)
                If $pos[2] > $rectLast[2] Then
                    Local Static $scrollbarWidth = _WinAPI_GetSystemMetrics($SM_CXVSCROLL)
                    Local $rectFirst = _GUICtrlListView_GetItemRect($hListView, 0)
                    Local $sw = ($rectFirst[1] < 0 Or $rectLast[3] > $pos[3]) ? $scrollbarWidth : 0
                    GUICtrlSendMsg( $cListView, $LVM_SETCOLUMNWIDTH, $colCount-1, $pos[2] - $rectLast[0] - $sw - 4);resize last column
                    Return True
                EndIf
            Case $NM_CUSTOMDRAW

                Local $tNMCustomDraw = DllStructCreate($tagNMCUSTOMDRAW, $lParam)
                Local $dwDrawStage = DllStructGetData($tNMCustomDraw, "dwDrawStage")
                Switch $dwDrawStage ; Holds a value that specifies the drawing stage
                    Case $CDDS_PREPAINT ; Before the paint cycle begins
                        Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations

                    Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background)
                        Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations

                    Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts)
                        Local $tRECT = DllStructCreate($tagRECT)
                        Local $iIndex = DllStructGetData($tNMCustomDraw, "dwItemSpec") ; Item index
                        Local $hDC = DllStructGetData($tNMCustomDraw, "hdc") ; Device context
                        _WinAPI_SelectObject($hDC, $hHdrFont) ; Set text font
                        _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; Transparent background
                        _WinAPI_SetTextColor( $hDC, $aHdrData[$iIndex][1]) ; Set text colour
                        ; Get header section size
                        DllStructSetData($tRECT, 1, DllStructGetData($tNMCustomDraw, 6) + 1)
                        DllStructSetData($tRECT, 2, DllStructGetData($tNMCustomDraw, 7) + 1)
                        DllStructSetData($tRECT, 3, DllStructGetData($tNMCustomDraw, 8) - 2)
                        DllStructSetData($tRECT, 4, DllStructGetData($tNMCustomDraw, 9) - 2)
                        ; Set and draw back colour
                        Local $hBrush = _WinAPI_CreateSolidBrush($aHdrData[$iIndex][2])
                        _WinAPI_FillRect($hDC, $tRECT, $hBrush)
                        ; Write text
                        If $iIndex < _GUICtrlListView_GetColumnCount($cListView) Then
                            _WinAPI_DrawText ( $hDC, $aHdrData[$iIndex][0], $tRECT, $DT_CENTER + $DT_VCENTER )
                        EndIf
                        Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors
                EndSwitch

        EndSwitch
    EndIf

EndFunc   ;==>_WM_NOTIFY

 

One thing I can't figure out is why $HDN_TRACKW event is not firing while column divider is being dragged?

Also there must be a better way detect when vertical scrollbar is shown, I'm using first item and last item coordinates to detect if they are outside of listview or not.

Edited by VAN0
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

×
×
  • Create New...