如何使用自绘的方式 修改LISTVIEW某个ITEM为标准高亮的显示状态??
如何使用自绘的方式 修改LISTVIEW某个ITEM为标准高亮的显示状态??官网 有段范例 可以修改 LISTVIEW 点击后 原本高亮的颜色
无奈 自己的程度太差了搞不定 看不懂,因此 求助高手
我想要的是 可以 指定 某一个ITEM 用自绘的方式达到 原本高亮的颜色与效果
能否达到呢??
#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 Not _GUICtrlListView_GetViewDetails($hWndFrom) 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
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_GetStockObject($GRAY_BRUSH)) ; Change the bush here. You can use GDI+ to make your own.
第六十八行。。这里指定了灰色。。。$GRAY_BRUSH
只是要修改颜色的话,你修改这里就可以了。。常规对象的类型. 参数可以是任何以下值之一:
$BLACK_BRUSH - 黑色笔刷
$DKGRAY_BRUSH - 暗灰色笔刷
$GRAY_BRUSH - 灰色笔刷
$HOLLOW_BRUSH - 空心笔刷 (相当于 NULL_BRUSH)
$LTGRAY_BRUSH - 浅灰色笔刷
$NULL_BRUSH - 空笔刷 (相当于 HOLLOW_BRUSH)
$WHITE_BRUSH - 白色笔刷
$BLACK_PEN - 黑色笔
$NULL_PEN - 空笔
$WHITE_PEN - 白色笔
$ANSI_FIXED_FONT - Windows 固定间距(等宽)系统字体
$ANSI_VAR_FONT - Windows 可变间距(比例空间)的系统字体
$DEVICE_DEFAULT_FONT - 设备相关的字体
$DEFAULT_GUI_FONT - 用户界面对象的默认字体
$OEM_FIXED_FONT - OEM 固定间距(等宽)字体
$SYSTEM_FONT - 系统字体
$SYSTEM_FIXED_FONT - 固定间距(等宽)系统字体,在早于 Windows 3.0版本系统中使用
$DEFAULT_PALETTE - 默认调色板. 包括系统调色板中的静态颜色注释说你可以用gdi+自己填充。。这句本身就是gdi的填充矩形 回复 2# yamakawa
我有看到 可是 這行我改成其他的選項 就是不會變成 那個顏色 你懂我意思嗎
事實上 我也有其他的 LISTVIEW 背景顏色 上色方法
但是問題是 高量藍色 就是表現不出來不知道那個藍要用怎樣的 色碼 回复 3# kk_lee69
就是说,这句实现不了你要的颜色?可以发个颜色代码给我么?或者截图,,所谓的高亮蓝色是什么颜色 回复 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 回复 4# yamakawa
我比對了兩段程式真的搞不懂 那個變色的 到底是在哪邊處理的?? 这样子符合你要求么?代码修改的地方我都画圈了
回复 7# yamakawa
方便提供 程式碼嗎感謝 回复 6# kk_lee69
不知道是不是你copy错了。。你的两个代码,在我这里都是暗灰色。。不管了。。
不用gdiplus。用gdi的。我是这样实现的,你看看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)
;GDI方式修改,添加自定义笔刷
$hbrush = _WinAPI_CreateSolidBrush ( 0xff4e00 )
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hbrush) ; Change the bush here. You can use GDI+ to make your own.
_WinAPI_DeleteObject ( $hbrush )
;用完要释放
EndIf
DllStructSetData($tRect, "Left", 2)
DllStructSetData($tRect, "Top", $iSubitem)
_SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
;添加这行,修改文字颜色
_WinAPI_SetTextColor($hDC,0xffffff)
;;;;;;;;;;;;其他不动
_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 回复 9# yamakawa
我沒COPY 錯誤我也覺得怪 為什麼寫法都一樣兩段程式我這邊跑起來 一段是灰色 一段是藍色 回复 9# yamakawa
這樣的效果正是我要的感謝 我測試看看
页:
[1]