效果一
#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
ConsoleWrite("C"&@CRLF)
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
|