Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   AutoIt (http://forum.oszone.net/forumdisplay.php?f=103)
-   -   [решено] Создание кнопок в GUICtrlCreateTreeViewitem (http://forum.oszone.net/showthread.php?t=143975)

ZeroCrash 02-07-2009 01:16 1156875

Создание кнопок в GUICtrlCreateTreeViewitem
 
Вот код из примера справки:

PHP код:

#include <GUIConstants.au3>

Global Const $TV_FIRST      0x1100 
Global Const $TVM_EXPAND    $TV_FIRST 2
Global Const $TVE_TOGGLE    0x0003


GUICreate
("My GUI with treeview"350215)

$treeview       GUICtrlCreateTreeView(66100150)
$generalitem    GUICtrlCreateTreeViewitem("General"$treeview)
GUICtrlSetColor(-10x0000C0)
$displayitem    GUICtrlCreateTreeViewitem("Display"$treeview)
GUICtrlSetColor(-10x0000C0)
$aboutitem      GUICtrlCreateTreeViewitem("About"$generalitem)
$compitem       GUICtrlCreateTreeViewitem("Computer"$generalitem)
$useritem       GUICtrlCreateTreeViewitem("User"$generalitem)
$resitem        GUICtrlCreateTreeViewitem("Resolution"$displayitem)
$otheritem      GUICtrlCreateTreeViewitem("Other"$displayitem)

$startlabel     GUICtrlCreateLabel("TreeView Demo",190,90,100,20)
$aboutlabel     GUICtrlCreateLabel("This little scripts demonstates the using of a treeview-control."1907010060)
GUICtrlSetState(-1$GUI_HIDE)  ; Hides the "aboutlabel"-text during initialization
$compinfo       
GUICtrlCreateLabel("Name:" & @TAB & @ComputerName & @LF "OS:" & @TAB & @OSVersion & @LF "SP:" & @TAB & @OSServicePack1203020080)
GUICtrlSetState(-1$GUI_HIDE)  ; Hides the "compinfo"-text during initialization

GUICtrlCreateLabel
(""01703502)
$togglebutton   GUICtrlCreateButton("&Toggle"351857020)
$infobutton     GUICtrlCreateButton("&Info"1051857020)
$statebutton    GUICtrlCreateButton("Col./Exp."1751857020)
$cancelbutton   GUICtrlCreateButton("&Cancel"2451857020)

GUICtrlSetState($generalitemBitOr($GUI_EXPAND,$GUI_DEFBUTTON))    ; Expand the "General"-item and paint in bold
GUICtrlSetState
($displayitemBitOr($GUI_EXPAND,$GUI_DEFBUTTON))    ; Expand the "Display"-item and paint in bold

GUISetState 
()
While 
1
    $msg 
GUIGetMsg()
    
Select
        
Case $msg $cancelbutton Or $msg $GUI_EVENT_CLOSE
            ExitLoop
    
        
Case $msg $togglebutton   Toggle the bold painting
            
If BitAnd(GUICtrlRead($generalitem), $GUI_DEFBUTTONThen
                GUICtrlSetState
($generalitem0)
                
GUICtrlSetState($displayitem0)
            Else
                
GUICtrlSetState($generalitem$GUI_DEFBUTTON)
                
GUICtrlSetState($displayitem$GUI_DEFBUTTON)
            EndIf
        
        Case 
$msg $infobutton
            $item 
GUICtrlRead($treeview)      ; Get the controlID of the current selected treeview item
            
If $item 0 Then
                MsgBox
(64"TreeView Demo""No item currently selected")
            Else
                
$advmsg GUICtrlRead($item1) ; Get advanced infos about the treeview item
                
If ($advmsg[0] == 0Then
                    MsgBox
(16"Error""Error while retrieving infos about item")
                Else
                    
MsgBox(64"TreeView Demo""Current item selected is: " $advmsg[0])  ; $advmsg[0contains the text and $advmsg[1the state value of the treeview item
                
EndIf
            EndIf
            
        Case 
$msg $statebutton
            $item 
GUICtrlRead($treeview)
            If 
$item 0 Then
                $hItem 
GUICtrlGetHandle($item)
                
GuiCtrlSendMsg($treeview$TVM_EXPAND$TVE_TOGGLE$hItem)
            EndIf
            
        ; 
The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
        Case 
$msg $generalitem
            GUIChangeItems
($aboutlabel$compinfo$startlabel$startlabel)
        
        Case 
$msg $aboutitem
            GUICtrlSetState 
($compinfo$GUI_HIDE)
            
GUIChangeItems($startlabel$startlabel$aboutlabel$aboutlabel)
            
        Case 
$msg $compitem
            GUIChangeItems
($startlabel$aboutlabel$compinfo$compinfo)
    
EndSelect
WEnd

GUIDelete
()
Exit

Func GUIChangeItems($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 

Как сделать чтобы при нажатии на подпункт "About" отображались мои кнопки? В общем как вставить туда кнопки?

Creat0R 02-07-2009 01:52 1156887

Код:

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

$hGUI = GUICreate("My GUI with TreeView", 350, 215)

$TreeView = GUICtrlCreateTreeView(6, 6, 110, 150, -1, $WS_EX_STATICEDGE+$WS_EX_CLIENTEDGE)

#Region General TVItem
$General_TVItem = GUICtrlCreateTreeViewItem("General", $TreeView)
GUICtrlSetColor(-1, 0x0000C0)

$TVDemo_Label = GUICtrlCreateLabel("TreeView Demo", 190, 90, 100, 20)

$iGeneral_Last_Item = GUICtrlCreateDummy()
#EndRegion General TVItem
;

#Region About TVItem
$About_TVItem = GUICtrlCreateTreeViewItem("About", $General_TVItem)

$About_Label = GUICtrlCreateLabel("This little scripts demonstates the using of a treeview-control.", 190, 70, 100, 60)
GUICtrlSetState(-1, $GUI_HIDE) ; Hides the "aboutlabel"-text during initialization

$Some_Button = GUICtrlCreateButton("My Button", 190, 135, 70, 20)
GUICtrlSetState(-1, $GUI_HIDE) ; Hides the "My Button"-button during initialization

$iAbout_Last_Item = GUICtrlCreateDummy()
#EndRegion About TVItem
;

#Region Computer TVItem
$Computer_TVItem = GUICtrlCreateTreeViewItem("Computer", $General_TVItem)

$ComputerInfo_Label = GUICtrlCreateLabel( _
    StringFormat("Name:\t%sOS:\n\t%s\nSP:\t%s", @ComputerName, @OSVersion, @OSServicePack), 120, 30, 200, 80)
GUICtrlSetState(-1, $GUI_HIDE) ; Hides the "compinfo"-text during initialization

$iComputer_Last_Item = GUICtrlCreateDummy()
#EndRegion Computer TVItem
;

#Region User TVItem
$User_TVItem = GUICtrlCreateTreeViewItem("User", $General_TVItem)

$UserInfo_Label = GUICtrlCreateLabel("User Name: " & @CRLF & @TAB & @TAB & @UserName, 120, 30, 200, 80)
GUICtrlSetState(-1, $GUI_HIDE) ; Hides the "UserInfo"-text during initialization

$iUser_Last_Item = GUICtrlCreateDummy()
#EndRegion User TVItem
;

#Region Display TVItem
$Display_TVItem = GUICtrlCreateTreeViewItem("Display", $TreeView)
GUICtrlSetColor(-1, 0x0000C0)

$iDisplay_Last_Item = GUICtrlCreateDummy()
#EndRegion Display TVItem
;

#Region Resolution TVItem
$Resolution_TVItem = GUICtrlCreateTreeViewItem("Resolution", $Display_TVItem)

$iResolution_Last_Item = GUICtrlCreateDummy()
#EndRegion Resolution TVItem
;

#Region Other TVItem
$Other_TVItem = GUICtrlCreateTreeViewItem("Other", $Display_TVItem)

$iOther_Last_Item = GUICtrlCreateDummy()
#EndRegion Other TVItem
;

;Last ItemID - to hide all controls when showing other (current)

$iLast_TVItemsID = GUICtrlCreateDummy()

GUICtrlCreateLabel("", 0, 180, 350, 2, $SS_SUNKEN)

$Toggle_Button = GUICtrlCreateButton("&Toggle", 35, 185, 70, 20)
$Info_Button = GUICtrlCreateButton("&Info", 105, 185, 70, 20)
$ColExp_Button = GUICtrlCreateButton("Col./Exp.", 175, 185, 70, 20)
$Cancel_Button = GUICtrlCreateButton("&Cancel", 245, 185, 70, 20)

GUICtrlSetState($General_TVItem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "General"-item and paint in bold
GUICtrlSetState($Display_TVItem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "Display"-item and paint in bold

GUISetState()

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $Cancel_Button, $GUI_EVENT_CLOSE
            Exit
        Case
$Toggle_Button ; Toggle the bold painting
            If BitAND(GUICtrlRead($General_TVItem), $GUI_DEFBUTTON) Then
                GUICtrlSetState($General_TVItem, 0)
                GUICtrlSetState($Display_TVItem, 0)
            Else
                GUICtrlSetState($General_TVItem, $GUI_DEFBUTTON)
                GUICtrlSetState($Display_TVItem, $GUI_DEFBUTTON)
            EndIf
        Case
$Info_Button
            $iItem_CtrlID
= GUICtrlRead($TreeView) ; Get the controlID of the current selected treeview item

            If $iItem_CtrlID = 0 Then
                MsgBox(64, "TreeView Demo", "No item currently selected.", 0, $hGUI)
            Else
                $sItem_Msg = GUICtrlRead($iItem_CtrlID, 1) ; Get advanced infos about the treeview item

                If $sItem_Msg == "" Then
                    MsgBox(16, "Error", "Error while retrieving infos about item.", 0, $hGUI)
                Else
                    MsgBox(64, "TreeView Demo", "Current item selected is: " & $sItem_Msg, 0, $hGUI)
                EndIf
            EndIf
        Case $ColExp_Button
            $iItem_CtrlID = GUICtrlRead($TreeView)

            If $iItem_CtrlID > 0 Then
                $hItem = GUICtrlGetHandle($iItem_CtrlID)
                GUICtrlSendMsg($TreeView, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
            EndIf

            ;The following items will hide the other labels (1st and 2nd parameter),
            ;and then show the 'own' labels (3rd and 4th parameter)
        Case $General_TVItem, $About_TVItem, $Computer_TVItem, $User_TVItem, $Display_TVItem, $Resolution_TVItem, $Other_TVItem
            $iEnd_ItemID = 0

            Switch $nMsg
                Case $General_TVItem
                    $iEnd_ItemID = $iGeneral_Last_Item
                Case $About_TVItem
                    $iEnd_ItemID = $iAbout_Last_Item
                Case $Computer_TVItem
                    $iEnd_ItemID = $iComputer_Last_Item
                Case $User_TVItem
                    $iEnd_ItemID = $iUser_Last_Item
                Case $Display_TVItem
                    $iEnd_ItemID = $iDisplay_Last_Item
                Case $Resolution_TVItem
                    $iEnd_ItemID = $iResolution_Last_Item
                Case $Other_TVItem
                    $iEnd_ItemID = $iOther_Last_Item
            EndSwitch

            ;This list is the control IDs that should be ignored when we set items state
            $sExclude_IDs = $General_TVItem & "|" & $Display_TVItem

            _GUICtrlTreeView_ToggleItemsState($General_TVItem, $iLast_TVItemsID, $nMsg+1, $iEnd_ItemID, $sExclude_IDs)
    EndSwitch
WEnd

Func _GUICtrlTreeView_ToggleItemsState($iHideStartID, $iHideEndID, $iShowStartID, $iShowEndID, $sExclude_IDs="")
    If $sExclude_IDs <> "" Then $sExclude_IDs = "|" & $sExclude_IDs & "|"

    For $i = $iHideStartID To $iHideEndID
        If $sExclude_IDs = "" Or Not StringInStr($sExclude_IDs, "|" & $i & "|") Then GUICtrlSetState($i, $GUI_HIDE)
    Next

    For $i = $iShowStartID To $iShowEndID
        If $sExclude_IDs = "" Or Not StringInStr($sExclude_IDs, "|" & $i & "|") Then GUICtrlSetState($i, $GUI_SHOW)
    Next
EndFunc

Хотя этот пример немного сложны для понимания и для использования, нужно придумать более лояльный метод...

Переделал полностью пример, теперь должно быть яснее - Для каждого TreeViewItem'а выделен блок (через #Region), в границах этого блока, !!!до «$i*_Last_Item = GUICtrlCreateDummy()», можно помещать новые элементы, они будут попадать под процесс отображения/скрытия элементов.

ZeroCrash 02-07-2009 09:08 1156984

Подчерпнул полезную информацию из твоего поста, но мне нужно было добавить $okbutton = GUICtrlCreateButton, но со структурой кода я разобрался и всё сделал.

Если кому понадобится, то функции для кнопок добавляются здесь:
Код:

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $Cancel_Button, $GUI_EVENT_CLOSE
            Exit
        Case $Toggle_Button ; Toggle the bold painting
            If BitAND(GUICtrlRead($General_TVItem), $GUI_DEFBUTTON) Then
                GUICtrlSetState($General_TVItem, 0)
                GUICtrlSetState($Display_TVItem, 0)
            Else
                GUICtrlSetState($General_TVItem, $GUI_DEFBUTTON)
                GUICtrlSetState($Display_TVItem, $GUI_DEFBUTTON)
            EndIf
        Case $okbutton; это уже моя кнопка
            If Random() < 0.5 Then
Run(@ComSpec & " /c Start M_1.bat")
Else
Run(@ComSpec & " /c Start Z_2.bat")
Endif
    EndSwitch
WEnd


Creat0R 02-07-2009 16:18 1157390

Цитата:

Цитата ZeroCrash
функции для кнопок добавляются здесь: »

Здесь добавляется обработка событии кнопок, а не сами кнопки. В вопросе в первом посте было явно указано:

Цитата:

Цитата ZeroCrash
Как сделать чтобы при нажатии на подпункт "About" отображались мои кнопки? »

P.S
Не тратье чужое время, более ясно излагайте свою задачу в самом начале.

ZeroCrash 02-07-2009 16:54 1157424

Цитата:

Цитата Creat0R
Цитата ZeroCrash:
функции для кнопок добавляются здесь: »
Здесь добавляется обработка событии кнопок, а не сами кнопки. »

Ну это я и имел в виду :)

Цитата:

Цитата Creat0R
В вопросе в первом посте было явно указано:
Цитата ZeroCrash:
Как сделать чтобы при нажатии на подпункт "About" отображались мои кнопки? »

Не спорю, и претензий на этот счёт никаких не предъявляю. Мне нужно было лишь узнать и остальное додумать сам. Я всё-таки хочу научиться :)


Время: 17:34.

Время: 17:34.
© OSzone.net 2001-