基 础 函 数 参 考


GUICtrlCreateTreeViewItem

创建树视图项目(TreeViewItem)控件.

GUICtrlCreateTreeViewItem ( "文本", 控件 ID )

参 数

文本 控件显示的文本.
控件 ID treeview 控件标识符. 由 GUICtrlCreateTreeView 函数返回;
如创建子项, 则使用本函数返回的标识符.

返 回 值

成功: 返回控件标识符(控件ID).
失败: 返回 0.

备 注

使用 GUICtrlUpdate.... 设置其它信息

要使项目文本使用粗体字并显示为默认值, 使用 GuiCtrlSetState($treeviewItem, $GUI_DEFBUTTON),
关闭这个效果, 使用 GUICtrlSetState(), 并使用另外值替换 $GUI_DEFBUTTON, 例如 GuiCtrlSetState($treeviewItem, 0).

展开 treeview 项目, 使用 GuiCtrlSetState($treeviewItem, $GUI_EXPAND).

选择 treeview 的特定项目, 使用 GuiCtrlSetState($treeviewItem, $GUI_FOCUS).

相 关 函 数

GUICtrlCreateTreeView, GUICtrlUpdate..., GUIGetMsg, GUICtrlRead, GUICtrlGetHandle

函 数 示 例


#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <StaticConstants.au3>

Example()

Func Example()
    Local $treeview, $generalitem , $displayitem , $aboutitem , $compitem
    Local $startlabel, $aboutlabel
    Local $compinfo, $togglebutton, $infobutton, $statebutton, $cancelbutton
    Local $msg, $item, $text, $hItem

    GUICreate("我的树视图 GUI", 350, 215)

    $treeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
    $generalitem  = GUICtrlCreateTreeViewItem ("常规", $treeview)
    GUICtrlSetColor(-1, 0x0000C0)
    $displayitem  = GUICtrlCreateTreeViewItem ("显示器", $treeview)
    GUICtrlSetColor(-1, 0x0000C0)
    $aboutitem  = GUICtrlCreateTreeViewItem ("关于...", $generalitem )
    $compitem  = GUICtrlCreateTreeViewItem ("计算机", $generalitem )
    GUICtrlCreateTreeViewItem ("用户", $generalitem )
    GUICtrlCreateTreeViewItem ("分辨率", $displayitem )
    GUICtrlCreateTreeViewItem ("其它", $displayitem )

    $startlabel = GUICtrlCreateLabel("树视图演示", 190, 90, 100, 20)
    $aboutlabel = GUICtrlCreateLabel("这个小脚本演示如何使用 TreeView 控件.", 190, 70, 100, 60)
    GUICtrlSetState(-1, $GUI_HIDE) ; 初始化隐藏 "关于" 标记的文本
    $compinfo = GUICtrlCreateLabel("名称:" & @TAB & @ComputerName & @LF & "系统:" & @TAB & @OSVersion & @LF & "服务包:" & @TAB & @OSServicePack, 120, 30, 200, 80)
    GUICtrlSetState(-1, $GUI_HIDE) ; 初始化隐藏 "计算机" 信息标记的文本

    GUICtrlCreateLabel("", 0, 170, 350, 2, $SS_SUNKEN)
    $togglebutton = GUICtrlCreateButton("切换 &T", 35, 185, 70, 20)
    $infobutton = GUICtrlCreateButton("信息 &I", 105, 185, 70, 20)
    $statebutton = GUICtrlCreateButton("Col./Exp.", 175, 185, 70, 20)
    $cancelbutton = GUICtrlCreateButton("取消 &C", 245, 185, 70, 20)

    GUICtrlSetState($generalitem , BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; 展开 "常规" 项目并显示为粗体文字
    GUICtrlSetState($displayitem , BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; 展开 "显示器" 项目并显示为粗体文字

    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $msg = $togglebutton ; 切换粗体文字
                If BitAND(GUICtrlRead($generalitem ), $GUI_DEFBUTTON) Then
                    GUICtrlSetState($generalitem , 0)
                    GUICtrlSetState($displayitem , 0)
                Else
                    GUICtrlSetState($generalitem , $GUI_DEFBUTTON)
                    GUICtrlSetState($displayitem , $GUI_DEFBUTTON)
                EndIf

            Case $msg = $infobutton
                $item = GUICtrlRead($treeview) ; 获取当前选中 TreeView 项目的控件 ID
                If $item = 0 Then
                    MsgBox(64, "树视图演示", "目前没有选定任何项目")
                Else
                    $text = GUICtrlRead($item, 1) ; 获取 TreeView 的项目文字
                    If $text == "" Then
                        MsgBox(16, "错误", "检索项目相关信息时出错 ")
                    Else
                        MsgBox(64, "树视图演示", "当前选定项目: " & $text)
                    EndIf
                EndIf

            Case $msg = $statebutton
                $item = GUICtrlRead($treeview)
                If $item > 0 Then
                    $hItem  = GUICtrlGetHandle($item)
                    DllCall("user32.dll", "int", "SendMessage", "hwnd", GUICtrlGetHandle($treeview), "int", $TVM_EXPAND, "int", $TVE_TOGGLE, "hwnd", $hItem )
                EndIf

                ; 下列项目会隐藏其它标记文本(第1和第2个参数), 并显示原有标记文本(第三和第四个参数)
            Case $msg = $generalitem
                GUIChangeItemss($aboutlabel, $compinfo, $startlabel, $startlabel)

            Case $msg = $aboutitem
                GUICtrlSetState($compinfo, $GUI_HIDE)
                GUIChangeItemss($startlabel, $startlabel, $aboutlabel, $aboutlabel)

            Case $msg = $compitem
                GUIChangeItemss($startlabel, $aboutlabel, $compinfo, $compinfo)
        EndSelect
    WEnd

    GUIDelete()
EndFunc   ;==>Example

Func GUIChangeItemss($hidestart, $hideend, $showstart, $showend)
    Local $idx

    For $idx = $hidestart To $hideend
        GUICtrlSetState($idx, $GUI_HIDE)
    Next
    For $idx = $showstart To $showend
        GUICtrlSetState($idx, $GUI_SHOW)
    Next
EndFunc   ;==>GUIChangeItemss

provider with jb51.net (unicode)