#include <GUIMenu.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
$MyGui = GUICreate('ListView项目重命名例子', 640, 480)
$hListView = _GUICtrlListView_Create($MyGui, '', 5, 5, 630, 460, $LVS_EDITLABELS)
_GUICtrlListView_SetView($hListView, 1)
_GUICtrlListView_EnableGroupView($hListView)
$hImage = _GUIImageList_Create(32, 32, 5, 1)
_GUICtrlListView_SetImageList($hListView, $hImage)
For $i = 0 To 39
_GUIImageList_AddIcon($hImage, 'Shell32.dll', 100 + $i, True)
_GUICtrlListView_AddItem($hListView, 'Item - ' & $i, $i)
$iNumber = _GUICtrlListView_GetCounterPage($hListView) - 1
_GUICtrlListView_InsertGroup($hListView, -1, 1, '')
_GUICtrlListView_SetItemGroupID($hListView, $iNumber, 1)
Next
$iMenu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$hMenu = GUICtrlGetHandle(-1)
$iMenuItem0 = GUICtrlCreateMenuItem('运行程序', $iMenu)
$iMenuItem1 = GUICtrlCreateMenuItem('修改名称', $iMenu)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Do
Until GUIGetMsg() = -3
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hListView
Switch $iCode
Case $LVN_BEGINLABELEDITA, $LVN_BEGINLABELEDITW; 开始编辑项目标签
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
Return False ; 允许编辑标签
;Return True ; 进制编辑标签
Case $LVN_ENDLABELEDITA, $LVN_ENDLABELEDITW
$tInfo = DllStructCreate($tagNMLVDISPINFO, $ilParam)
Local $tBuffer = DllStructCreate("WCHAR Text[" & DllStructGetData($tInfo, "TextMax") & "]", DllStructGetData($tInfo, "Text"))
$sNewItemName = DllStructGetData($tBuffer, "Text")
If $sNewItemName Then
TrayTip('重命名', '新名称:' & $sNewItemName, 1)
Return True
EndIf
; 如果文本非空, 返回真以设置项目标签为编辑的文本, 返回假拒绝
; 如果文本为空忽略返回值
Case $NM_DBLCLK
_Run()
Case $NM_RCLICK
$hMenu = GUICtrlGetHandle($iMenu)
Local $iMenuId = _GUICtrlMenu_TrackPopupMenu($hMenu, $MyGui, -1, -1, 1, 1, 2)
Switch $iMenuId
Case $iMenuItem0
_Run()
Case $iMenuItem1
$aItemIndex = _GUICtrlListView_GetSelectedIndices($hListView, True)
If $aItemIndex[0] Then
_GUICtrlListView_EditLabel($hListView, $aItemIndex[1])
EndIf
EndSwitch
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _Run()
$aItemIndex = _GUICtrlListView_GetSelectedIndices($hListView, True)
If $aItemIndex[0] Then
$sItemText = _GUICtrlListView_GetItemText($hListView, $aItemIndex[1])
MsgBox(0, '', $sItemText)
EndIf
EndFunc ;==>_Run
|