找回密码
 加入
搜索
查看: 1654|回复: 7

[AU3基础] 想右键删除TreeView里面的某一个子项(已解决)

[复制链接]
发表于 2020-5-11 10:25:17 | 显示全部楼层 |阅读模式
本帖最后由 qsy666888 于 2020-5-11 16:40 编辑

达到目的:在不用左键的情况下,鼠标指针移动到想要删除TreeView里面的某一个子项时,直接右键就可以删除想要删除的那个子项,大侠们 ! 兄弟伙们!该如何操作呢?求指教。
现在我这里只能先左键点击子项 ,然后再右键删除,但是不能删除自己指定的子项
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

$Debug_TV = False ; 检查传递给函数的类名, 设置为True并输出到一个控件的句柄,用于检查它是否工作

Global $hTreeView

_Main()

Func _Main()

    Local $hItem, $hImage, $iImage
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)


    GUICreate("TreeView Click Item", 400, 300)

    $hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $WS_BORDER+$iStyle)
    GUISetState()

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

$a = StringSplit('a|b|c|d|e|f|g','|',1)
    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To 10
        $hItem = GUICtrlCreateTreeViewItem(StringFormat("[%02d] text", $x), $hTreeView)
        For $y = 1 To 7
            GUICtrlCreateTreeViewItem(StringFormat($a[$y]&'_text', $y), $hItem)
        Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    ; 循环直到用户退出
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Main

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTreeView
    If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)

    $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 ; The user has clicked the right mouse button within the control

                                $iPos_X = _WinAPI_GetMousePosX(True, $hTreeView)
                                $iPos_Y = _WinAPI_GetMousePosY(True, $hTreeView)
                                $uFlags = _GUICtrlTreeView_HitTest($hTreeView, $iPos_X, $iPos_Y)
                                        Local $hItem, $hParent, $sText_I
                                        $hItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iPos_X, $iPos_Y)
                                                                                $id_hItem = BinaryLen($hItem)
                                                                                Local $Id_P = _GUICtrlTreeView_GetParentParam($hTreeView, $hItem)
                                      _GUICtrlTreeView_Delete($hTreeView, $Id_P)
                                                                   

            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY


发表于 2020-5-11 11:15:55 | 显示全部楼层
句柄用错了,那是ID
                                Case $NM_RCLICK ; The user has clicked the right mouse button within the control
                                        Local $iPos_X = _WinAPI_GetMousePosX(True, $hWndTreeview)
                                        Local $iPos_Y = _WinAPI_GetMousePosY(True, $hWndTreeview)
                                        Local $hItem = _GUICtrlTreeView_HitTestItem($hWndTreeview, $iPos_X, $iPos_Y)
                                        If $hItem Then _GUICtrlTreeView_Delete($hWndTreeview, $hItem)
 楼主| 发表于 2020-5-11 11:25:23 | 显示全部楼层
afan 发表于 2020-5-11 11:15
句柄用错了,那是ID

感谢感谢,非常感谢A大
 楼主| 发表于 2020-5-11 15:37:32 | 显示全部楼层
本帖最后由 qsy666888 于 2020-5-11 15:56 编辑
afan 发表于 2020-5-11 11:15
句柄用错了,那是ID

a大 你好!我想再问一下,  如果不用 id 的话 (id好像需要左键点击后才能获取id地址),那么我如果移动到某一个父项上,按右键(不使用左键),就能添加一项子项在那父项里面去。
(如果使用左键的话,就能实现,但如果使用右键的话,就获取不了id),  感谢a大!
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

$Debug_TV = False ; 检查传递给函数的类名, 设置为True并输出到一个控件的句柄,用于检查它是否工作

Global $hTreeView,$hWndTreeview,$hItem

_Main()

Func _Main()

    Local $hItem, $hImage, $iImage
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)


    GUICreate("TreeView Click Item", 400, 300)

    $hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268)
    GUISetState()

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

$a = StringSplit('a|b|c|d|e|f|g','|',1)
    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To 5
        $hItem = GUICtrlCreateTreeViewItem(StringFormat("[%02d] text111", $x), $hTreeView)
        For $y = 1 To 2
           GUICtrlCreateTreeViewItem(StringFormat($a[$y]&'_text', $y), $hItem)
                        
        Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    ; 循环直到用户退出
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Main

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTreeView
    If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)

    $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 ; The user has clicked the right mouse button within the control
;Local $iPos_X = _WinAPI_GetMousePosX(True, $hWndTreeview)
;Local $iPos_Y = _WinAPI_GetMousePosY(True, $hWndTreeview)
                                                                                $Id_P = _GUICtrlTreeView_HitTestItem($hTreeView)
                                                                        If $hItem Then GUICtrlCreateTreeViewItem('tj_text', $Id_P)
                                                                        _GUICtrlTreeView_Expand($hTreeView, $Id_P)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY


发表于 2020-5-11 16:33:13 | 显示全部楼层
                                Case $NM_RCLICK ; The user has clicked the right mouse button within the control
                                        Local $iPos_X = _WinAPI_GetMousePosX(True, $hWndTreeview)
                                        Local $iPos_Y = _WinAPI_GetMousePosY(True, $hWndTreeview)
                                        Local $hItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iPos_X, $iPos_Y)
                                        _GUICtrlTreeView_SelectItem($hWndTreeview, $hItem)
                                        Local $iID = GUICtrlRead($hTreeView)
                                        GUICtrlCreateTreeViewItem('tj_text', $iID)
                                        _GUICtrlTreeView_Expand($hTreeView, $iID)
 楼主| 发表于 2020-5-11 16:39:48 | 显示全部楼层

感谢感谢    a大好人哈   好人一生平安
发表于 2020-5-11 21:02:12 | 显示全部楼层
qsy666888 发表于 2020-5-11 16:39
感谢感谢    a大好人哈   好人一生平安

下面能添加也能删除:

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg, $iwParam
        $a = StringSplit('a|b|c|d|e|f|g|tj', '|', 1)
        Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
        $hWndTreeview = $hTreeView
        If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)
        $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 ; The user has clicked the right mouse button within the control
                                        Local $iPos_X = _WinAPI_GetMousePosX(True, $hWndTreeview)
                                        Local $iPos_Y = _WinAPI_GetMousePosY(True, $hWndTreeview)
                                        Local $hItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iPos_X, $iPos_Y)
                                        _GUICtrlTreeView_SelectItem($hWndTreeview, $hItem)
                                        $Index = GUICtrlRead($hTreeView, 1)
                                        For $x = 1 To 10
                                                $Indexa = StringFormat("[%02d] text", $x)
                                                If $Index = $Indexa Then
                                                        Local $iID = GUICtrlRead($hTreeView)
                                                        GUICtrlCreateTreeViewItem('tj_text', $iID)
                                                        _GUICtrlTreeView_Expand($hTreeView, $iID)
                                                EndIf
                                        Next
                                        For $y = 1 To 8
                                                $Indexb = StringFormat($a[$y] & '_text', $y)
                                                If $Index = $Indexb Then
                                                        If $hItem Then _GUICtrlTreeView_Delete($hWndTreeview, $hItem)
                                                EndIf
                                        Next
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
 楼主| 发表于 2020-5-12 18:27:29 | 显示全部楼层
chzj589 发表于 2020-5-11 21:02
下面能添加也能删除:

你这代码也不错,非常棒
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-17 20:57 , Processed in 0.077596 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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