AutoBert

[solved] Set HeaderColumn Color in ListView

19 posts in this topic

#1 ·  Posted (edited)

I have a LV with different coloured Cols:

;https://autoit.de/index.php/Thread/84279-Unterschiedliche-Textfarben-innerhalb-einer-ListView/?postID=674195#post674195
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>

$aColBK = IniReadSection(@ScriptDir & "\lv_format.ini", "ColBKColor")
_ArrayDelete($aColBK, 0)
;_ArrayDisplay($aColBK)
$aRowBK = IniReadSection(@ScriptDir & "\lv_format.ini", "RowBKColor")
_ArrayDelete($aRowBK, 0)
;_ArrayDisplay($aColBK)
$aColText = IniReadSection(@ScriptDir & "\lv_format.ini", "ColTextColor")
_ArrayDelete($aColText, 0)
;_ArrayDisplay($aColBK)
$aRowText = IniReadSection(@ScriptDir & "\lv_format.ini", "RowTextColor")
_ArrayDelete($aRowText, 0)
;_ArrayDisplay($aRowText)

$GUI = GUICreate("Listview Farbig", 1024, 300, 0, 0) ;<==== Breite geändert
$cListView = GUICtrlCreateListView("", 2, 2, 1020, 294, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ;<==== Breite geändert
$hListView = GUICtrlGetHandle($cListView)
For $i = 1 To 31
    _GUICtrlListView_InsertColumn($hListView, $i - 1, $i, 25)
    _GUICtrlListView_SetColumnWidth($hListView, $i - 1, 32) ;<==== eingefügt für Spaltenbreite
Next
For $i = 0 To 21 ; alle Item/SubItem erstellen
    _GUICtrlListView_AddItem($hListView, "Item: " &$i, $i)
    For $j = 1 To 30
        _GUICtrlListView_AddSubItem($hListView, $i, "Sub: "&$j, $j)
    Next
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRect
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Item/SubItem das aktuell gezeichnet werden soll ermitteln
                            $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
                            Switch $iItem ; Zeilenwahl
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aRowBK, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also weiß
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR(0xFFFFFF))
                                    Else
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aRowBK[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' BK: '&$aRowBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aRowText, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also schwarz
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR(0x000000))
                                    Else
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR($aRowText[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' Text: '&$aRowText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Switch $iSubitem
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aColBK, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aColBK[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' BK: '&$aColBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aColText, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR($aColText[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' Text: '&$aColText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR

i use lv_format.ini for defining the cols Background- and Textcolour. Now i want to colour the cols in the LV-Header also, but i can't figure it out.

 

62_Listview Farbig.jpg

Edited by AutoBert

Share this post


Link to post
Share on other sites



Maybe this is what you are looking for:

 

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

AutoBert,

You can also do it via the WM_NOTIFY message sent when repainting:

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

Global $iDllGDI = DllOpen("gdi32.dll")

;Three column colours
Global $aCol[3][2] = [[0xFF0000, 0xFFFF00], [0x00FF00, 0x0000FF], [0x0000FF, 0xFFCCCC]]
;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)

$cListView = GUICtrlCreateListView("Items List|SubItems1|SubItems2", 10, 10, 480, 280)
_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

;Get header handle
Global $hHeader = HWnd(GUICtrlSendMsg($cListView, $LVM_GETHEADER, 0, 0))
;Turn off theme for header
DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", $hHeader, "wstr", "", "wstr", "")

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

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

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 $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
    Switch $iCode

        Case $NM_CUSTOMDRAW

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

                Case $CDDS_ITEMPREPAINT
                    Local $hDC = DllStructGetData($tNMLVCUSTOMDRAW, "hDC")
                    Local $iItem = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec")
                    If $iItem < 3 Then
                        DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", $aCol[$iItem][0])
                        DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", $aCol[$iItem][1])
                    EndIf
                    Return $CDRF_NEWFONT; $CDRF_NEWFONT must be returned after changing font or colors
                    Return $CDRF_SKIPDEFAULT

            EndSwitch

    EndSwitch

EndFunc   ;==>_WM_NOTIFY

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

I might look at how I can incorporate that into my GUIListViewEx UDF - although it does affect the visual style of the header quite a bit.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

It works in his script only headercols are coloured, but when i implement to mine:

Spoiler
;https://autoit.de/index.php/Thread/84279-Unterschiedliche-Textfarben-innerhalb-einer-ListView/?postID=674195#post674195
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>

Global $iDllGDI = DllOpen("gdi32.dll")

$aColBK = IniReadSection(@ScriptDir & "\lv_format.ini", "ColBKColor")
_ArrayDelete($aColBK, 0)
;_ArrayDisplay($aColBK)
$aRowBK = IniReadSection(@ScriptDir & "\lv_format.ini", "RowBKColor")
_ArrayDelete($aRowBK, 0)
;_ArrayDisplay($aColBK)
$aColText = IniReadSection(@ScriptDir & "\lv_format.ini", "ColTextColor")
_ArrayDelete($aColText, 0)
;_ArrayDisplay($aColBK)
$aRowText = IniReadSection(@ScriptDir & "\lv_format.ini", "RowTextColor")
_ArrayDelete($aRowText, 0)
;_ArrayDisplay($aRowText)

$GUI = GUICreate("Listview Farbig", 1024, 300, 0, 0) ;<==== Breite geändert
$cListView = GUICtrlCreateListView("", 2, 2, 1020, 294, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ;<==== Breite geändert
$hListView = GUICtrlGetHandle($cListView)
For $i = 1 To 31
    _GUICtrlListView_InsertColumn($hListView, $i - 1, $i, 25)
    _GUICtrlListView_SetColumnWidth($hListView, $i - 1, 32) ;<==== eingefügt für Spaltenbreite
Next
;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 = 0 To 21 ; alle Item/SubItem erstellen
    _GUICtrlListView_AddItem($hListView, "Item: " & $i, $i)
    For $j = 1 To 30
        _GUICtrlListView_AddSubItem($hListView, $i, "Sub: " & $j, $j)
    Next
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    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")
                            $iIndex = _ArraySearch($aColText, $iItem, 0, 0, 0, 0, 1, 0)
                            DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", RGB2BGR($aColText[$iIndex][1]))
                            $iIndex = _ArraySearch($aColBK, $iItem, 0, 0, 0, 0, 1, 0)
                            DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", RGB2BGR($aColBK[$iIndex][1]))
                            Return $CDRF_NEWFONT
                            ;Return $CDRF_SKIPDEFAULT
                    EndSwitch
            EndSwitch
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRect
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            ;                           Return $CDRF_NOTIFYSUBITEMDRAW
                        Case $CDDS_ITEMPREPAINT
                            $hDC = DllStructGetData($tCustDraw, "hDC")
                            $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                            $iIndex = _ArraySearch($aColText, $iSubitem, 0, 0, 0, 0, 1, 0)
                            DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", RGB2BGR($aColText[$iIndex][1]))
                            $iIndex = _ArraySearch($aColBK, $iSubitem, 0, 0, 0, 0, 1, 0)
                            DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", RGB2BGR($aColBK[$iIndex][1]))
                            ;Return $CDRF_NEWFONT
                            ;Return $CDRF_SKIPDEFAULT
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Item/SubItem das aktuell gezeichnet werden soll ermitteln
                            $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
                            Switch $iItem ; Zeilenwahl
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aRowBK, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also weiß
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR(0xFFFFFF))
                                    Else
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aRowBK[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' BK: '&$aRowBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aRowText, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also schwarz
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR(0x000000))
                                    Else
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR($aRowText[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' Text: '&$aRowText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Switch $iSubitem
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aColBK, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aColBK[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' BK: '&$aColBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aColText, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR($aColText[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' Text: '&$aColText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR

 

it fails. Nothing is drawn.

@Melba23: i will try your (seems to be identic) later.

60_Listview Farbig.jpg

Share this post


Link to post
Share on other sites

AutoBert,

Quote

seems to be identic

Not at all. The actual colouring calls are the same (as you would expect) but rover subclassed the header while I simply intercept the WM_NOTIFY message using GUIRegisterMsg.  In my opinion, subclassing is fraught with danger and should be avoided if at all possible - when developing my NoFocusLines UDF I learnt a lot about it and how you need to clean up carefully afterwards to prevent crashes when the script exits.

I will look to see how my code might be implemented in your script this afternoon - unless you beat me to it!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

I found souluton:

;https://autoit.de/index.php/Thread/84279-Unterschiedliche-Textfarben-innerhalb-einer-ListView/?postID=674195#post674195
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>

Global $iDllGDI = DllOpen("gdi32.dll")

$aColBK = IniReadSection(@ScriptDir & "\lv_format.ini", "ColBKColor")
_ArrayDelete($aColBK, 0)
;_ArrayDisplay($aColBK)
$aRowBK = IniReadSection(@ScriptDir & "\lv_format.ini", "RowBKColor")
_ArrayDelete($aRowBK, 0)
;_ArrayDisplay($aColBK)
$aColText = IniReadSection(@ScriptDir & "\lv_format.ini", "ColTextColor")
_ArrayDelete($aColText, 0)
;_ArrayDisplay($aColBK)
$aRowText = IniReadSection(@ScriptDir & "\lv_format.ini", "RowTextColor")
_ArrayDelete($aRowText, 0)
;_ArrayDisplay($aRowText)

$GUI = GUICreate("Listview Farbig", 1024, 300, 0, 0) ;<==== Breite geändert
$cListView = GUICtrlCreateListView("", 2, 2, 1020, 294, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ;<==== Breite geändert
_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)
For $i = 1 To 31
    _GUICtrlListView_InsertColumn($hListView, $i - 1, $i, 25)
    _GUICtrlListView_SetColumnWidth($hListView, $i - 1, 32) ;<==== eingefügt für Spaltenbreite
Next
;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 = 0 To 21 ; alle Item/SubItem erstellen
    _GUICtrlListView_AddItem($hListView, "Item: " & $i, $i)
    For $j = 1 To 30
        _GUICtrlListView_AddSubItem($hListView, $i, "Sub: " & $j, $j)
    Next
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hHeader
            ConsoleWrite('Header: ' & $iCode & @CRLF)
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    ConsoleWrite($iDrawStage & @CRLF)
                    Switch $iDrawStage
                        Case $CDDS_PREPAINT
                            ConsoleWrite('$CDDS_PREPAINT' & @CRLF)
                            Return $CDRF_NOTIFYITEMDRAW
                        Case $CDDS_ITEMPREPAINT
                            Local $hDC = DllStructGetData($tCustDraw, "hDC")
                            Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                            ConsoleWrite('Item: ' & $iItem & @CRLF)
                            $iIndex = _ArraySearch($aColText, $iItem, 0, 0, 0, 0, 1, 0)
                            If $iIndex > -1 Then
                                ConsoleWrite('Index: ' & $iIndex & @CRLF)
                                DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", RGB2BGR($aColText[$iIndex][1]))
                            EndIf
                            $iIndex = _ArraySearch($aColBK, $iItem, 0, 0, 0, 0, 1, 0)
                            If $iIndex > -1 Then
                                ConsoleWrite('Index: ' & $iIndex & @CRLF)
                                DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", RGB2BGR($aColBK[$iIndex][1]))
                            EndIf
                            ;Return 1
                            Return $CDRF_NEWFONT
                            Return $CDRF_SKIPDEFAULT
                    EndSwitch
            EndSwitch
        Case $hListView
            ;ConsoleWrite('Listview' & @CRLF)
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRect
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Item/SubItem das aktuell gezeichnet werden soll ermitteln
                            $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
                            Switch $iItem ; Zeilenwahl
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aRowBK, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also weiß
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR(0xFFFFFF))
                                    Else
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aRowBK[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' BK: '&$aRowBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aRowText, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also schwarz
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR(0x000000))
                                    Else
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR($aRowText[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' Text: '&$aRowText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Switch $iSubitem
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aColBK, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($aColBK[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' BK: '&$aColBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aColText, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tCustDraw, 'clrText', RGB2BGR($aColText[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' Text: '&$aColText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR

but the result is not the best:

 

64_Listview Farbig.jpg

Share this post


Link to post
Share on other sites

AutoBert,

Like you I can only get the text to be coloured, not the entire header section. I will continue to look around.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

AutoBert,

As far as I can see you can only colour the text and its background within the header - pity.

But I would be happy to be proved wrong!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Here is another example:

 

Styles%20fonts%20colors_zpshqabfk8w.png

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

mLipok,

Thanks for that - my brain is bleeding already......

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Hi all,

Got it working within a WM_NOTIFY handler:

#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]]


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

$cListView = GUICtrlCreateListView(" | | ", 10, 10, 480, 280)
_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

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

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


    If DllStructGetData($tStruct, 1) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Switch $iCode

            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

All credit to LarsJ for his code which I have plundered mercilessly.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Hi again,

Now I have it working in my GUIListViewEx UDF - after a bit of a struggle, I must admit. At least I now understand how the code works and does its magic.

Thanks to AutoBert for the question, water and mLipok for the links, and LarsJ for the code - how pleasant when we all work together to produce something really useful.

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Well, why not using additionally GDI+ to pimp up the colors?

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

_GDIPlus_Startup()
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]]


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

$cListView = GUICtrlCreateListView(" | | ", 10, 10, 480, 280)
_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

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

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_Shutdown()

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


    If DllStructGetData($tStruct, 1) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Switch $iCode

            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)

                        Local Const $hGfx = _GDIPlus_GraphicsCreateFromHDC($hDC)
                        Local Const $hBrush = _GDIPlus_LineBrushCreate($tRECT.Left, $tRECT.Top, $tRECT.Right, $tRECT.Bottom, 0xFF000000 + $aHdrData[$iIndex][1], 0xFFFFFFFF, 1)
                        _GDIPlus_GraphicsFillRect($hGfx, $tRECT.Left, $tRECT.Top, $tRECT.Right, $tRECT.Bottom, $hBrush)
                        _GDIPlus_BrushDispose($hBrush)
                        _GDIPlus_GraphicsDispose($hGfx)

                        ; 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

 

I used a very simple color gradient but you can extend this example by using more GDI+ functions (plenty of variations)! 

Edited by UEZ
Changed the code to use the $aHdrData array for colors
1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

@UEZ This is awesome modyfication.

Can you do it for LV elements ?


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites
Just now, mLipok said:

Can you do it for LV elements ?

I'm not sure but I would say yes. Let me check this out.

 

Btw, you can add crazy things like an animation made with GDI+ to the drawn headers...

 


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites
Just now, UEZ said:

Btw, you can add crazy things like an animation made with GDI+ to the drawn headers...

Example please :)


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

The triggering of_WM_NOTIFY is not permanentely:

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

_GDIPlus_Startup()
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]]

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

$cListView = GUICtrlCreateListView(" | | ", 10, 10, 480, 280)
_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

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

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_Shutdown()

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


    If DllStructGetData($tStruct, 1) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Switch $iCode

            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)

                        Switch $iIndex
                            Case 2
                                Anim($hDC, $tRECT)
                            Case Else
                                Local Const $hGfx = _GDIPlus_GraphicsCreateFromHDC($hDC)
                                Local Const $hBrush = _GDIPlus_LineBrushCreate($tRECT.Left, $tRECT.Top, $tRECT.Right, $tRECT.Bottom, 0xFF000000 + $aHdrData[$iIndex][1], 0xFFFFFFFF, 1)
                                _GDIPlus_GraphicsFillRect($hGfx, $tRECT.Left, $tRECT.Top, $tRECT.Right, $tRECT.Bottom, $hBrush)
                                _GDIPlus_BrushDispose($hBrush)
                                _GDIPlus_GraphicsDispose($hGfx)
                        EndSwitch

                        ; 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

Func Anim($hDC, $tRECT)
    Local Const $hGfx = _GDIPlus_GraphicsCreateFromHDC($hDC)
    _GDIPlus_GraphicsSetSmoothingMode($hGfx, 4)
    Local Const $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000), $fSize = 4
    Local Static $fX = 10, $fY = 10, $bSet = False, $fVX, $fVY
    If Not $bSet Then
        $fVX = Random(0, 1, 1) ? Random(-1.5, -0.5) : Random(0.5, 1.5)
        $fVY = Random(0, 1, 1) ? Random(-1.5, -0.5) : Random(0.5, 1.5)
        $bSet = True
    EndIf
    $fX += $fVX
    $fY += $fVY
    If BitOR($tRECT.Left + $fX <= $tRECT.Left, $tRECT.Left + $fX - $fSize - 2 >= $tRECT.Right - $fSize - 2) Then $fVX *= -1
    If BitOR($tRECT.Top + $fY <= $tRECT.Top, $tRECT.Top + $fY - $fSize - 2 >= $tRECT.Bottom - $tRECT.Top - $fSize - 2) Then $fVY *= -1
    $fX = ($fX > $tRECT.Right) ? $tRECT.Right - $fSize : $fX
    _GDIPlus_GraphicsFillEllipse($hGfx, $tRECT.Left + $fX, $tRECT.Top + $fY, $fSize, $fSize, $hBrush)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGfx)
EndFunc

Either hover your mouse over the 3rd header or resize any column to move background dot.

 

Any idea how to trigger the animation permanentely?

1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Again very handy code.

Now I'm sure it is possible to do simple anim with rolling Up or Down Arrowhead to show reverting sort order.
btw. I do not challange you, I just give a description of example how this can be used, to describe what it might be useful.

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Thanks to all, i will try to implement:

The solution of UEZ is amazing, but i want to build a small UDF _ArrayDisplayEX and coloring some LV-Columns (incl. LV-Headercols) is just enough.

 

Solved: here is the quick and dirty C&P and S&R solution:

;https://autoit.de/index.php/Thread/84279-Unterschiedliche-Textfarben-innerhalb-einer-ListView/?postID=674195#post674195
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>

Global $iDllGDI = DllOpen("gdi32.dll")

$aColBK = IniReadSection(@ScriptDir & "\lv_format.ini", "ColBKColor")
_ArrayDelete($aColBK, 0)
;_ArrayDisplay($aColBK)
$aRowBK = IniReadSection(@ScriptDir & "\lv_format.ini", "RowBKColor")
_ArrayDelete($aRowBK, 0)
;_ArrayDisplay($aColBK)
$aColText = IniReadSection(@ScriptDir & "\lv_format.ini", "ColTextColor")
_ArrayDelete($aColText, 0)
;_ArrayDisplay($aColBK)
$aRowText = IniReadSection(@ScriptDir & "\lv_format.ini", "RowTextColor")
_ArrayDelete($aRowText, 0)
;_ArrayDisplay($aRowText)

$GUI = GUICreate("Listview Farbig", 1024, 300, 0, 0) ;<==== Breite geändert
$cListView = GUICtrlCreateListView("", 2, 2, 1020, 294, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ;<==== Breite geändert
;_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)
For $i = 1 To 31
    _GUICtrlListView_InsertColumn($hListView, $i - 1, $i, 25)
    _GUICtrlListView_SetColumnWidth($hListView, $i - 1, 32) ;<==== eingefügt für Spaltenbreite
Next
;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))

; 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 = 0 To 21 ; alle Item/SubItem erstellen
    _GUICtrlListView_AddItem($hListView, "Item: " & $i, $i)
    For $j = 1 To 30
        _GUICtrlListView_AddSubItem($hListView, $i, "Sub: " & $j, $j)
    Next
Next
$iCols = _GUICtrlListView_GetColumnCount($hListView)
Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _
        ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct"

Global $aHdrData[$iCols][3]
For $i=0 To $iCols -1
    $aHdrData[$i][0]=_GUICtrlListView_GetColumn($hListView,$i)[5]
    $iIndex=_ArraySearch($aColText, $i, 0, 0, 0, 0, 1, 0)
    If $iIndex <>-1 Then
        $aHdrData[$i][1]=RGB2BGR($aColText[$iIndex][1])
    Else
        $aHdrData[$i][1]=0x000000
    EndIf
    $iIndex=_ArraySearch($aColBK, $i, 0, 0, 0, 0, 1, 0)
    If $iIndex <>-1 Then
        $aHdrData[$i][2]=RGB2BGR($aColBK[$iIndex][1])
    Else
        $aHdrData[$i][2]=0xFFFFFF
    EndIf
Next
#cs
    [][] = [["Tom",   0x000000, 0x00FFFF], _
    ["Dick",  0x00FFFF, 0x0000FF], _
    ["Harry", 0xFF0000, 0xFFCCCC]]
#ce
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hHeader
            ;ConsoleWrite('Header: ' & $iCode & @CRLF)
            Switch $iCode
                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
        Case $hListView
            ;ConsoleWrite('Listview' & @CRLF)
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tNMCustomDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRECT
                    $iDrawStage = DllStructGetData($tNMCustomDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Item/SubItem das aktuell gezeichnet werden soll ermitteln
                            $iItem = DllStructGetData($tNMCustomDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tNMCustomDraw, 'iSubItem')
                            Switch $iItem ; Zeilenwahl
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aRowBK, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also weiß
                                        DllStructSetData($tNMCustomDraw, 'clrTextBk', RGB2BGR(0xFFFFFF))
                                    Else
                                        DllStructSetData($tNMCustomDraw, 'clrTextBk', RGB2BGR($aRowBK[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' BK: '&$aRowBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aRowText, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also schwarz
                                        DllStructSetData($tNMCustomDraw, 'clrText', RGB2BGR(0x000000))
                                    Else
                                        DllStructSetData($tNMCustomDraw, 'clrText', RGB2BGR($aRowText[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' Text: '&$aRowText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Switch $iSubitem
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aColBK, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tNMCustomDraw, 'clrTextBk', RGB2BGR($aColBK[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' BK: '&$aColBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aColText, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tNMCustomDraw, 'clrText', RGB2BGR($aColText[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' Text: '&$aColText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR

no i have to understand and to polish the code.

66_Listview Farbig.jpg

Edited by AutoBert

Share this post


Link to post
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

  • Similar Content

    • Skysnake
      By Skysnake
      Hi
      Best example I could find is here
      ::/html/libfunctions/_GUICtrlTreeView_ClickItem.htm
      The way I understand this, the standard Windows messages, such as Left/Right Click etc are covered by AutoIt macros, but the real power is locked up inside this WM_NOTIFY .  I have tried, but I am not even sure I understand what I am looking at.
      I need help understanding this.  What I am looking for is Help file or Tutorial explaining how this works?  
      Perhaps if there is a script showing both the working of an AutoIt macro and the WM_NOTIFY  in action, it would help.
      Right now I am so lost, I do not even know what are the right questions to ask.  One issue I have is on ListViews, how to combine the "Click" of a line with a standard Switch loop?  Like users clicks a line, then get the loop to detect the click and ;do something ...
      Any ideas?
      Skysnake
    • PINTO1927
      By PINTO1927
      Hello guys,
      I'm working with a simple script, complementing a GUICtrlCreateListView which has 5 columns. I would order the first column so that the numbers are decreasing.
      #include <GuiListView.au3> #include <ListViewConstants.au3> #include <WindowsConstants.au3> #include <GuiConstantsEx.au3> #include <StructureConstants.au3> Opt('MustDeclareVars', 1) Global $search_LV, $B_DESCENDING GUICreate("ListView Sort by Column Click", 400, 300) $search_LV = GUICtrlCreateListView("String|Number|String", 2, 2, 394, 268) GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES) GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT) GUICtrlCreateListViewItem("line4|5|1A", $search_LV) GUICtrlCreateListViewItem("line5|4.50 |1B", $search_LV) GUICtrlCreateListViewItem("line10|4.0 |2C", $search_LV) GUICtrlCreateListViewItem("line3|23|01", $search_LV) GUICtrlCreateListViewItem("line2|0.34560 |09", $search_LV) GUICtrlCreateListViewItem("line1|1.0 |7A", $search_LV) GUICtrlCreateListViewItem("line1|0.1 |8C", $search_LV) GUICtrlCreateListViewItem("line1|97|5B", $search_LV) GUICtrlCreateListViewItem("line1|910|9B", $search_LV) GUICtrlCreateListViewItem("line1|99|11", $search_LV) GUICtrlCreateListViewItem("line1|990.99|06", $search_LV) _GUICtrlListView_SetColumnWidth($search_LV, 0, 75) _GUICtrlListView_SetColumnWidth($search_LV, 1, 75) _GUICtrlListView_SetColumnWidth($search_LV, 2, 75) GUISetState() GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;~ _GUICtrlListView_RegisterSortCallBack($search_LV, False) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $search_LV ; Kick off the sort callback ;~ _GUICtrlListView_SortItems($search_LV, GUICtrlGetState($search_LV)) ;~ _GUICtrlListView_UnRegisterSortCallBack($search_LV) EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo $hWndListView = $search_LV ;~ If Not IsHWnd($search_LV) Then $hWndListView = GUICtrlGetHandle($search_LV) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom ;~ Case $search_LV Case GUICtrlGetHandle($search_LV) Switch $iCode Case $LVN_COLUMNCLICK ; A column was clicked ConsoleWrite("Header geklickt" & @CRLF) Local $tInfo = DllStructCreate($tagNMLISTVIEW, $iLparam) _GUICtrlListView_UnRegisterSortCallBack($search_LV) Local $ColumnSorted = DllStructGetData($tInfo, 'SubItem') If $ColumnSorted = 1 Then ConsoleWrite('Numeric: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, True) & @CRLF) Else ConsoleWrite('Literal: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, False) & @CRLF) EndIf _GUICtrlListView_SortItems($search_LV, DllStructGetData($tInfo, 'SubItem')) EndSwitch EndSwitch EndFunc ;==>WM_NOTIFY Ordering the "String" column, you notice that the line "line10" is not ordered last. And 'possible to order the "String" column in descending order?
      thank you very much
    • david1337
      By david1337
      Hey guys
      Quick question:
      In this GUI the listview is created from items in test.txt
      I would like my "Get Info" button to show the actual text of the item that is selected. So far I have been able to make it show the number of the item.
      What is the last missing puzzle here?
       
      #include <GUIConstantsEx.au3> #include <GUIListViewEx.au3> Global $MainGUI_ManageItemList Global $File = "test.txt" Global $FileToArray = FileReadToArray("test.txt") Call ("MainGUI_ManageItemList") Func MainGUI_ManageItemList() Local $Button1 $MainGUI_ManageItemList = GUICreate("Manage Item List", 800, 400, -1, -1) $cLV = GUICtrlCreateListView("[items]", 10, 10, 400, 775, $LVS_NOCOLUMNHEADER) GUICtrlSetFont(-1, 12, 800, 0, "@Arial Unicode MS") _GUICtrlListView_SetColumnWidth($cLV, 0, 378) $Button1 = GUICtrlCreateButton("Button 1", 425, 10, 80, 30) $RemoveItem = GUICtrlCreateButton("Remove Item", 425, 50, 80, 30) $GetInfo = GUICtrlCreateButton("GetInfo", 425, 120, 80, 30) GUISetState(@SW_SHOW, $MainGUI_ManageItemList) ; Intialise ListView Global $iLV_Index = _GUIListViewEx_Init($cLV) ; Insert lines _GUIListViewEx_Insert($FileToArray, True) ; Register required messages _GUIListViewEx_MsgRegister(True, False, False, False) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $Button1 MsgBox(0,"","Button 1 is pressed") Case $RemoveItem _GUIListViewEx_Delete() Case $GetInfo $iIndex = _GUICtrlListView_GetSelectedIndices($cLV) msgbox (0, "Selected item", $iIndex) EndSwitch WEnd EndFunc ;==>Main  
    • PINTO1927
      By PINTO1927
      Hi Guys,
      I'm working on this project:
      $Import = GUICtrlCreateButton("Import", 15, 175, 90, 40, $WS_GROUP) $List = GUICtrlCreateListView("Name|Address|E-mail", 15, 220, 400, 363, $LVS_SORTDESCENDING) $Item1 = GUICtrlCreateListViewItem("test|test|test", $List) GUISetState(@SW_SHOW, $GUI) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE, $Exit Exit Case $Import $Table_Import = FileOpenDialog("Import list '*.txt'", @DesktopDir & "\", "Text (*.txt)") If @error Then ContinueLoop GUICtrlSetData($Item1, $Table_Import) In the text file the fields are delimited with "|" and the values are not in the same row but one below the other.
      However in the txt file the sum of the values may change.
      Can you help me?
      Thank's
    • PINTO1927
      By PINTO1927
      Hello guys,
      I built a gui where a GUICtrlCreateListView with two columns is present.
      Through an "Import" button I would be able to import the contents of an .xls or .txt.
      Can you help me?
      Thank's