本帖最后由 kk_lee69 于 2016-4-17 21:03 编辑
求由按钮控制LISTVIEW项目选中并且高亮
如下范例 程序执行后 按下按钮 選中
可设定 项目2 被选中 但是无法高亮 如第二张图
求修改 协助 由按钮 選中 按下后 可以 选中项目2 并且高亮
#cs ____________________________________
Au3 版本: 3.3.14.2
?本作者: CrossDoor
?子?件: 382869232@qq.com
QQ/TM: 382869232
#ce _______________?本?始_________________
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
;#include <WinAPISys.au3>
#include <GuiEdit.au3>
#include <array.au3>
Global $B_DESCENDING[3] ; 排序用??
Global $hEdit, $Item = -1, $SubItem = 0
Local $tText = DllStructCreate("wchar Text[512]");建??构,用?放listview列?据
Local $GUI, $hImage, $iITEM_COUNT = 10000
Local $sSomeString = ""
Local $aShowdata[$iITEM_COUNT][4]
For $i = 0 To $iITEM_COUNT - 1;初始化??
$aShowdata[$i][0] = "??" & $i
$aShowdata[$i][1] = "爸爸" & $i
$aShowdata[$i][2] = "儿子" & $i
$aShowdata[$i][3] = 4096;复?框?? 4096未?中 8192?中
Next
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$hGUI = GUICreate("ListView?表 加?[" & $iITEM_COUNT & "]?据 复?框 原地??例子 By_Crossdoor", 500, 350)
$hListView = GUICtrlCreateListView("Item1|nSubItem1|nSubItem2", 2, 2, 494, 294, $LVS_SHOWSELALWAYS + $LVS_OWNERDATA, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER + $LVS_EX_CHECKBOXES)
;?置列?
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", 110)
_GUICtrlListView_SetImageList($hListView, $hImage, 1);分配?像列表到列表??控件
$Button3 = GUICtrlCreateButton("修改", 180, 310, 75, 25)
$Button4 = GUICtrlCreateButton("選中", 260, 310, 75, 25)
GUISetState()
While 1
$iMsg = GUIGetMsg()
Switch $iMsg
Case -3
ExitLoop
Case $Button3
If StringLen($sSomeString) Then
$sSomeString = ""
Else
$sSomeString = Asc(Random(48, 255, 1))
EndIf
For $i = 0 To $iITEM_COUNT - 1
$aShowdata[$i][0] = "??" & $i & "-" & $sSomeString
$aShowdata[$i][1] = "爸爸" & $i & "-" & $sSomeString
$aShowdata[$i][2] = "儿子" & $i & "-" & $sSomeString
Next
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
Case $Button4
_GUICtrlListView_SetItemSelected($hListView,2,True,True)
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_CLICK;??
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
$iIndex = DllStructGetData($tInfo, "Index")
$x = DllStructGetData($tInfo, "X")
If ($x < 16) And (3 < $x) And (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hListView), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then;使用x坐??判?是否在复?框上??
If $aShowdata[$iIndex][3] = 4096 Then
$aShowdata[$iIndex][3] = 8192;4096未?中 8192?中
Else
$aShowdata[$iIndex][3] = 4096;4096未?中 8192?中
EndIf
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
DllStructSetData($tInfo, "State", $aShowdata[$iIndex][3]);?置复?框??
;====立刻刷新复?框?示?? 重?$iIndex-1到$iIndex+20的?
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, $iIndex - 1, $iIndex + 20)
EndIf
Case $NM_DBLCLK ; ??
$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才??原地??
$Item = $iIndex
$SubItem = $iSub
;==========?算??框坐?
Local $aRect = _GUICtrlListView_GetSubItemRect($hListView, $iIndex, $iSub)
Local $iEdit_X = $aRect[0] + 4
Local $iEdit_Y = $aRect[1] + 3
Local $iEdit_Width = _GUICtrlListView_GetColumnWidth($hListView, $iSub) + 3
Local $iEdit_Height = $aRect[3] - $aRect[1] + 3
If $iSub = 0 Then
Local $i1=0,$i2=0
If (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hListView), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then $i1=18;判?是否有复?框
If _GUICtrlListView_GetImageList($hListView,1) Then $i2=20;判?是否有分配?像列表到列表??控件
$iEdit_X += ($i2+$i1)
$iEdit_Width -= ($i2+$i1)
EndIf
$hEdit = _GUICtrlEdit_Create($hWnd, $aShowdata[$iIndex][$iSub], $iEdit_X, $iEdit_Y, $iEdit_Width, $iEdit_Height, BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT))
_WinAPI_SetFocus($hEdit)
Send("{RIGHT}")
_GUICtrlEdit_SetSel($hEdit, 0, -1)
HotKeySet("{ENTER}", "_EndItemEdit");?置????回?
EndIf
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);判?是否有LVIF_IMAGE?性,有??置列?像
If (BitAND(_GUICtrlListView_GetExtendedListViewStyle($hListView), $LVS_EX_CHECKBOXES) = $LVS_EX_CHECKBOXES) Then
;===============?置复?框信息
DllStructSetData($tInfo, "Mask", BitOR($LVIF_STATE,DllStructGetData($tInfo, "Mask")))
DllStructSetData($tInfo, "StateMask", $LVIS_STATEIMAGEMASK)
DllStructSetData($tInfo, "State", $aShowdata[$iIndex][3])
EndIf
DllStructSetData($tText, "Text", $aShowdata[$iIndex][$iSub]);列?据放入$tText?构
DllStructSetData($tInfo, "Text", DllStructGetPtr($tText));用$tText?构的指???置列?据
DllStructSetData($tInfo, "TextMax", StringLen($aShowdata[$iIndex][$iSub]));?置列?据?度
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
$iIDFrom = _WinAPI_LoWord($wParam)
$iCode = _WinAPI_HiWord($wParam)
Switch $lParam
Case $hEdit
Switch $iCode
Case $EN_KILLFOCUS
_EndItemEdit()
Case $EN_UPDATE
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
Func _EndItemEdit()
If ($Item < UBound($aShowdata,1)) And ($SubItem < UBound($aShowdata,2)) Then
Local $sText = _GUICtrlEdit_GetText($hEdit)
_GUICtrlListView_SetItemText($hListView, $Item, $sText, $SubItem)
_WinAPI_DestroyWindow($hEdit)
If ($Item >= 0) Then
$aShowdata[$Item][$SubItem] = $sText
GUICtrlSendMsg($hListView, $LVM_ENSUREVISIBLE, 1, 0)
GUICtrlSendMsg($hListView, $LVM_REDRAWITEMS, 0, 20)
EndIf
$Item = -1
$SubItem = 0
HotKeySet("{ENTER}");取消回???
EndIf
EndFunc
圖一 執行後結果
圖二 希望的結果
|