找回密码
 加入
搜索
查看: 3150|回复: 5

[GUI管理] 有没有办法不通过树节点句柄,而只通过树节点文本来对节点进行删除

[复制链接]
发表于 2012-8-9 13:48:44 | 显示全部楼层 |阅读模式
知道一颗树,需要提供右键菜单对其中一个节点进行删除,请问有什么方法?(无法获得节点的句柄)
 楼主| 发表于 2012-8-10 08:20:20 | 显示全部楼层
顶起来,大家帮帮忙...
发表于 2012-8-10 20:53:17 | 显示全部楼层
有例子吗?无法获得节点的句柄?TREE不是自己画的吗,第三方窗口的?
 楼主| 发表于 2012-8-13 15:19:11 | 显示全部楼层
回复 3# yhxhappy


就是自己创建了一颗树,这颗树是通过数据库查询数据动态产生的,想对树节点进行删除或是增加节点来对数据库表进行增加或是删除操作。
发表于 2012-8-13 19:35:15 | 显示全部楼层
没时间仔细验证,先抛砖引玉
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>


$GUI = GUICreate("树视图(TreeView) UDF 函数演示", 400, 300)
$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT))

For $x = 1 To 3
        $hItem = GUICtrlCreateTreeViewItem(StringFormat("[%02d] 主项", $x), $hTreeView)
        For $y = 1 To Random(2, 10, 1)
                GUICtrlCreateTreeViewItem(StringFormat("[%02d] 子项", $y), $hItem)
        Next
Next

$RightMenu = GUICtrlCreateContextMenu($hTreeView)
$AddBtnP = GUICtrlCreateMenuItem("增加根项目", $RightMenu)
GUICtrlCreateMenuItem("", $RightMenu)
$AddBtnC = GUICtrlCreateMenuItem("增加子项目", $RightMenu)
GUICtrlCreateMenuItem("", $RightMenu)
$DelBtn = GUICtrlCreateMenuItem("删除项目", $RightMenu)
GUICtrlCreateMenuItem("", $RightMenu)
$RenameBtn = GUICtrlCreateMenuItem("重命名", $RightMenu)

GUISetState()

; 循环到用户退出
While 1
        $Msg = GUIGetMsg()
        Switch $Msg
        Case $GUI_EVENT_CLOSE
                Exit
        Case $AddBtnP, $AddBtnC
                $SelectItem = _GUICtrlTreeView_GetSelection($hTreeView)
                If $SelectItem = 0 Or $Msg = $AddBtnP Then
                        $NewItem = _GUICtrlTreeView_Add($hTreeView, 0, "新项目")
                Else
                        _GUICtrlTreeView_Expand($hTreeView, $SelectItem)
                        $NewItem = _GUICtrlTreeView_AddChild($hTreeView, $SelectItem, "新项目")                
                EndIf
                $EditHWND = _GUICtrlTreeView_EditText($hTreeView, $NewItem)
                _UpItemText($EditHWND, $NewItem)
        Case $DelBtn
                $SelectItem = _GUICtrlTreeView_GetSelection($hTreeView)
                $ItemText = _GUICtrlTreeView_GetText($hTreeView, $SelectItem)
                
                If _GUICtrlTreeView_GetChildren($hTreeView, $SelectItem) Then
                        $Text = '确定要删除 "' & $ItemText & '" 项目及其全部子项吗?'
                Else
                        $Text = '确定要删除 "' & $ItemText & '" 项目吗?'
                EndIf
                If MsgBox(32+1, "请确认", $Text) = 1 Then
                        _GUICtrlTreeView_Delete($hTreeView, $SelectItem)
                EndIf
        Case $RenameBtn
                $SelectItem = _GUICtrlTreeView_GetSelection($hTreeView)
                $EditHWND = _GUICtrlTreeView_EditText($hTreeView, $SelectItem)
                
                _UpItemText($EditHWND, $SelectItem)
        EndSwitch
WEnd

Func _UpItemText($EditHWND, $SelectItem)
        $sText = ""                
        While WinExists($EditHWND)
                $sText = WinGetTitle($EditHWND)
                Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        Exit
                Case -8                                                ;如果鼠标释放且鼠标光标位置没有控件,说明点击在窗口空白区域上
                        $Pos = GUIGetCursorInfo($GUI)
                        If $Pos[4] = 0 Then _GUICtrlTreeView_EndEdit($hTreeView)
                EndSwitch
        WEnd
        _GUICtrlTreeView_SetText($hTreeView, $SelectItem, $sText)
        _GUICtrlTreeView_SetFocused($hTreeView, $SelectItem)                
EndFunc
 楼主| 发表于 2012-8-13 22:08:18 | 显示全部楼层
回复 5# yhxhappy


    很有用,非常感谢!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-18 08:06 , Processed in 0.082936 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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