[已解决]请问如何为treeview分配不同的右击menu菜单
本帖最后由 7tenlboy 于 2023-4-13 19:12 编辑#include <WinAPISys.au3>
#include <WinAPITheme.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI 部分 ### Form=
$ Form1 = GUICreate("Form1", 615, 437, 192, 124)
$TreeView1 = GUICtrlCreateTreeView(0, 0, 289, 433,BitOR($TVS_LINESATROOT, $TVS_TRACKSELECT, $TVS_SHOWSELALWAYS, $TVS_FULLROWSELECT)) $
tonedir=GUICtrlCreateTreeViewItem("一级目录",$TreeView1)
$mag1=GUICtrlCreateTreeViewItem("text1",$tonedir)
$mag2=GUICtrlCreateTreeViewItem("text2",$tonedir)
_WinAPI_SetWindowTheme(GUICtrlGetHandle($TreeView1), 'Explorer')
_WinAPI_SetWindowTheme(GUICtrlGetHandle($mag1), 'Explorer')
_WinAPI_SetWindowTheme(GUICtrlGetHandle($mag2), 'Explorer')
$menuc=GUICtrlCreateContextMenu($tonedir)
$menucitem1=GUICtrlCreateMenuItem("一级目录",$menuc)
$menucitem2=MeGUICtemr ("目录",$menuc)
$menuct1=GUICtrlCreateContextMenu($mag1)
$menucitemt1=GUICtrlCreateMenuItem("目录",$menuct1)
GUISetState(@SW_SHOW)
#EndRegion ### 结束 Koda GUI 部分 ###
While 1
$nMsg = GUIGetMsg()
开关 $nMsg
Case $GUI_EVENT_CLOSE
退出
Case $menucitem1
MsgBox(0,"一级目录",$nmsg)
MsgBox(0,GUICtrlRead($menucitem1),0)
Case $menucitemt1
MsgBox(0,"二级目录",$nmsg)
MsgBox(0,GUICtrlRead($menucitemt1),0)
EndSwitch
WEnd
这是一个现实方法。但不选文字是无法现实弹出菜单不现实就可以可以弹窗菜单
#include <WinAPISys.au3>
#include <WinAPITheme.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
$TreeView1 = GUICtrlCreateTreeView(0, 0, 289, 433,BitOR($TVS_LINESATROOT, $TVS_TRACKSELECT, $TVS_SHOWSELALWAYS, $TVS_FULLROWSELECT))
$tonedir=GUICtrlCreateTreeViewItem("一级目录",$TreeView1)
$mag1=GUICtrlCreateTreeViewItem("text1",$tonedir)
$mag2=GUICtrlCreateTreeViewItem("text2",$tonedir)
$tonedir2=GUICtrlCreateTreeViewItem("一级目录2",$TreeView1)
$mag12=GUICtrlCreateTreeViewItem("text1",$tonedir2)
$mag22=GUICtrlCreateTreeViewItem("text2",$tonedir2)
_WinAPI_SetWindowTheme(GUICtrlGetHandle($TreeView1), 'Explorer')
_WinAPI_SetWindowTheme(GUICtrlGetHandle($tonedir), 'Explorer')
_WinAPI_SetWindowTheme(GUICtrlGetHandle($mag1), 'Explorer')
_WinAPI_SetWindowTheme(GUICtrlGetHandle($mag2), 'Explorer')
$menuc=GUICtrlCreateContextMenu($TreeView1)
$menucitem1=GUICtrlCreateMenuItem("666目录",$menuc)
$menucitem1=GUICtrlCreateMenuItem("一级目录",$menuc)
$menucitem2=GUICtrlCreateMenuItem("目录",$menuc)
$menuc133=GUICtrlCreateContextMenu($TreeView1)
GUICtrlCreateMenuItem("999目录",$menuc133)
GUICtrlCreateMenuItem("一级目录",$menuc133)
GUICtrlCreateMenuItem("目录",$menuc133)
GUICtrlSetState($menuc,$GUI_HIDE)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $menucitem1
MsgBox(0,"一级目录",$nmsg)
MsgBox(0,GUICtrlRead($TreeView1),0)
_GUICtrlMenu_DestroyMenu($menuc)
Case $menucitem2
MsgBox(0,"目录",$nmsg)
MsgBox(0,GUICtrlRead($TreeView1),0)
EndSwitch
WEnd
这个也是一个方法。但是菜单都相同了。逐一分配菜单又只能选择文字才能右击。难搞。有没有大佬帮帮帮我 7tenlboy 发表于 2023-4-10 19:14
#include
#include
#include
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $hTreeView, $hContext1, $hContext2, $hItem1, $hItem2
Global Const $TVS_HASBUTTONS = 0x0001
Global Const $TVS_HASLINES = 0x0002
Global Const $TVS_LINESATROOT = 0x0004
Global Const $TVS_DISABLEDRAGDROP = 0x0010
Global Const $TVS_SHOWSELALWAYS = 0x0020
$hMainGUI = GUICreate("TreeView with Context Menus", 400, 300)
$hTreeView = GUICtrlCreateTreeView(10, 10, 380, 280, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS))
$htvRoot = GUICtrlCreateTreeViewItem("Root Node", $hTreeView)
$hItem1 = GUICtrlCreateTreeViewItem("Child Node 1", $htvRoot)
$hItem2 = GUICtrlCreateTreeViewItem("Child Node 2", $htvRoot)
; Create Context Menu 1 for Child Node 1
$hContext1 = GUICtrlCreateContextMenu($hItem1)
$hItem1Option1 = GUICtrlCreateMenuItem("Option 1 for Child Node 1", $hContext1)
$hItem1Option2 = GUICtrlCreateMenuItem("Option 2 for Child Node 1", $hContext1)
; Create Context Menu 2 for Child Node 2
$hContext2 = GUICtrlCreateContextMenu($hItem2)
$hItem2Option1 = GUICtrlCreateMenuItem("Option 1 for Child Node 2", $hContext2)
$hItem2Option2 = GUICtrlCreateMenuItem("Option 2 for Child Node 2", $hContext2)
GUISetState(@SW_SHOW)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hItem1Option1
MsgBox(0, "Child Node 1 Option 1", "Option 1 for Child Node 1")
Case $hItem1Option2
MsgBox(0, "Child Node 1 Option 2", "Option 2 for Child Node 1")
Case $hItem2Option1
MsgBox(0, "Child Node 2 Option 1", "Option 1 for Child Node 2")
Case $hItem2Option2
MsgBox(0, "Child Node 2 Option 2", "Option 2 for Child Node 2")
EndSwitch
WEnd
GUIDelete($hMainGUI)
itzyx 发表于 2023-4-12 23:56
#include <WinAPISys.au3>
#include <WinAPITheme.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
Global $hTreeView, $hContext1, $hContext2, $hItem1, $hItem2
$hMainGUI = GUICreate("TreeView with Context Menus", 400, 300)
$hTreeView = GUICtrlCreateTreeView(10, 10, 380, 280, BitOr($TVS_NOHSCROLL, $TVS_NONEVENHEIGHT, $TVS_FULLROWSELECT, $TVS_INFOTIP, $TVS_HASBUTTONS, $TVS_LINESATROOT)) ;, $TVS_TRACKSELECT))
_WinAPI_SetWindowTheme(GUICtrlGetHandle($hTreeView), 'Explorer')
$htvRoot = GUICtrlCreateTreeViewItem("Root Node", $hTreeView)
$hItem1 = GUICtrlCreateTreeViewItem("Child Node 1", $htvRoot)
$hItem2 = GUICtrlCreateTreeViewItem("Child Node 2", $htvRoot)
; Create Context Menu 1 for Child Node 1
$hContext1 = GUICtrlCreateContextMenu($hItem1)
$hItem1Option1 = GUICtrlCreateMenuItem("Option 1 for Child Node 1", $hContext1)
$hItem1Option2 = GUICtrlCreateMenuItem("Option 2 for Child Node 1", $hContext1)
; Create Context Menu 2 for Child Node 2
$hContext2 = GUICtrlCreateContextMenu($hItem2)
$hItem2Option1 = GUICtrlCreateMenuItem("Option 1 for Child Node 2", $hContext2)
$hItem2Option2 = GUICtrlCreateMenuItem("Option 2 for Child Node 2", $hContext2)
GUISetState(@SW_SHOW)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hItem1Option1
MsgBox(0, "Child Node 1 Option 1", "Option 1 for Child Node 1")
Case $hItem1Option2
MsgBox(0, "Child Node 1 Option 2", "Option 2 for Child Node 1")
Case $hItem2Option1
MsgBox(0, "Child Node 2 Option 1", "Option 1 for Child Node 2")
Case $hItem2Option2
MsgBox(0, "Child Node 2 Option 2", "Option 2 for Child Node 2")
EndSwitch
WEnd
GUIDelete($hMainGUI)
我修改了一下但是还是不是理想的那样。右击识别空白地方无反应 找了个笨方法。给单项添加字符。就能选择上了
页:
[1]