#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>
Global Enum $idOpen = 1000, $idSave, $idInfo
$Form1 = GUICreate("Form1", 402, 376, 337, 189)
$ListView1 = GUICtrlCreateListView("row1|row2|row3", 8, 8, 385, 329, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
GUICtrlCreateListViewItem("a1|a2|a3", $ListView1)
GUICtrlCreateListViewItem("b1|b2|b3", $ListView1)
GUICtrlCreateListViewItem("c1|c2|c3", $ListView1)
GUICtrlCreateListViewItem("d1|d2|d3", $ListView1)
$Button1 = GUICtrlCreateButton("讀取", 8, 344, 75, 25)
$Button2 = GUICtrlCreateButton("關閉", 320, 344, 75, 25)
$Button3 = GUICtrlCreateButton("清除", 110, 344, 75, 25)
$Button4 = GUICtrlCreateButton("建立", 210, 344, 75, 25)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") ;$WM_COMMAND:0x0111,当用户选择一条菜单命令项或当某个控件发送一条消息给它的父窗口
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU") ;$WM_CONTEXTMENU:0x007B,当用户某个窗口中点击了一下右键就发送此消息给这个窗口
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $Button2
Exit
Case $Button1
ListViewSelect()
Case $Button3
_GUICtrlListView_DeleteAllItems ($ListView1)
Case $Button4
GUICtrlCreateListViewItem("a1|a2|a3", $ListView1)
GUICtrlCreateListViewItem("b1|b2|b3", $ListView1)
GUICtrlCreateListViewItem("c1|c2|c3", $ListView1)
GUICtrlCreateListViewItem("d1|d2|d3", $ListView1)
EndSwitch
WEnd
Func ListViewSelect()
Dim $val[1],$j = 1
$totalnum = _GUICtrlListView_GetItemCount($ListView1)
$selectcount = _GUICtrlListView_GetSelectedCount($ListView1)
$val[0] = $selectcount
ReDim $val[$selectcount + 1]
For $i = 0 To $totalnum
If _GUICtrlListView_GetItemSelected($ListView1, $i) == True Then
$val[$j] = _GUICtrlListView_GetItemTextString($ListView1, $i)
$j += 1
EndIf
Next
_ArrayDisplay($val)
EndFunc
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Switch $wParam
Case $idOpen
_WinAPI_ShowMsg("你点击了 [打开] 菜单")
Case $idSave
_WinAPI_ShowMsg("你点击了 [保存] 菜单")
Case $idInfo
_WinAPI_ShowMsg("你点击了 [信息] 菜单")
EndSwitch
EndFunc ;==>WM_COMMAND
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $ilParam) ;$WM_CONTEXTMENU:0x007B,当用户某个窗口中点击了一下右键就发送此消息给这个窗口
Local $hMenu, $selectcount,$totalnum
Local $iRows = _GUICtrlListView_GetItemCount($ListView1) ;检索列表视图行数
If $iRows > 0 Then
For $i = 0 To $iRows
If _GUICtrlListView_GetItemSelected($ListView1, $i) == True Then
$hMenu = _GUICtrlMenu_CreatePopup() ;创建下拉菜单, 子菜单或快捷菜单.
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen) ;插入一个新菜单项到指定位置.(使用参数)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 2, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
_GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd) ;在指定位置显示一个快捷菜单
_GUICtrlMenu_DestroyMenu($hMenu) ;销毁指定菜单, 并释放菜单占用的内存
Return True
Else
_GUICtrlMenu_DestroyMenu($hMenu)
EndIf
Next
Else
_GUICtrlMenu_DestroyMenu($hMenu)
EndIf
EndFunc ;==>WM_CONTEXTMENU