本帖最后由 haijie1223 于 2012-2-16 13:11 编辑
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>
Global $GUI1, $GUI2, $MainGUI, $ListView1, $ListView2
Global $Menu1, $Menu2
Global Enum $MenuAdd = 1000, $MenuEdit, $MenuDel
$MainGUI = GUICreate("主窗口", 800, 600)
$Button1 = GUICtrlCreateButton("按钮一", 60, 200)
$Button2 = GUICtrlCreateButton("按钮二", 120, 200)
$Menu1 = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($Menu1, 0, " 添加数据 ", $MenuAdd)
_GUICtrlMenu_InsertMenuItem($Menu1, 1, " 修改数据 ", $MenuEdit)
_GUICtrlMenu_InsertMenuItem($Menu1, 2, " 删除数据 ", $MenuDel)
$Menu2 = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($Menu2, 0, " 修改记录 ", $MenuEdit)
_GUICtrlMenu_InsertMenuItem($Menu2, 1, " 删除记录 ", $MenuDel)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
$GUI1 = GUICreate("窗口一", 400, 300, -1, -1, -1, -1, $MainGUI)
$ListView1 = GUICtrlCreateListView("ABCD|EFGH", 50, 50)
GUICtrlCreateListViewItem("1234|5678", -1)
GUISetState(@SW_HIDE)
$GUI2 = GUICreate("窗口二", 400, 300, -1, -1, -1, -1, $MainGUI)
$ListView2 = GUICtrlCreateListView("1234|5678", 50, 50)
GUICtrlCreateListViewItem("ABCD|EFGH", -1)
GUISetState(@SW_HIDE)
While 1
$Msg = GUIGetMsg()
If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
If $Msg = $Button1 Then
GUISetState(@SW_DISABLE, $MainGUI)
GUISetState(@SW_SHOW, $GUI1)
While 1
$Msg_child = GUIGetMsg()
If $Msg_child = $GUI_EVENT_CLOSE Then
GUISetState(@SW_ENABLE, $MainGUI)
GUISetState(@SW_HIDE,$GUI1)
ExitLoop
EndIf
WEnd
EndIf
If $Msg = $Button2 Then
GUISetState(@SW_DISABLE, $MainGUI)
GUISetState(@SW_SHOW, $GUI2)
While 1
$Msg_child = GUIGetMsg()
If $Msg_child = $GUI_EVENT_CLOSE Then
GUISetState(@SW_ENABLE, $MainGUI)
GUISetState(@SW_HIDE,$GUI2)
ExitLoop
EndIf
WEnd
EndIf
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iCode, $tNMHDR, $hWndListView1, $hWndListView2, $SelectedID, $hWndTab
$hWndListView1 = $ListView1
$hWndListView2 = $ListView2
If Not IsHWnd($ListView2) Then $hWndListView2 = GUICtrlGetHandle($ListView2)
If Not IsHWnd($ListView1) Then $hWndListView1 = GUICtrlGetHandle($ListView1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndListView1
Switch $iCode
Case $NM_RCLICK
If _GUICtrlListView_GetSelectedIndices($hWndListView1) = "" Then
_GUICtrlMenu_SetItemDisabled($Menu1, 1)
_GUICtrlMenu_SetItemDisabled($Menu1, 2)
Else
_GUICtrlMenu_SetItemEnabled($Menu1, 1)
_GUICtrlMenu_SetItemEnabled($Menu1, 2)
EndIf
$SelectedID = _GUICtrlMenu_TrackPopupMenu($Menu1, $hWndListView1, -1, -1, 1, 1, 2)
If $SelectedID = $MenuAdd Then
MsgBox(0, "", "您点击了添加")
EndIf
If $SelectedID = $MenuEdit Then
MsgBox(0, "", "您点击了编辑")
EndIf
If $SelectedID = $MenuDel Then
MsgBox(0, "", "您点击了删除")
EndIf
Case $NM_DBLCLK
If _GUICtrlListView_GetSelectedIndices($hWndListView1) <> "" Then MsgBox(0, "", "您点击了编辑")
EndSwitch
;~ Case $hWndListView2
Case $hWndListView2
Switch $iCode
Case $NM_RCLICK
If _GUICtrlListView_GetSelectedIndices($hWndListView2) = "" Then
_GUICtrlMenu_SetItemDisabled($Menu2, 0)
_GUICtrlMenu_SetItemDisabled($Menu2, 1)
Else
_GUICtrlMenu_SetItemEnabled($Menu2, 0)
_GUICtrlMenu_SetItemEnabled($Menu2, 1)
EndIf
$SelectedID = _GUICtrlMenu_TrackPopupMenu($Menu2, $hWndListView2, -1, -1, 1, 1, 2)
If $SelectedID = $MenuEdit Then
MsgBox(0, "", "您点击了编辑")
EndIf
If $SelectedID = $MenuDel Then
MsgBox(0, "", "您点击了删除")
EndIf
Case $NM_DBLCLK
If _GUICtrlListView_GetSelectedIndices($hWndListView2) <> "" Then MsgBox(0, "", "您点击了编辑")
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
这个用event模式转换就更好了,懒得重新写了。
总之就是不要把已经创建的gui删掉,可能会有句柄重复,所以会菜单相同。 |