[已解决]TreeView右键菜单选中项目时显示
本帖最后由 dhlhmgc 于 2015-12-2 16:43 编辑问题已经解决,代码不再发了看后面跟贴的大大们的代码,总能找到您想要的
再次感谢 水木子A大大!
收集到一部分TreeView中显示右键菜单的代码,
自己做了一些整合,右键菜单已可以实现
但如何实现,在选择节点(项目)上单击右键才会显示右键菜单
其余区域不会显示,如下图所示,让右键菜单只在红色区域才会显示
有高人提示过,是需要结合X,Y。。。鼠标位置来判断TreeView是否显示
附上调试尚未通过的代码,求版主、高手解决!#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <GuiImageList.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
Local $hItem, $hImage, $tRect, $hTreeView
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
$Form1 = GUICreate("Form1", 640, 450)
$TreeView1 = GUICtrlCreateTreeView(16, 24, 297, 329, $iStyle, $WS_EX_CLIENTEDGE)
$Input1 = GUICtrlCreateInput("Input1", 16, 376, 585, 21)
$Label1 = GUICtrlCreateLabel("Label1", 16, 408, 588, 17)
$Button1 = GUICtrlCreateButton("Button1", 368, 24, 75, 25)
$Button2 = GUICtrlCreateButton("Button2", 368, 58, 75, 25)
$Button3 = GUICtrlCreateButton("Button3", 368, 91, 75, 25)
$Button4 = GUICtrlCreateButton("Button4", 368, 125, 75, 25)
$Button5 = GUICtrlCreateButton("Button5", 368, 158, 75, 25)
$Button6 = GUICtrlCreateButton("Button6", 368, 192, 75, 25)
Global Enum $MenuAdd = 1000, $MenuEdit, $MenuDel
$Menu1 = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($Menu1, 0, " 添加数据 ", $MenuAdd)
_GUICtrlMenu_InsertMenuItem($Menu1, 1, " 修改数据 ", $MenuEdit)
_GUICtrlMenu_InsertMenuItem($Menu1, 2, " 删除数据 ", $MenuDel)
GUIRegisterMsg($WM_NOTIFY, 'MY_WM') ;注册事件
GUISetState(@SW_SHOW)
$Debug_TV = False
; 检查传递给函数的类名, 设置为真并使用另一控件的句柄可以看出它是否有效
;//设置图标
$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 110)
_GUIImageList_AddIcon($hImage, "shell32.dll", 131)
_GUIImageList_AddIcon($hImage, "shell32.dll", 165)
_GUIImageList_AddIcon($hImage, "shell32.dll", 168)
_GUIImageList_AddIcon($hImage, "shell32.dll", 137)
_GUIImageList_AddIcon($hImage, "shell32.dll", 146)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImage)
;//增加节点
For $x = 0 To _GUIImageList_GetImageCount($hImage) - 1
$hItem[$x] = _GUICtrlTreeView_Add($TreeView1, 0, StringFormat("[%02d] New Item", $x + 1), $x, $x)
Next
;//得到项目的坐标
$tRect = _GUICtrlTreeView_DisplayRectEx($hTreeView, $hItem)
#EndRegion ### END Koda GUI section ###
Dim $text, $tRect
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
;MsgBox(4160, "信息", )
Case $Button2
Case $Button3
Case $Button4
Case $Button5
Case $Button6
EndSwitch
WEnd
#cs
OnSelchangedTREEMachineInfo(NMHDR* pNMHDR, LRESULT* pResult)
树形控件消息映射使用ON_NOTIFY宏,有两个消息响应函数,这两个消息具备完全一样的入口参数(NMHDR结构体 指针及long型指针),
但完全是两种不同的效果,所用结构体都为(NMHDR)。
用错了不会有正确的结果。
而TVN_SELCHANGED是我们所用。一般都要求选中后都改变
TVN_SELCHANGE:该消息为当前页改变(后)发送。
TVN_SELCHANGING:在当前页改变(时)发送,可以通过返回TRUE来禁止页面的改变,即不改变,我们看到的都是上一次的结果而非当前选中的结果。
#ce
Func MY_WM($hWnd, $Msg, $wParam, $lParam)
Local $tNMHdr = DllStructCreate($tagNMHDR, $lParam), $tNM_TREEVIEW
Local $hWndFrom = DllStructGetData($tNMHdr, 'hWndFrom')
Local $iIDFrom= DllStructGetData($tNMHdr, 'IDFrom')
Local $iCode = DllStructGetData($tNMHdr, 'Code')
Local $text, $oCtlID, $oCtlData
If $iIDFrom = $TreeView1 Then
Switch $iCode ;根据事件ID处理
Case $NM_CLICK ; 左击
;
$a = GUIGetCursorInfo()
GUICtrlSetData( $Input1, StringFormat("鼠标: [左键:%u 右键:%u] 控件[%u = %u]",$a,$a,$a,$a,$a,$iIDFrom) )
#cs
$array = X 坐标(水平轴)
$array = Y 坐标(垂直轴)
$array = 鼠标左键被按下 (1 为按下, 0 为未按下)
$array = 鼠标右键被按下 (1 未按下, 0 为未按下)
$array = 鼠标下面的控件的控件ID( 0 为没有或者无法获取)
#ce
Case $NM_DBLCLK ; 双击
;GUICtrlSetData($Input1,"双击")
Case $NM_RCLICK ; 右击
;GUICtrlSetData($Input1,"右键")
$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
;If Not $Index Then Return ; 这里用以判断是否选定了TreeViewItem
If Not $Index Then Return; 这里用以判断是否选定了TreeViewItem
;//在指定区域显示一个快捷菜单(弹出菜单)
;右键菜单
;$Menu1 = GUICtrlCreateContextMenu($TreeView1)
;$rclk = GUICtrlCreateMenuItem("萌萌的菜单", $Menu1)
$hhW = _GUICtrlTreeView_GetItemHandle($hWnd , $Index)
;GUICtrlSetData($Input1,"单击" & GUICtrlRead($iIDFrom, 1))
$oCtlID = GUICtrlRead($iIDFrom, 0)
$oCtlData = GUICtrlRead($iIDFrom, 1)
$tRect = _GUICtrlTreeView_DisplayRectEx($iIDFrom, $hhW)
$text = StringFormat("项目: ", DllStructGetData($tRect, "Left"), DllStructGetData($tRect, "Top"), DllStructGetData($tRect, "Right"), DllStructGetData($tRect, "Bottom"), $Index)
GUICtrlSetData($Label1,$text)
if DllStructGetData($tRect, "Bottom")>0 Then
;//在指定区域显示一个快捷菜单(弹出菜单)
$SelectedID = _GUICtrlMenu_TrackPopupMenu($Menu1, $hWndFrom, -1, -1, 1, 1, 2)
Switch $SelectedID ;根据所选菜单项执行功能
Case $MenuAdd
MsgBox(48, "", "您点击了添加", 0, $hWndFrom)
Case $MenuEdit
MsgBox(48, "", "您点击了编辑", 0, $hWndFrom)
Case $MenuDel
MsgBox(48, "", "您点击了删除", 0, $hWndFrom)
EndSwitch
Endif
Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
; $array = X 坐标(水平轴)
; $array = Y 坐标(垂直轴)
; $array = 鼠标左键被按下 (1 为按下, 0 为未按下)
; $array = 鼠标右键被按下 (1 未按下, 0 为未按下)
; $array = 鼠标下面的控件的控件ID( 0 为没有或者无法获取)
#cs
If GUICtrlRead($iIDFrom) > 0 Then
;GUICtrlRead ( 控件ID [, 高级] )
;0 = (默认) 返回控件的句柄或ID
;1 = 返回控件的扩展信息 (参考 注意 部分).
$oCtlID = GUICtrlRead($iIDFrom,0)
$oCtlData = GUICtrlRead($iIDFrom,1)
if _GUICtrlTreeView_GetChildren($iIDFrom, $oCtlID)Then
;有子项
$text = StringFormat(" 控件ID: [%s] \r\n 文本: [%s] \r\n 子项:[有子项]", GUICtrlRead($TreeView,0), GUICtrlRead($TreeView,1) )
Else
;没有子项(最底层节点)
$text = StringFormat(" 控件ID: [%s] \r\n 文本: [%s] \r\n 子项:[无]", GUICtrlRead($TreeView,0), GUICtrlRead($TreeView,1) )
;MsgBox(48,"单击提示","选择了" & GUICtrlRead($treeview, 1), 0, $Manage)
Endif
$tRect = _GUICtrlTreeView_DisplayRectEx($iIDFrom, $oCtlID)
$text = StringFormat("项目: ", DllStructGetData($tRect, "Left"), DllStructGetData($tRect, "Top"), DllStructGetData($tRect, "Right"), DllStructGetData($tRect, "Bottom"))
;StringFormat("鼠标: [左键:%u 右键:%u] 控件[%u]",$a,$a,$a,$a,$a)
GUICtrlSetData($Label1,$text)
;GUICtrlSetData ($Edit1, $text )
;MsgBox(0, 0, 'ID: ' & GUICtrlRead($TreeView,0) & @CRLF & 'Text: ' & GUICtrlRead($TreeView, 1))
Endif
#ce
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_OnNotify
图跟代码不一致…
114 和 140 行 下面分别插入一行
Return $GUI_RUNDEFMSG 本帖最后由 dhlhmgc 于 2015-12-2 01:14 编辑
贴错了!,感谢提醒,已做代码修改 本帖最后由 水木子 于 2015-12-2 11:30 编辑
还有点小问题,现在有点忙,抽空再改。
#include <GUIMenu.au3>
#include <GuiTreeView.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
$MyGui = GUICreate('', 400, 300)
$iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
$TreeView1 = GUICtrlCreateTreeView(5, 5, 390, 250, $iStyle, $WS_EX_CLIENTEDGE)
$TreeViewItem1 = GUICtrlCreateTreeViewItem('TreeViewItem1', $TreeView1)
GUICtrlCreateTreeViewItem('TreeViewItem1-1', $TreeViewItem1)
GUICtrlCreateTreeViewItem('TreeViewItem1-2', $TreeViewItem1)
GUICtrlCreateTreeViewItem('TreeViewItem1-3', $TreeViewItem1)
$TreeViewItem2 = GUICtrlCreateTreeViewItem('TreeViewItem2', $TreeView1)
GUICtrlCreateTreeViewItem('TreeViewItem2-1', $TreeViewItem2)
GUICtrlCreateTreeViewItem('TreeViewItem2-2', $TreeViewItem2)
GUICtrlCreateTreeViewItem('TreeViewItem2-3', $TreeViewItem2)
$TreeViewItem3 = GUICtrlCreateTreeViewItem('TreeViewItem3', $TreeView1)
GUICtrlCreateTreeViewItem('TreeViewItem3-1', $TreeViewItem3)
GUICtrlCreateTreeViewItem('TreeViewItem3-2', $TreeViewItem3)
GUICtrlCreateTreeViewItem('TreeViewItem3-3', $TreeViewItem3)
GUICtrlSetState($TreeViewItem1, $GUI_EXPAND)
GUICtrlSetState($TreeViewItem2, $GUI_EXPAND)
;~ GUICtrlSetState($TreeViewItem3, $GUI_EXPAND)
Global $aMenuItemText = ['MenuItem - 1', 'MenuItem - 2', 'MenuItem - 3', 'MenuItem - 4', 'MenuItem - 5']
$iMenu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$hMenu = GUICtrlGetHandle(-1)
$iMenuItem1 = GUICtrlCreateMenuItem($aMenuItemText, $iMenu)
$iMenuItem2 = GUICtrlCreateMenuItem($aMenuItemText, $iMenu)
$iMenuItem3 = GUICtrlCreateMenuItem($aMenuItemText, $iMenu)
$iMenuItem4 = GUICtrlCreateMenuItem($aMenuItemText, $iMenu)
$iMenuItem5 = GUICtrlCreateMenuItem($aMenuItemText, $iMenu)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Do
Until GUIGetMsg() = -3
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $TreeView1
If Not IsHWnd($TreeView1) Then $hWndTreeview = GUICtrlGetHandle($TreeView1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_RCLICK
MouseDown('left')
$iItemID = GUICtrlRead($TreeView1)
$hItemID = GUICtrlGetHandle($iItemID)
$aCursor = GUIGetCursorInfo()
$aRect = _GUICtrlTreeView_DisplayRect($TreeView1, $hItemID, True)
$aPos = ControlGetPos($MyGui, '', $TreeView1)
$aCursor = $aCursor - $aPos
$aCursor = $aCursor - $aPos
If $aCursor > $aRect And $aCursor < $aRect And $aCursor > $aRect And $aCursor < $aRect Then
$hMenu = GUICtrlGetHandle($iMenu)
Local $iMenuId = _GUICtrlMenu_TrackPopupMenu($hMenu, $MyGui, -1, -1, 1, 1, 2)
Switch $iMenuId
Case $iMenuItem1
MsgBox(0, 0, 'MenuItem - 1')
Case $iMenuItem2
MsgBox(0, 0, 'MenuItem - 2')
Case $iMenuItem3
MsgBox(0, 0, 'MenuItem - 3')
Case $iMenuItem4
MsgBox(0, 0, 'MenuItem - 4')
Case $iMenuItem5
MsgBox(0, 0, 'MenuItem - 5')
EndSwitch
EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
LZ先整理一下代码再提问那么难吗?#include <GUIConstants.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
Local $Form1 = GUICreate('Form1', 640, 450)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
Local $TreeView1 = GUICtrlCreateTreeView(16, 24, 297, 329, $iStyle, $WS_EX_CLIENTEDGE)
Local $hTreeView = GUICtrlGetHandle(-1)
$Input1 = GUICtrlCreateInput('Input1', 16, 376, 585, 21)
$Label1 = GUICtrlCreateLabel('Label1', 16, 408, 588, 17)
$Button1 = GUICtrlCreateButton('Button1', 368, 24, 75, 25)
$Button2 = GUICtrlCreateButton('Button2', 368, 58, 75, 25)
$Button3 = GUICtrlCreateButton('Button3', 368, 91, 75, 25)
$Button4 = GUICtrlCreateButton('Button4', 368, 125, 75, 25)
$Button5 = GUICtrlCreateButton('Button5', 368, 158, 75, 25)
$Button6 = GUICtrlCreateButton('Button6', 368, 192, 75, 25)
Local Enum $MenuAdd = 1000, $MenuEdit, $MenuDel
Local $Menu1 = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($Menu1, 0, ' 添加数据 ', $MenuAdd)
_GUICtrlMenu_InsertMenuItem($Menu1, 1, ' 修改数据 ', $MenuEdit)
_GUICtrlMenu_InsertMenuItem($Menu1, 2, ' 删除数据 ', $MenuDel)
GUIRegisterMsg($WM_NOTIFY, '_MY_WM') ;注册事件
GUISetState()
$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 110)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 131)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 165)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 168)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 137)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 146)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImage)
Local $hItem
For $x = 0 To _GUIImageList_GetImageCount($hImage) - 1
$hItem[$x] = _GUICtrlTreeView_Add($TreeView1, 0, StringFormat('[%02d] New Item', $x + 1), $x, $x)
Next
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
EndSwitch
WEnd
Func _MY_WM($hWnd, $Msg, $wParam, $lParam)
Local $tNMHdr = DllStructCreate($tagNMHDR, $lParam), $tNM_TREEVIEW
Local $hWndFrom = DllStructGetData($tNMHdr, 'hWndFrom')
Local $iIDFrom = DllStructGetData($tNMHdr, 'IDFrom')
Local $iCode = DllStructGetData($tNMHdr, 'Code')
If $iIDFrom = $TreeView1 Then
Switch $iCode ;根据事件ID处理
Case $NM_CLICK ; 左击
$a = GUIGetCursorInfo()
GUICtrlSetData($Input1, StringFormat('鼠标: [左键:%u 右键:%u] 控件[%u = %u]', $a, $a, $a, $a, $a, $iIDFrom))
Case $NM_DBLCLK ; 双击
Case $NM_RCLICK ; 右击
Local $aPos = MouseGetPos()
Local $tag = DllStructCreate('long X;long Y')
DllStructSetData($tag, 'X', $aPos)
DllStructSetData($tag, 'Y', $aPos)
_WinAPI_ScreenToClient($hTreeView, $tag)
Local $iX = DllStructGetData($tag, 'X')
Local $iY = DllStructGetData($tag, 'Y')
$tag = 0
Local $iPos = _GUICtrlTreeView_HitTest($hTreeView, $iX, $iY)
If ($iPos <> 2) And ($iPos <> 4) And ($iPos <> 64) Then Return $GUI_RUNDEFMSG
Local $hItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iX, $iY)
_GUICtrlTreeView_SelectItem($hTreeView, $hItem)
Switch _GUICtrlMenu_TrackPopupMenu($Menu1, $hWndFrom, -1, -1, 1, 1, 2)
Case $MenuAdd
MsgBox(48, '', '您点击了添加', 0, $hWndFrom)
Case $MenuEdit
MsgBox(48, '', '您点击了编辑', 0, $hWndFrom)
Case $MenuDel
MsgBox(48, '', '您点击了删除', 0, $hWndFrom)
EndSwitch
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_MY_WM
学习了还是你们厉害啊 问题已经解决,代码不再发了看后面跟贴的大大们的代码,总能找到您想要的
再次感谢 水木子A大大!
收 ...
dhlhmgc 发表于 2015-12-1 21:16 http://www.autoitx.com/images/common/back.gif
这里提问求帮助, 结果在别处发布成果.......... 这里提问求帮助, 结果在别处发布成果..........
131738 发表于 2015-12-4 12:46 http://www.autoitx.com/images/common/back.gif
是吗?哪里呢?我也想看看呢! 本帖最后由 131738 于 2015-12-4 23:04 编辑
是吗?哪里呢?我也想看看呢!
水木子 发表于 2015-12-4 20:52 http://www.autoitx.com/images/common/back.gif
也不是无名小站..................... 高手的代码看起来就是舒服,学习啦 新手学习了 autoit 还是蛮实用
页:
[1]