改变指定 ListView 项目的字体颜色
改变指定 ListView 项目的字体颜色下面是 netegg老大的範例
求教高手改成判斷
原本是 CASE 5 判斷 第五筆資料
希望 改成判斷內容是Item3 才變色
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <FontConstants.au3>
#include <WinAPI.au3>
Global Const $CDDS_SUBITEMPREPAINT = BitOR($CDDS_ITEM, $CDDS_SUBITEM, $CDDS_PREPAINT)
Global $Font1 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_BOLD)
Global $Font2 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, _
$CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, "MS Sans Serif")
$hGUI = GUICreate("Test", 300, 200)
$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems|", 10, 10, 280, 180, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
For $i = 1 To 10
_GUICtrlListView_AddItem($hListView, "Item" & $i)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 2)
Next
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_WinAPI_DeleteObject($Font1)
_WinAPI_DeleteObject($Font2)
Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
Local $tNMHDR, $hWndFrom, $iCode
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView
Switch $iCode
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC
Switch $iItem
Case 5
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR() 谢谢分享! for $i = 0 to _GUICtrlListView_GetItemCount($hListview) - 1
ifstringinstr(_GUICtrlListView_GetItemText($hListView, $i), $text)) then
剩下的差不多,无非是把$iItem定义为$i 本帖最后由 kk_lee69 于 2014-11-27 17:52 编辑
回复 3# netegg
改這樣 變成全部變色
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <FontConstants.au3>
#include <WinAPI.au3>
Global Const $CDDS_SUBITEMPREPAINT = BitOR($CDDS_ITEM, $CDDS_SUBITEM, $CDDS_PREPAINT)
Global $Font1 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_BOLD)
Global $Font2 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, _
$CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, "MS Sans Serif")
$hGUI = GUICreate("Test", 300, 200)
$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems|", 10, 10, 280, 180, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))
For $i = 1 To 10
_GUICtrlListView_AddItem($hListView, "Item" & $i)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 2)
Next
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_WinAPI_DeleteObject($Font1)
_WinAPI_DeleteObject($Font2)
Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
Local $tNMHDR, $hWndFrom, $iCode
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView
Switch $iCode
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $S = DllStructGetData($tCustDraw, "Text")
Local $iColor, $hDC
;Switch $iItem
for $i = 0 to _GUICtrlListView_GetItemCount($hListview) - 1 Step 1
ifStringInStr(_GUICtrlListView_GetItemText($hListView, $i), "Item3")THEN
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndIf
NEXT
;EndSwitch
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR() 这样就好多了 ifStringInStr(_GUICtrlListView_GetItemText($hListView, $i), "Item3")THEN $item = $i 本帖最后由 netegg 于 2014-11-27 18:54 编辑
For $i = 0 To _GUICtrlListView_GetItemCount($hListView) - 1 Step 1
If StringInStr(_GUICtrlListView_GetItemText($hListView, $i), "Item2") Then
Switch $iItem
Case $i
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndIf
Next
如果是读取数据库要怎样改?
For $i = 0 To _GUICtrlListView_GetItemCount($hListView) - 1 Step 1
If StringInStr(_GUICtrlListView_GetItemText($hListView, $i), "Item2") Then
;---------------------------------------------
$i), "Item2" ??? 回复 7# netegg
這樣說我就懂了~~~感謝 !!
雖然如此 可是實際應用其實 還是得想個其他方法因為原本
虛擬列表 10000 筆數據只要不到一秒鐘
加上 這個判斷後卡住了 停頓好幾秒
請看 代碼
;~ 關於虛擬列表的一份說明
;~ http://blog.vckbase.com/iwaswzq/archive/2006/07/07/21113.aspx
#cs ____________________________________
Au3 版本: 3.3.9.0
腳本作者: CrossDoor
電子郵件: 382869232@qq.com
QQ/TM: 382869232
#ce _______________腳本開始_________________
#Region ;**** 參數創建於 ACNWrapper_GUI ****
#PRE_UseX64=n
#EndRegion ;**** 參數創建於 ACNWrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#include <FontConstants.au3>
Global $Font1 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_BOLD)
Global $Font2 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, _
$CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, "MS Sans Serif")
Global $B_DESCENDING ; 排序用陣列
Local $tText = DllStructCreate("wchar Text");建個結構,用來放listview列資料
Local $GUI, $hImage, $iITEM_COUNT = 10000
Local $sSomeString = ""
Local $aBoy[$iITEM_COUNT], $aGirl[$iITEM_COUNT]
For $i = 0 To $iITEM_COUNT - 1;初始化陣列
$aBoy[$i] = "爺爺" & $i
$aBoy[$i] = "爸爸" & $i
$aBoy[$i] = "兒子" & $i
$aGirl[$i] = "奶奶" & $i
$aGirl[$i] = "媽媽" & $i
$aGirl[$i] = "女兒" & $i
Next
Local $aShowdata = $aBoy
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$hGUI = GUICreate("ListView虛擬清單 載入[" & $iITEM_COUNT & "]數據", 500, 350)
$hListView = GUICtrlCreateListView("Item1|nSubItem1|nSubItem2", 2, 2, 494, 294, $LVS_SHOWSELALWAYS + $LVS_OWNERDATA, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER)
;設置列寬
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 0, 150)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 1, 150)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 2, 150)
GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);分配清單記憶體。 為什麼要這樣做?因為虛擬清單必須要知道資料總量
;創建並關聯圖像清單
$hImage = _GUIImageList_Create()
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 115)
_GUICtrlListView_SetImageList($hListView, $hImage, 1)
$Button1 = GUICtrlCreateButton("男", 20, 310, 75, 25)
$Button2 = GUICtrlCreateButton("女", 100, 310, 75, 25)
$Button3 = GUICtrlCreateButton("修改", 180, 310, 75, 25)
$Button4 = GUICtrlCreateButton("清空", 260, 310, 75, 25)
GUISetState()
Local $iCol
While 1
$iMsg = GUIGetMsg()
Switch $iMsg
Case -3
ExitLoop
Case $Button1
$aShowdata = $aBoy
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button2
$aShowdata = $aGirl
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button3
If StringLen($sSomeString) Then
$sSomeString = ""
Else
$sSomeString = Asc(Random(48, 255, 1))
EndIf
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button4
$aShowdata = 0
$iITEM_COUNT = 0
GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0)
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
EndSwitch
WEnd
GUIDelete()
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo, $s
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Local $iIndex = DllStructGetData($tNMHDR, 'Index')
Switch $iIDFrom
Case $hListView
Switch $iCode
Case $LVN_COLUMNCLICK ; 點擊列頭排序
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
$iSub = DllStructGetData($tInfo, "SubItem")
_ArraySort($aShowdata, $B_DESCENDING[$iSub], 0, 0, $iSub)
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
$B_DESCENDING[$iSub] = Not $B_DESCENDING[$iSub]
Case $NM_DBLCLK ; 按兩下
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$iSub = DllStructGetData($tInfo, "SubItem")
MsgBox(0, $iIndex, $aShowdata[$iIndex][$iSub])
Case - 150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177
If Not IsArray($aShowdata) Then ContinueCase
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
$iIndex = Int(DllStructGetData($tInfo, "Item"))
$iSub = Int(DllStructGetData($tInfo, "SubItem"))
IF BitAND(DllStructGetData($tInfo, "Mask"), $LVIF_IMAGE) And $iSub = 0 THEN DllStructSetData($tInfo, "Image", 0);設置列圖像
$s = $aShowdata[$iIndex][$iSub] & "-" & $sSomeString
;If $aShowdata[$iIndex]="爺爺10" Then $iSTR=$iIndex
DllStructSetData($tText, "Text", $s);列資料放入$tText結構
DllStructSetData($tInfo, "Text", DllStructGetPtr($tText));用$tText結構的指標來設置列資料
DllStructSetData($tInfo, "TextMax", StringLen($s));設置列數據長度
Case $NM_CUSTOMDRAW
;For $i = 0 To UBound($aShowdata) - 1 Step 1
;If $aShowdata[$i]="爺爺10" Then
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC
#cs
Switch $iItem
Case $iIndex
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndSwitch
#ce
;#cs
For $i = 0 To UBound($aShowdata) - 1 Step 1
If $aShowdata[$i]="爺爺10" Then
Switch $iItem
Case $i
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndIf
Next
;#ce
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR() 回复 9# kk_lee69
这么写就没必要了,先获取整列数组,在数组里把所有关键字的索引存到另一个数组里,直接for...in...next For $i = 0 To UBound($aShowdata) - 1 Step 1关键是这个循环有很多是没用的 $aTemp = _arrayfindall($aShowdata, '爺爺10')
for $i in $aTemp 回复 12# netegg
新的方案 解決了 我擔心的效率問題.......
理由是不管我 把搜尋的 判斷 放在哪裡跑完 一萬多筆 的循環都需要點時間
後來找到一個 一個位置直接在 listview 繪圖的時候 判斷處理
這樣不用在多做一個循環 判斷效率好些..........
;~ 關於虛擬列表的一份說明
;~ http://blog.vckbase.com/iwaswzq/archive/2006/07/07/21113.aspx
#cs ____________________________________
Au3 版本: 3.3.9.0
腳本作者: CrossDoor
電子郵件: 382869232@qq.com
QQ/TM: 382869232
#ce _______________腳本開始_________________
#Region ;**** 參數創建於 ACNWrapper_GUI ****
#PRE_UseX64=n
#EndRegion ;**** 參數創建於 ACNWrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#include <FontConstants.au3>
DIM $aTemp
Global $Font1 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_BOLD)
Global $Font2 = _WinAPI_CreateFont(14, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, _
$CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, "MS Sans Serif")
Global $B_DESCENDING ; 排序用陣列
Local $tText = DllStructCreate("wchar Text");建個結構,用來放listview列資料
Local $GUI, $hImage, $iITEM_COUNT = 10000
Local $sSomeString = ""
Local $aBoy[$iITEM_COUNT], $aGirl[$iITEM_COUNT]
For $i = 0 To $iITEM_COUNT - 1;初始化陣列
$aBoy[$i] = "爺爺" & $i
$aBoy[$i] = "爸爸" & $i
$aBoy[$i] = "兒子" & $i
$aGirl[$i] = "奶奶" & $i
$aGirl[$i] = "媽媽" & $i
$aGirl[$i] = "女兒" & $i
Next
Local $aShowdata = $aBoy
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$hGUI = GUICreate("ListView虛擬清單 載入[" & $iITEM_COUNT & "]數據", 500, 350)
$hListView = GUICtrlCreateListView("Item1|nSubItem1|nSubItem2", 2, 2, 494, 294, $LVS_SHOWSELALWAYS + $LVS_OWNERDATA, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER)
;設置列寬
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 0, 150)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 1, 150)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 2, 150)
GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);分配清單記憶體。 為什麼要這樣做?因為虛擬清單必須要知道資料總量
;創建並關聯圖像清單
$hImage = _GUIImageList_Create()
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 115)
_GUICtrlListView_SetImageList($hListView, $hImage, 1)
$Button1 = GUICtrlCreateButton("男", 20, 310, 75, 25)
$Button2 = GUICtrlCreateButton("女", 100, 310, 75, 25)
$Button3 = GUICtrlCreateButton("修改", 180, 310, 75, 25)
$Button4 = GUICtrlCreateButton("清空", 260, 310, 75, 25)
GUISetState()
Local $iCol
While 1
$iMsg = GUIGetMsg()
Switch $iMsg
Case -3
ExitLoop
Case $Button1
$aShowdata = $aBoy
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button2
$aShowdata = $aGirl
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button3
If StringLen($sSomeString) Then
$sSomeString = ""
Else
$sSomeString = Asc(Random(48, 255, 1))
EndIf
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button4
_ArrayDisplay($aTemp)
; $aShowdata = 0
; $iITEM_COUNT = 0
;GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0)
;GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
;GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
EndSwitch
WEnd
GUIDelete()
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo, $s
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Local $iIndex = DllStructGetData($tNMHDR, 'Index')
Switch $iIDFrom
Case $hListView
Switch $iCode
Case $LVN_COLUMNCLICK ; 點擊列頭排序
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
$iSub = DllStructGetData($tInfo, "SubItem")
_ArraySort($aShowdata, $B_DESCENDING[$iSub], 0, 0, $iSub)
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
$B_DESCENDING[$iSub] = Not $B_DESCENDING[$iSub]
Case $NM_DBLCLK ; 按兩下
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$iSub = DllStructGetData($tInfo, "SubItem")
MsgBox(0, $iIndex, $aShowdata[$iIndex][$iSub])
Case - 150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177
If Not IsArray($aShowdata) Then ContinueCase
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
$iIndex = Int(DllStructGetData($tInfo, "Item"))
$iSub = Int(DllStructGetData($tInfo, "SubItem"))
IF BitAND(DllStructGetData($tInfo, "Mask"), $LVIF_IMAGE) And $iSub = 0 THEN DllStructSetData($tInfo, "Image", 0);設置列圖像
$s = $aShowdata[$iIndex][$iSub] & "-" & $sSomeString
Dim$aTemp
If $aShowdata[$iIndex]="爺爺10"OR$aShowdata[$iIndex]="爺爺100"OR$aShowdata[$iIndex]="爺爺1000" Then _ArrayAdd ( $aTemp , $iIndex)
DllStructSetData($tText, "Text", $s);列資料放入$tText結構
DllStructSetData($tInfo, "Text", DllStructGetPtr($tText));用$tText結構的指標來設置列資料
DllStructSetData($tInfo, "TextMax", StringLen($s));設置列數據長度
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
Local $iColor, $hDC
for $i in $aTemp
if $I <>"" Then
Switch $iItem
Case $i
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndIf
Next
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>RGB2BGR() 回复 13# kk_lee69
这个没测试过,不太清楚了,反正个人满意就行了 回复 12# netegg
蛋大 有個細節我還沒處理好這個有點困擾想跟您請教一下
下面語法中
其實當$i ="" OR $I=0這兩種情況 都會被擋掉............我該怎麼寫 不會 當 $I=0 時也會被擋掉.....
我記得好像有人問過類似$I="" 與 $=0 的 判斷邏輯
for $i in $aTemp
if $I <>"" Then
Switch $iItem
Case $i
$hDC = DllStructGetData($tCustDraw, "hdc")
If $iSubItem = 0 Then
$iColor = RGB2BGR(0xFF0000)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
ElseIf $iSubItem = 1 Then
$iColor = RGB2BGR(0x000000)
_WinAPI_SelectObject($hDC, $Font2)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFF80))
ElseIf $iSubItem = 2 Then
$iColor = RGB2BGR(0x008080)
_WinAPI_SelectObject($hDC, $Font1)
DllStructSetData($tCustDraw, "clrText", $iColor)
DllStructSetData($tCustDraw, "clrTextBk", RGB2BGR(0xFFFFFF))
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndIf
Next
页:
[1]
2