回复 4# yamakawa
你在看一下這段程式好了 同一個人寫的 範例 但是 怪的是 我看顏色部分寫法都一樣
不知道是否 哪邊出了問題 這段程式的 顏色並沒有變成其他的顏色
而是 原本的高亮藍色
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <StructureConstants.au3>
$GUI = GUICreate("Listview Custom Draw", 400, 300)
$cListView = GUICtrlCreateListView("Column 1|Column 2|Column 3", 2, 2, 394, 268, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)
; Add items
For $i = 1 To 30
GUICtrlCreateListViewItem("Row" & $i & ": Col 1|Row" & $i & ": Col 2|Row" & $i & ": Col 3", $cListView)
Next
GUICtrlCreateInput("Click here to test focus", 50, 275, 200, 18)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Exit
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
If _GUICtrlListView_GetView($hWndFrom) <> 1 Then Return $GUI_RUNDEFMSG ; Not in details mode
Local $tCustDraw, $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $iColor1, $iColor2, $iColor3
$tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
$iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
Switch $iDrawStage
Case $CDDS_PREPAINT
Return $CDRF_NOTIFYITEMDRAW
Case $CDDS_ITEMPREPAINT
Return $CDRF_NOTIFYSUBITEMDRAW
Case $CDDS_ITEMPOSTPAINT
; Not handled
Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
$iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
$iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then ; Item to draw is selected
$hDC = _WinAPI_GetDC($hWndFrom)
$tRect = DllStructCreate($tagRECT)
; We draw the background when we draw the first item.
If $iSubitem = 0 Then
; We must send the message as we want to use the struct. _GUICtrlListView_GetSubItemRect returns an array.
_SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
DllStructSetData($tRect, "Left", 2)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_GetStockObject($GRAY_BRUSH)) ; Change the bush here. You can use GDI+ to make your own.
EndIf
DllStructSetData($tRect, "Left", 2)
DllStructSetData($tRect, "Top", $iSubitem)
_SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
_WinAPI_SetBkMode($hDC, $TRANSPARENT) ; It uses the background drawn for the first item.
; Select the font we want to use
_WinAPI_SelectObject($hDC, _SendMessage($hWndFrom, $WM_GETFONT))
If $iSubitem = 0 Then
DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 2)
Else
DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 6)
EndIf
_WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_VCENTER, $DT_END_ELLIPSIS, $DT_SINGLELINE))
_WinAPI_ReleaseDC($hWndFrom, $hDC)
Return $CDRF_SKIPDEFAULT ; Don't do default processing
EndIf
Return $CDRF_NEWFONT ; Let the system do the drawing for non-selected items
Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
; Not handled
EndSwitch
Case $LVN_COLUMNCLICK
$tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
$iCol = DllStructGetData($tInfo, "SubItem")
$hHdr = _GUICtrlListView_GetHeader($hWndFrom)
; Work out sort from arrows in header, and clear other arrows
$fSort = True
For $i = 0 To _GUICtrlHeader_GetItemCount($hHdr)
$iFmt = _GUICtrlHeader_GetItemFormat($hHdr, $i)
If $i = $iCol Then
If BitAND($iFmt, $HDF_SORTUP) Then
$fSort = True
$iFmt = BitOR(BitAND($iFmt, BitNOT($HDF_SORTUP)), $HDF_SORTDOWN)
ElseIf BitAND($iFmt, $HDF_SORTDOWN) Then
$fSort = False
$iFmt = BitOR(BitAND($iFmt, BitNOT($HDF_SORTDOWN)), $HDF_SORTUP)
Else
; Default
$iFmt = BitOR(BitAND($iFmt, BitNOT($HDF_SORTUP)), $HDF_SORTDOWN)
$fSort = True
EndIf
_GUICtrlHeader_SetItemFormat($hHdr, $i, $iFmt)
Else
$iFmt = BitAND($iFmt, BitNOT(BitOR($HDF_SORTUP, $HDF_SORTDOWN)))
_GUICtrlHeader_SetItemFormat($hHdr, $i, $iFmt)
EndIf
Next
$tInfo = DllStructCreate("HWND;int;int")
DllStructSetData($tInfo, 1, $hWndFrom)
DllStructSetData($tInfo, 2, $fSort)
DllStructSetData($tInfo, 3, $iCol)
$cb = DllCallbackRegister(__Compare, "int", "LPARAM;LPARAM;LPARAM")
_SendMessage($hWndFrom, $LVM_SORTITEMSEX, DllStructGetPtr($tInfo), DllCallbackGetPtr($cb))
DllCallbackFree($cb)
Return True
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
; int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
Func __Compare($nItem1, $nItem2, $lParamSort)
Local $tInfo = DllStructCreate("HWND;int;int", $lParamSort)
Local $hWndFrom = DllStructGetData($tInfo, 1)
Local $fSort = DllStructGetData($tInfo, 2)
Local $iCol = DllStructGetData($tInfo, 3)
Local $s1 = _GUICtrlListView_GetItemText($hWndFrom, $nItem1, $iCol)
Local $s2 = _GUICtrlListView_GetItemText($hWndFrom, $nItem2, $iCol)
Return StringCompare($s1, $s2) * ($fSort ? -1 : 1)
EndFunc ;==>__Compare
|