找回密码
 加入
搜索
查看: 4586|回复: 10

[GUI管理] [已解决]TreeView右键菜单选中项目时显示

 火.. [复制链接]
发表于 2015-12-1 21:16:48 | 显示全部楼层 |阅读模式
本帖最后由 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[6], $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[2])

#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("鼠标:[X:%.f,Y:%.f] [左键:%u 右键:%u] 控件[%u = %u]",$a[0],$a[1],$a[2],$a[3],$a[4],$iIDFrom) )

#cs
 $array[0] = X 坐标(水平轴) 
 $array[1] = Y 坐标(垂直轴) 
 $array[2] = 鼠标左键被按下 (1 为按下, 0 为未按下) 
 $array[3] = 鼠标右键被按下 (1 未按下, 0 为未按下) 
 $array[4] = 鼠标下面的控件的控件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("项目: [Left:%d, Top:%d, Right: %d, Button:%s = %u]", 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[0] = X 坐标(水平轴) 
; $array[1] = Y 坐标(垂直轴) 
; $array[2] = 鼠标左键被按下 (1 为按下, 0 为未按下) 
; $array[3] = 鼠标右键被按下 (1 未按下, 0 为未按下) 
; $array[4] = 鼠标下面的控件的控件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("项目: [Left:%d, Top:%d, Right: %d, Button:%d]", DllStructGetData($tRect, "Left"), DllStructGetData($tRect, "Top"), DllStructGetData($tRect, "Right"), DllStructGetData($tRect, "Bottom"))
                                        
                                        ;StringFormat("鼠标:[X:%.f,Y:%.f] [左键:%u 右键:%u] 控件[%u]",$a[0],$a[1],$a[2],$a[3],$a[4])
                                        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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2015-12-1 23:44:56 | 显示全部楼层
图跟代码不一致…
114 和 140 行 下面分别插入一行
Return $GUI_RUNDEFMSG
 楼主| 发表于 2015-12-2 01:11:15 | 显示全部楼层
本帖最后由 dhlhmgc 于 2015-12-2 01:14 编辑

贴错了!,感谢提醒,已做代码修改
发表于 2015-12-2 11:28:13 | 显示全部楼层
本帖最后由 水木子 于 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[5] = ['MenuItem - 1', 'MenuItem - 2', 'MenuItem - 3', 'MenuItem - 4', 'MenuItem - 5']
$iMenu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$hMenu = GUICtrlGetHandle(-1)

$iMenuItem1 = GUICtrlCreateMenuItem($aMenuItemText[0], $iMenu)
$iMenuItem2 = GUICtrlCreateMenuItem($aMenuItemText[1], $iMenu)
$iMenuItem3 = GUICtrlCreateMenuItem($aMenuItemText[2], $iMenu)
$iMenuItem4 = GUICtrlCreateMenuItem($aMenuItemText[3], $iMenu)
$iMenuItem5 = GUICtrlCreateMenuItem($aMenuItemText[4], $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[0] = $aCursor[0] - $aPos[0]
                                        $aCursor[1] = $aCursor[1] - $aPos[1]
                                        If $aCursor[0] > $aRect[0] And $aCursor[0] < $aRect[2] And $aCursor[1] > $aRect[1] And $aCursor[1] < $aRect[3] 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
发表于 2015-12-2 14:32:50 | 显示全部楼层
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[6]
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('鼠标:[X:%.f,Y:%.f] [左键:%u 右键:%u] 控件[%u = %u]', $a[0], $a[1], $a[2], $a[3], $a[4], $iIDFrom))

                        Case $NM_DBLCLK ; 双击

                        Case $NM_RCLICK ; 右击
                                Local $aPos = MouseGetPos()
                                Local $tag = DllStructCreate('long X;long Y')
                                DllStructSetData($tag, 'X', $aPos[0])
                                DllStructSetData($tag, 'Y', $aPos[1])
                                _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
发表于 2015-12-3 09:58:34 | 显示全部楼层
学习了  还是你们厉害啊
发表于 2015-12-4 12:46:30 | 显示全部楼层
问题已经解决,代码不再发了看后面跟贴的大大们的代码,总能找到您想要的
再次感谢 水木子  A大大!


收 ...
dhlhmgc 发表于 2015-12-1 21:16


这里提问求帮助, 结果在别处发布成果..........
发表于 2015-12-4 20:52:37 | 显示全部楼层
这里提问求帮助, 结果在别处发布成果..........
131738 发表于 2015-12-4 12:46


是吗?哪里呢?我也想看看呢!
发表于 2015-12-4 22:29:21 | 显示全部楼层
本帖最后由 131738 于 2015-12-4 23:04 编辑
是吗?哪里呢?我也想看看呢!
水木子 发表于 2015-12-4 20:52


也不是无名小站.....................
发表于 2015-12-8 13:10:07 | 显示全部楼层
高手的代码看起来就是舒服,学习啦
发表于 2015-12-8 15:17:19 | 显示全部楼层
新手  学习了 autoit 还是蛮实用
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-18 15:54 , Processed in 0.084996 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表