如何获取Listview第2行控件行标识?【已解决】
本帖最后由 blue_dvd 于 2016-11-24 21:48 编辑如何获取Listview第2行控件行标识?
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
Example()
Func Example()
GUICreate("列表视图控件", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x00E0FFFF) ; 将改变背景颜色
Local $idListview = GUICtrlCreateListView("列 1|列 2|列 3", 10, 10, 200, 150) ;,$LVS_SORTDESCENDING)
Local $idButton = GUICtrlCreateButton("测试", 75, 170, 70, 20)
Local $idItem1 = GUICtrlCreateListViewItem("条目2|col22|col23", $idListview)
Local $idItem2 = GUICtrlCreateListViewItem("条目1|子目2|子目3", $idListview)
Local $idItem3 = GUICtrlCreateListViewItem("条目3|col32|col33", $idListview)
GUICtrlCreateInput("", 20, 200, 150)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; 允许拖动操作
GUISetState(@SW_SHOW)
MsgBox(0, "第2行数据标识", $i)
EndFunc ;==>Example
GUICtrlListView_GetItemText
--------------------------------------------------------------------------------
取得一个项目或者子项目的文本
#Include <GuiListView.au3>
_GUICtrlListView_GetItemText($hWnd, $iIndex[, $iSubItem = 0])
参数
$hWnd 控件句柄
$iIndex 基于0开始的项目索引
$iSubItem [可选参数] 基于1开始的子项目索引
返回值
返回项目或者子项目文本
注意/说明
要取得一个项目文本, 设置 iSubItem 为 0. 要取得一个子项目文本, 设置 iSubItem 为
基于1开始的子项目索引. 回复 2# ap112
不是我想要的,我主要看到Listview间隔底色代码
$l_item = GUICtrlCreateListViewItem( $l_listview_item, Eval($p_listview) )
$l_i1 = $l_i1 + 1
If Mod($l_i1+2 , 2) = 0 Then
GUICtrlSetBkColor($l_item, 0xFFFFFF)
Else
GUICtrlSetBkColor($l_item, 0xA2B5CD)
EndIf
但运行不停的闪,且大数据慢
所以想先写listview,再得到行的标识,设置颜色,可惜不成功! 本帖最后由 kk_lee69 于 2016-11-23 13:41 编辑
回复 3# blue_dvd
幫你改好了你是看看 下面的範例吧 !!!
至於原理請參考
下面的文章
http://www.autoitx.com/thread-50083-1-1.html
#PRE_UseX64=n
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <array.au3>
#include <Misc.au3>
Global $B_DESCENDING ; 排序用陣列
Global $hActLst
Global $bCtrlDown = False;listview控制項ctrl鍵按下標識
Global $hEdit, $Item = -1, $SubItem = 0
Global $hEnter, $aAccelKeys = [["{ENTER}", $hEnter]];虛擬控制項的控制碼和綁定的快速鍵
Local $hUser32 = DllOpen("user32.dll")
Local $ichked = 0;勾選的行數
Local $tText = DllStructCreate("wchar Text");建個結構,用來放listview列資料
Local $GUI, $hImage
Local $iITEM_COUNT = 1000, $aShowdata[$iITEM_COUNT], $aShowdata2[$iITEM_COUNT], $aShowdata3[$iITEM_COUNT];注意陣列最大專案數為2^24(16777216),所以$iITEM_COUNT = 4194304為二維陣列$aShowdata最大行數
Local $sSomeString = ""
Local $hGUI, $hListView, $hListView2, $hListView3
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$hGUI = GUICreate("ListView虛表 載入[" & UBound($aShowdata) & "]資料 核取方塊 原地編輯例子 By_Crossdoor", 600, 605)
$hListView = GUICtrlCreateListView("$hListView| | ", 2, 2, 295, 296, BitOR($LVS_REPORT, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_OWNERDATA), $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;~ _WinAPI_SetWindowTheme(GUICtrlGetHandle($hListView), "Explorer");設置主題
;設置列寬
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 0, 110)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlSendMsg($hListView, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);設定數據總數
$hListView2 = GUICtrlCreateListView("$hListView2| | ", 300, 2, 295, 296, BitOR($LVS_REPORT, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_OWNERDATA), $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;~ _WinAPI_SetWindowTheme(GUICtrlGetHandle($hListView), "Explorer");設置主題
;設置列寬
GUICtrlSendMsg($hListView2, $LVM_SETCOLUMNWIDTH, 0, 110)
GUICtrlSendMsg($hListView2, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg($hListView2, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlSendMsg($hListView2, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);設定數據總數
$hListView3 = GUICtrlCreateListView("$hListView3| | ", 300, 300, 295, 296, BitOR($LVS_REPORT, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_OWNERDATA), $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;~ _WinAPI_SetWindowTheme(GUICtrlGetHandle($hListView), "Explorer");設置主題
;設置列寬
GUICtrlSendMsg($hListView3, $LVM_SETCOLUMNWIDTH, 0, 110)
GUICtrlSendMsg($hListView3, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg($hListView3, $LVM_SETCOLUMNWIDTH, 2, 80)
GUICtrlSendMsg($hListView3, $LVM_SETITEMCOUNT, $iITEM_COUNT, 0);設定數據總數
;===========創建並關聯圖像清單
$hImage = _GUIImageList_Create()
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110)
_GUICtrlListView_SetImageList($hListView, $hImage, 1);分配圖像清單到清單視圖控制項$hListView
_GUICtrlListView_SetImageList($hListView2, $hImage, 1);分配圖像清單到清單視圖控制項$hListView2
$Button1 = GUICtrlCreateButton("選中行", 10, 310, 70, 25)
$Button2 = GUICtrlCreateButton("未選行", 80, 310, 70, 25)
$Button3 = GUICtrlCreateButton("修改", 150, 310, 70, 25)
$Button4 = GUICtrlCreateButton("清空", 220, 310, 70, 25)
GUISetState()
;==================在表單顯示後再初始化資料,因為初始化陣列資料比較慢
;如果先初始化陣列後顯示視窗,會卡很久,就造成虛表也很慢的假像
For $i = 0 To $iITEM_COUNT - 1
;重繪 刷新 listview 0-13行(讓ListView顯示陣列資料),不用太多行,只需要一屏的行數就就可以
;因為下拉滾動的時候listview會收到LVN_GETDISPINFO消息,這個消息我們在WM_NOTIFY內處理過了
If $i = 18 Then
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, $i)
GUICtrlSendMsg($hListView2, $LVM_REDRAWITEMS, 0, $i)
GUICtrlSendMsg($hListView3, $LVM_REDRAWITEMS, 0, $i)
EndIf
$aShowdata[$i] = "爺爺" & $i
$aShowdata[$i] = "爸爸" & $i
$aShowdata[$i] = "兒子" & $i
$aShowdata[$i] = 4096;核取方塊狀態4096未選中 8192選中
$aShowdata2[$i] = "2爺爺" & $i
$aShowdata2[$i] = "2爸爸" & $i
$aShowdata2[$i] = "2兒子" & $i
$aShowdata2[$i] = 4096;核取方塊狀態4096未選中 8192選中
$aShowdata3[$i] = "3爺爺" & $i
$aShowdata3[$i] = "3爸爸" & $i
$aShowdata3[$i] = "3兒子" & $i
$aShowdata3[$i] = 4096;核取方塊狀態4096未選中 8192選中
Next
While 1
$bCtrlDown = _IsPressed("11", $hUser32);取ctrl按鍵的狀態
Switch GUIGetMsg()
Case -3
ExitLoop
Case $Button1
$aShowdata = 8192
$aShowdata = 8192
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 3, 7)
;~ $a = _GetChkIndex(8192)
;~ If $a = -1 Then
;~ MsgBox(32, "提示", "未勾選任何數據")
;~ Else
;~ _ArrayDisplay($a, "勾選行的索引");陣列很大時,_ArrayDisplay會非常耗時
;~ EndIf
Case $Button2
$a = _GetChkIndex(4096)
If $a = -1 Then
MsgBox(32, "提示", "數據全都被勾選了。")
Else
;MsgBox(32, "提示", "未勾選的數據有" & UBound($a) & "行")
_ArrayDisplay($a, "未勾選行的索");陣列很大時,_ArrayDisplay會非常耗時
EndIf
Case $Button3
If StringLen($sSomeString) Then
$sSomeString = ""
Else
$sSomeString = Asc(Random(48, 255, 1))
EndIf
For $i = 0 To $iITEM_COUNT - 1
$aShowdata[$i] = "爺爺" & $i & "-" & $sSomeString
$aShowdata[$i] = "爸爸" & $i & "-" & $sSomeString
$aShowdata[$i] = "兒子" & $i & "-" & $sSomeString
Next
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()
DllClose($hUser32)
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iIDFrom
Case $hListView
Switch $iCode
Case $LVN_COLUMNCLICK ; 點擊列頭排序
_SortItems($iIDFrom, $ilParam, $aShowdata)
Case $NM_CUSTOMDRAW;自繪消息處理選中行背景色
Local $tCustDraw = DllStructCreate("hwnd hwndFrom;int idFrom;int code;dword DrawStage;hwnd hdc;long rect;dword ItemSpec;int ItemState;dword Itemlparam;dword clrText;dword clrTextBk;int SubItem; dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText;int Align", $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "DrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
_SetchkedBkClr($tCustDraw, $aShowdata)
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED ; An item has changed
_CtrlSel($iIDFrom, $ilParam, $aShowdata)
Case $LVN_BEGINSCROLL ; 滾動事件開始
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata);如果存在$hEdit控制項,則結束編輯
Case $NM_CLICK;按一下
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata)
_chkboxState($iIDFrom, $ilParam, $aShowdata);刷新核取方塊狀態
Case $NM_DBLCLK ; 按兩下
_StartItemEdit($iIDFrom, $ilParam, $aShowdata);開始原地編輯
Case -150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177 請求顯示資料
_LVN_GETDISPINFO($iIDFrom, $ilParam, $aShowdata)
EndSwitch
Case $hListView2
Switch $iCode
Case -150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177 請求顯示資料
_LVN_GETDISPINFO($iIDFrom, $ilParam, $aShowdata2)
Case $LVN_COLUMNCLICK ; 點擊列頭排序
_SortItems($iIDFrom, $ilParam, $aShowdata2)
Case $NM_CUSTOMDRAW;自繪消息處理選中行背景色
Local $tCustDraw = DllStructCreate("hwnd hwndFrom;int idFrom;int code;dword DrawStage;hwnd hdc;long rect;dword ItemSpec;int ItemState;dword Itemlparam;dword clrText;dword clrTextBk;int SubItem; dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText;int Align", $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "DrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
_SetchkedBkClr($tCustDraw, $aShowdata2, 0x738393)
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED ; An item has changed
_CtrlSel($iIDFrom, $ilParam, $aShowdata2)
Case $LVN_BEGINSCROLL ; 滾動事件開始
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata2);如果存在$hEdit控制項,則結束編輯
Case $NM_CLICK;按一下
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata2)
_chkboxState($iIDFrom, $ilParam, $aShowdata2)
Case $NM_DBLCLK ; 按兩下
_StartItemEdit($iIDFrom, $ilParam, $aShowdata2)
EndSwitch
Case $hListView3
Switch $iCode
Case -150, -177 ;$LVN_GETDISPINFOA = -150, $LVN_GETDISPINFOW = -177 請求顯示資料
_LVN_GETDISPINFO($iIDFrom, $ilParam, $aShowdata3)
Case $LVN_COLUMNCLICK ; 點擊列頭排序
_SortItems($iIDFrom, $ilParam, $aShowdata3)
Case $NM_CUSTOMDRAW;自繪消息處理選中行背景色
Local $tCustDraw = DllStructCreate("hwnd hwndFrom;int idFrom;int code;dword DrawStage;hwnd hdc;long rect;dword ItemSpec;int ItemState;dword Itemlparam;dword clrText;dword clrTextBk;int SubItem; dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText;int Align", $ilParam)
Local $iDrawStage = DllStructGetData($tCustDraw, "DrawStage")
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
_SetchkedBkClr($tCustDraw, $aShowdata3, 0xD307AD, 0xE3F7ED)
Return $CDRF_NEWFONT
Case $LVN_ITEMCHANGED ; An item has changed
_CtrlSel($iIDFrom, $ilParam, $aShowdata3)
Case $LVN_BEGINSCROLL ; 滾動事件開始
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata3);如果存在$hEdit控制項,則結束編輯
Case $NM_CLICK;按一下
If IsHWnd($hEdit) Then _EndItemEdit($iIDFrom, $aShowdata3)
_chkboxState($iIDFrom, $ilParam, $aShowdata3)
Case $NM_DBLCLK ; 按兩下
_StartItemEdit($iIDFrom, $ilParam, $aShowdata3)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
$iIDFrom = _WinAPI_LoWord($wParam)
$iCode = _WinAPI_HiWord($wParam)
Switch $lParam
Case $hEdit
Switch $iCode
Case $EN_KILLFOCUS;編輯方塊失去焦點,結束編輯
Switch $hActLst
Case $hListView
_EndItemEdit($hListView, $aShowdata)
Case $hListView2
_EndItemEdit($hListView2, $aShowdata2)
Case $hListView3
_EndItemEdit($hListView3, $aShowdata3)
EndSwitch
Case $EN_UPDATE
EndSwitch
Case $hEnter;虛擬控制項消息觸發收到回車消息才觸發
If IsHWnd($hEdit) Then
If $SubItem = 0 Then
__ClickListViewItem($hActLst, $Item, True);使用點擊來結束編輯,省去判斷listview的資料陣列
Else
__ClickListViewItem($hActLst, $Item)
EndIf
EndIf
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Func _SetchkedBkClr(ByRef $tCustDraw, ByRef $aDataArray, $ihighBkClr = 0xF7D3AD, $idefBkclr = 0xFFFFFF)
Local $iIndex = DllStructGetData($tCustDraw, "ItemSpec")
If MOD($iIndex,2) = 0 Then
DllStructSetData($tCustDraw, "clrTextBk", $idefBkclr);BGR格式顏色值 默認背景色
Else
DllStructSetData($tCustDraw, "clrTextBk", $ihighBkClr);BGR格式顏色值 高亮背景色
EndIf
EndFunc
Func _chkboxState($hlst, $ilParam, ByRef $aDataArray);刷新核取方塊狀態
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$x = DllStructGetData($tInfo, "X")
If ($x < 16) And (3 < $x) And (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hlst), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then;使用點擊的x座標來判斷是否在核取方塊上點擊
If $aDataArray[$iIndex] = 4096 Then
$ichked+=1
$aDataArray[$iIndex] = 8192;4096未選中 8192選中
Else
$ichked-=1
$aDataArray[$iIndex] = 4096;4096未選中 8192選中
EndIf
;====立刻刷新核取方塊顯示狀態 重繪$iIndex項
GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, $iIndex, $iIndex)
EndIf
EndFunc
Func _SortItems($hlst, $ilParam, ByRef $aDataArray);排序
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
$iSub = DllStructGetData($tInfo, "SubItem")
_ArraySort($aDataArray, $B_DESCENDING[$iSub], 0, 0, $iSub)
GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, 0, 20)
$B_DESCENDING[$iSub] = Not $B_DESCENDING[$iSub]
EndFunc
Func _CtrlSel($hlst, $ilParam, ByRef $aDataArray);按住ctrl鍵點選item
If $bCtrlDown Then;ctrl鍵按下
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
Local $iIndex = DllStructGetData($tInfo, "Item")
Local $iNewState = DllStructGetData($tInfo, "NewState")
If $iIndex > -1 And $iNewState = BitOR($LVIS_FOCUSED, $LVIS_SELECTED) Then
If $aDataArray[$iIndex] = 4096 Then
$ichked+=1
$aDataArray[$iIndex] = 8192;4096未選中 8192選中
Else
$ichked-=1
$aDataArray[$iIndex] = 4096;4096未選中 8192選中
EndIf
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
DllStructSetData($tInfo, "State", $aDataArray[$iIndex]);設置核取方塊狀態
;====立刻刷新核取方塊顯示狀態 重繪$iIndex項
GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, $iIndex, $iIndex)
EndIf
EndIf
EndFunc
Func _LVN_GETDISPINFO($hlst, $ilParam, ByRef $aDataArray)
If Not IsArray($aDataArray) Then Return
$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);判斷是否有LVIF_IMAGE屬性,有則設置列圖像。不設置_GUICtrlListView_SetImageList則無
If (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hlst), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then
;===============設置核取方塊資訊
DllStructSetData($tInfo, "Mask", BitOR($LVIF_STATE, DllStructGetData($tInfo, "Mask")))
DllStructSetData($tInfo, "StateMask", $LVIS_STATEIMAGEMASK)
DllStructSetData($tInfo, "State", $aDataArray[$iIndex])
EndIf
DllStructSetData($tText, "Text", $aDataArray[$iIndex][$iSub]);列資料放入$tText結構
DllStructSetData($tInfo, "Text", DllStructGetPtr($tText));$tText結構的指標來設置列資料
DllStructSetData($tInfo, "TextMax", StringLen($aDataArray[$iIndex][$iSub]));設置列數據字串長
EndFunc
Func _StartItemEdit($hlst, $ilParam, ByRef $aDataArray)
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$iSub = DllStructGetData($tInfo, "SubItem")
$x = DllStructGetData($tInfo, "X")
If ($iIndex <> -1) And ($iSub >= 0) And ($x > 16) Then;點擊的x>16才回應原地編輯
$hActLst = $hlst
If _GUICtrlListView_EnsureVisible($hlst, $iIndex) Then GUICtrlSendMsg($hlst, $LVM_REDRAWITEMS, $iIndex, $iIndex + 1);確保列表視圖專案可見
Local $alstPos = ControlGetPos($hGUI, "", $hlst)
$Item = $iIndex
$SubItem = $iSub
;==========計算編輯方塊座標
Local $aRect = _GUICtrlListView_GetSubItemRect($hlst, $iIndex, $iSub)
Local $iEdit_X = $aRect + $alstPos + 4
Local $iEdit_Y = $aRect + $alstPos + 3
Local $iEdit_Width = _GUICtrlListView_GetColumnWidth($hlst, $iSub) - 1
Local $iEdit_Height = $aRect - $aRect
If $iSub = 0 Then
Local $i1 = 0, $i2 = 0
If (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hlst), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then $i1 = 17;判斷是否有核取方塊
If _GUICtrlListView_GetImageList($hlst, 1) Then $i2 = 19;判斷是否有分配圖像清單到清單視圖控制項
$iEdit_X += ($i2 + $i1)
$iEdit_Width -= ($i2 + $i1)
EndIf
Local $iEdit = GUICtrlCreateInput($aDataArray[$iIndex][$iSub], $iEdit_X, $iEdit_Y, $iEdit_Width, $iEdit_Height, BitOR($WS_CHILD, $WS_VISIBLE, $ES_LEFT))
GUICtrlSetFont($iEdit, 11)
GUICtrlSetState($iEdit, $GUI_FOCUS)
$hEdit = GUICtrlGetHandle($iEdit)
_WinAPI_BringWindowToTop($hEdit)
_GUICtrlEdit_SetSel($hEdit, 0, -1) ;全選編輯方塊文字
Local $iEnter = GUICtrlCreateDummy()
$hEnter = GUICtrlGetHandle($iEnter)
GUISetAccelerators($aAccelKeys, $hGUI);在虛擬控制項上綁定一個回車快捷按鍵
EndIf
EndFunc
Func _EndItemEdit($hlst, ByRef $aDataArray)
If ($Item < UBound($aDataArray, 1)) And ($SubItem < UBound($aDataArray, 2)) And ($hActLst == $hlst) Then
Local $sText = _GUICtrlEdit_GetText($hEdit)
_GUICtrlListView_SetItemText($hlst, $Item, $sText, $SubItem)
If IsHWnd($hEdit) Then _WinAPI_DestroyWindow($hEdit)
If IsHWnd($hEnter) Then _WinAPI_DestroyWindow($hEnter)
$hEnter = Null
$hEdit = Null
If ($Item >= 0) Then $aDataArray[$Item][$SubItem] = $sText
$Item = -1
$SubItem = 0
EndIf
EndFunc
Func _GetChkIndex($iChkState)
Local $aIndex = [-1], $j = 0
If $ichked = 0 Then Return $aIndex
If $iChkState = 8192 Then
ReDim $aIndex[$ichked]
Else
ReDim $aIndex
EndIf
For $i = 0 To UBound($aShowdata) -1
If $aShowdata[$i] == $iChkState Then
$aIndex[$j] = $i
$j+=1
EndIf
Next
Return $aIndex
EndFunc
Func __ClickListViewItem($hWnd, $iIndex, $baddX = False)
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
Local $tRECT = _GUICtrlListView_GetItemRectEx($hWnd, $iIndex, $LVIR_LABEL)
Local $tPoint = _WinAPI_PointFromRect($tRECT, True)
$tPoint = _WinAPI_ClientToScreen($hWnd, $tPoint)
Local $iX, $iY
_WinAPI_GetXYFromPoint($tPoint, $iX, $iY)
If $baddX Then $iX += DllStructGetData($tRECT, "Right")
Local $iMode = Opt("MouseCoordMode", 1)
Local $aPos = MouseGetPos()
MouseClick("left", $iX, $iY, 1, 0)
MouseMove($aPos, $aPos, 0)
Opt("MouseCoordMode", $iMode)
EndFunc
回复 4# kk_lee69
兄弟,LZ要的可能没这么复杂
回复 4# kk_lee69
太复杂了吧!有简单些的吗? 回复 5# chzj589
这个功能是我要的,有代码吗?简单的 回复chzj589
这个功能是我要的,有代码吗?简单的
blue_dvd 发表于 2016-11-23 14:47 http://www.autoitx.com/images/common/back.gif
是没想到?加个背景颜色
Local $idItem1 = GUICtrlCreateListViewItem("条目2|col22|col23", $idListview)
Local $idItem2 = GUICtrlCreateListViewItem("条目1|子目2|子目3", $idListview)
Local $idItem3 = GUICtrlCreateListViewItem("条目3|col32|col33", $idListview)
GUICtrlSetBkColor($idItem1, 0xFFFFFF)
GUICtrlSetBkColor($idItem2, 0xA2B5CD)
GUICtrlSetBkColor($idItem3, 0x009900)
回复 5# chzj589
已經不用傳統 LISTVIEW了所以 呵呵簡單的不會寫了@@ 本帖最后由 chzj589 于 2016-11-23 17:48 编辑
回复chzj589
已經不用傳統 LISTVIEW了所以 呵呵簡單的不會寫了@@
kk_lee69 发表于 2016-11-23 15:46 http://www.autoitx.com/images/common/back.gif
请问一下:我那个LISTVIEW导入Excel,一千条数据,导入时间25秒。
这样的导入速度怎么样?
而整表导入才不到2秒,差了十多倍
回复 10# chzj589
酷 回复 8# chzj589
GUICtrlCreateListViewItem;
GUICtrlSetBkColor
这两个函数在一起可以实现,但是太慢了;
我是想先利用整表写入,快很多,然后再搞背景色,不知为何就是不行! 回复 12# blue_dvd
怎麼會不行 怎麼不把你的程式貼出來看看?? 回复chzj589 这两个函数在一起可以实现,但是太慢了;
我是想先利用整表写入,快很多,然后再搞背景色, ...
blue_dvd 发表于 2016-11-23 21:31 http://www.autoitx.com/images/common/back.gif
要把你提出的问题说清楚,是用什么写入显示在Listview?
要叫人猜? 本帖最后由 blue_dvd 于 2016-11-24 11:21 编辑
回复 14# chzj589
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ColorConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 591, 349, 192, 114)
$ListView1 = GUICtrlCreateListView("", 24, 24, 546, 302)
GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
_GUICtrlListView_AddColumn($ListView1, "Item1",100)
_GUICtrlListView_AddColumn($ListView1, "nSubItem1", 100)
_GUICtrlListView_AddColumn($ListView1, "nSubItem2", 100)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $GUI, $hImage, $iITEM_COUNT =1000
Local $sSomeString = ""
Local $aBoy[$iITEM_COUNT]
For $i = 0 To $iITEM_COUNT - 1;初始化数组
$aBoy[$i] = "爷爷" & $i
$aBoy[$i] = "爸爸" & $i
$aBoy[$i] = "儿子" & $i
Next
Local $aShowdata = $aBoy
for $i=0 toUBound($aShowdata ,1)-1
$t_a=$aShowdata[$i]
For $j=1 ToUBound($aShowdata,2) - 1
$t_a=$t_a&"|"&$aShowdata[$i][$j]
Next
;MsgBox(0,"提醒", $t_a)
GUICtrlCreateListViewItem($t_a,$ListView1)
Next
;设置列宽
For $i = 0 To UBound($aShowdata ,2) - 1
_GUICtrlListView_SetColumnWidth($ListView1, $i, $LVSCW_AUTOSIZE) ;设置列宽还不懂
Next
local $ii=0
;设置行间隔颜色
For $i = 0 To UBound($aShowdata,1) - 1
ifmod($i,2)=0 Then
$ii= _GUICtrlListView_SetItemSelected($ListView1, $i)
$a = ControlListView($form1, "", $ListView1, "GetSelected", $ii)
GUICtrlSetBkColor(_GUICtrlListView_GetItemParam($ListView1, $a), $COLOR_BLUE) ;蓝色
EndIf
Next
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd这样就可以,有点闪,但是换成快速写入Listview就不行了!
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ColorConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 591, 349, 192, 114)
$ListView1 = GUICtrlCreateListView("", 24, 24, 546, 302)
GUICtrlSendMsg($ListView1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
_GUICtrlListView_AddColumn($ListView1, "Item1",100)
_GUICtrlListView_AddColumn($ListView1, "nSubItem1", 100)
_GUICtrlListView_AddColumn($ListView1, "nSubItem2", 100)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $GUI, $hImage, $iITEM_COUNT =1000
Local $sSomeString = ""
Local $aBoy[$iITEM_COUNT]
For $i = 0 To $iITEM_COUNT - 1;初始化数组
$aBoy[$i] = "爷爷" & $i
$aBoy[$i] = "爸爸" & $i
$aBoy[$i] = "儿子" & $i
Next
Local $aShowdata = $aBoy
_GUICtrlListView_AddArray($ListView1, $aShowdata)
;设置列宽
For $i = 0 To UBound($aShowdata ,2) - 1
_GUICtrlListView_SetColumnWidth($ListView1, $i, $LVSCW_AUTOSIZE) ;设置列宽还不懂
Next
local $ii=0
;设置行间隔颜色
For $i = 0 To UBound($aShowdata,1) - 1
ifmod($i,2)=0 Then
$ii= _GUICtrlListView_SetItemSelected($ListView1, $i)
$a = ControlListView($form1, "", $ListView1, "GetSelected", $ii)
GUICtrlSetBkColor(_GUICtrlListView_GetItemParam($ListView1, $a), $COLOR_BLUE) ;蓝色
EndIf
Next
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
页:
[1]
2