[已解决]两个子窗口ListView右键菜单的问题
本帖最后由 ccy666 于 2012-2-24 11:30 编辑#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
$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)
$MainGUI=GUICreate("主窗口",800,600)
$Button1=GUICtrlCreateButton("按钮一",60,200)
$Button2=GUICtrlCreateButton("按钮二",120,200)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
Sleep(1)
$Msg=GUIGetMsg()
If $Msg=$GUI_EVENT_CLOSE Then ExitLoop
If $Msg=$Button1 Then CreateGui1()
If $Msg=$Button2 Then CreateGui2()
WEnd
Func CreateGui1()
$gui1=GUICreate("窗口一",400,300,-1,-1,-1,-1,$MainGUI)
$ListView1=GUICtrlCreateListView("ABCD|EFGH",50,50)
GUICtrlCreateListViewItem("1234|5678",-1)
GUISetState(@SW_SHOW)
While 1
$Msg_child=GUIGetMsg()
If $Msg_child=$GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete($gui1)
EndFunc
Func CreateGui2()
$gui2=GUICreate("窗口二",400,300,-1,-1,-1,-1,$MainGUI)
$ListView2=GUICtrlCreateListView("1234|5678",50,50)
GUICtrlCreateListViewItem("ABCD|EFGH",-1)
GUISetState(@SW_SHOW)
While 1
$Msg_child=GUIGetMsg()
If $Msg_child=$GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete($gui2)
EndFunc
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iCode, $tNMHDR, $hWndListView1,$hWndListView2, $SelectedID, $hWndTab
$hWndListView1 = $ListView1
$hWndListView2 = $ListView2
If Not IsHWnd($ListView1) Then $hWndListView1 = GUICtrlGetHandle($ListView1)
If Not IsHWnd($ListView2) Then $hWndListView2 = GUICtrlGetHandle($ListView2)
$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
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不好意思,这是以前的一个问题:为什么ListView1和ListView2的右键菜单会变成一样?请哪位大哥帮忙看看?谢谢 本帖最后由 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删掉,可能会有句柄重复,所以会菜单相同。 回复 2# haijie1223
老大,我找到别人的这个错误代码前就是搜索EVENT模式的例子,但没搜索到。能否辛苦一下,简单写个单窗口treelistview右键菜单删除和添加选择例子参考一下?拜托了 回复 3# ccy666
有时间的吧,白天只是偶尔上上。 回复 4# haijie1223
好的,先谢过了 本帖最后由 benkel 于 2012-2-16 12:52 编辑
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>
Global $GUI1,$GUI2,$MainGUI,$ListView1,$ListView2,$hWndListView1,$hWndListView2
Global $Menu1, $menu2
Global Enum $MenuAdd = 1000, $MenuEdit, $MenuDel
$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)
$MainGUI=GUICreate("主窗口",800,600)
$Button1=GUICtrlCreateButton("按钮一",60,200)
$Button2=GUICtrlCreateButton("按钮二",120,200)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$gui1=GUICreate("窗口一",400,300,-1,-1,-1,-1,$MainGUI)
$ListView1=GUICtrlCreateListView("ABCD|EFGH",50,50)
GUICtrlCreateListViewItem("1234|5678",-1)
$gui2=GUICreate("窗口二",400,300,-1,-1,-1,-1,$MainGUI)
$ListView2=GUICtrlCreateListView("1234|5678",50,50)
GUICtrlCreateListViewItem("ABCD|EFGH",-1)
$hWndListView1 = GUICtrlGetHandle($ListView1)
$hWndListView2 = GUICtrlGetHandle($ListView2)
While 1
$Msg=GUIGetMsg(1)
Switch $Msg
Case $GUI_EVENT_CLOSE
If $Msg = $MainGUI Then
Exit
Else
GUISetState(@SW_ENABLE,$MainGUI)
GUISetState(@SW_HIDE,$Msg)
EndIf
Case $Button1
GUISetState(@SW_DISABLE,$MainGUI)
GUISetState(@SW_SHOW,$gui1)
Case $Button2
GUISetState(@SW_DISABLE,$MainGUI)
GUISetState(@SW_SHOW,$gui2)
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iCode, $tNMHDR,$SelectedID, $hWndTab
$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
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没注意看,原来2楼的大大已经给出代码了,我这个多余的了{:face (394):} 回复 6# benkel
谢谢您的热心帮助,论坛有您而精彩,谢谢! 雷锋精神传天下!谢谢分享! 感谢各位大大,小弟学习了 标记下,有时间再来学习
页:
[1]