Code : Tout sélectionner
;http://www.autoitscript.com/forum/topic/130653-changing-listview-column-headers-color/
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>
#include <Constants.au3>
#include <GuiImageList.au3>
Opt('MustDeclareVars', 1)
Global $hGUI, $cListView, $hListView
Global $iDllGDI = DllOpen("gdi32.dll")
Global $iDllUSER32 = DllOpen("user32.dll")
Local $sURL = "http://www.autoitscript.com/autoit3/files/graphics/autoit9_wall_grey_800x600.jpg"
Local $sFilePath = @TempDir & "\AutoIt.jpg"
InetGet($sURL, $sFilePath)
;Three column colours
Global $aCol[3][2] = [[0xFFFF00, 0x494E49],[0xFFFF00, 0x0000FF],[0xFFFFFF, 0x0D0E0F]]
;One colour to rule them all
;blow off array and just set SetTextColor() / SetBkColor() with one colour
;Global $aCol[3][2] = [[0xFFFF00, 0x0000FF], [0xFFFF00, 0x0000FF], [0xFFFF00, 0x0000FF]]
;Convert RBG to BGR for SetText/BkColor()
For $i = 0 To UBound($aCol)-1
$aCol[$i][0] = _BGR2RGB($aCol[$i][0])
$aCol[$i][1] = _BGR2RGB($aCol[$i][1])
Next
$hGUI = GUICreate("Custom listview", 620, 500)
GUISetBkColor(0xFFFFFF)
$cListView = GUICtrlCreateListView("Items List|SubItems1 |SubItems2 large", 10, 10, 600, 480, -1, $WS_EX_TRANSPARENT)
;get ListView handle from control ID
GUICtrlSetFont(-1, 9, 800, 0, "Comic Sans MS", $ANTIALIASED_QUALITY)
;GUICtrlSetBkColor(-1, 0xabcdef)
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)) ;, $LVS_EX_GRIDLINES
DllCall('ole32.dll', 'long', 'CoInitializeEx', 'ptr', 0, 'dword', 0x02)
_GUICtrlListView_SetBkImage($hListView, $sFilePath)
;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))
Global $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, @Autoitexe, 0)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 132)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 209)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 138)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 131)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 165)
_GUICtrlListView_SetImageList($hListView, $hImage, 1)
For $i = 1 To 15
_GUICtrlListView_AddItem($hListView, "Item" & $i, 0)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1, 1)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 2, 2)
Next
_GUICtrlListView_SetItemImage($hListView, 14, 5, 2)
_GUICtrlListView_SetColumnWidth($hListView, 0, 90)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 2, $LVSCW_AUTOSIZE_USEHEADER)
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
If $wProcOld Then _WinAPI_SetWindowLong($hListView, $GWL_WNDPROC, $wProcOld)
; Delete callback function
If $wProcNew Then DllCallbackFree($wProcNew)
DllCall('ole32.dll', 'long', 'CoUinitialize')
GUIDelete()
FileDelete($sFilePath)
Exit
;====================================================
Func _LVWndProc($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
If $iMsg = $WM_NOTIFY Then
Local $tNMHDR, $hWndFrom, $iCode, $iItem, $hDC
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iCode = DllStructGetData($tNMHDR, "Code")
;Local $IDFrom = DllStructGetData($tNMHDR, "IDFrom")
Switch $hWndFrom
Case $hHeader
Switch $iCode
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
Switch DllStructGetData($tCustDraw, "dwDrawStage")
Case $CDDS_PREPAINT
Return $CDRF_NOTIFYITEMDRAW
Case $CDDS_ITEMPREPAINT
$hDC = DllStructGetData($tCustDraw, "hDC")
$iItem = DllStructGetData($tCustDraw, "dwItemSpec")
DllCall($iDllGDI, "int", "SetTextColor", "handle", $hDC, "dword", $aCol[$iItem][0])
DllCall($iDllGDI, "int", "SetBkColor", "handle", $hDC, "dword", $aCol[$iItem][1])
Return $CDRF_NEWFONT
Return $CDRF_SKIPDEFAULT
EndSwitch
EndSwitch
EndSwitch
EndIf
;pass the unhandled messages to default WindowProc
Local $aResult = DllCall($iDllUSER32, "lresult", "CallWindowProcW", "ptr", $wProcOld, _
"hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
If @error Then Return -1
Return $aResult[0]
EndFunc ;==>_LVWndProc
Func _BGR2RGB($iColor)
;Author: Wraithdu
Return BitOR(BitShift(BitAND($iColor, 0x0000FF), -16), BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc ;==>_BGR2RGB