Jump to content

Recommended Posts

Posted (edited)

Not particularly complicated, but very useful. Right click on the treeview to add a child or other node, and click "Generate code" to get the code.

It only assigns variables to the bare minimum of menus, as a result if you want to use it in a message loop then you need to add all your own variable names, but for this too be useful you are probably looking at the bigger menus for applications, and you are probably better off using OnEvent mode (You will still have to add the calls to GUICtrlSetOnEvent

#Include<GUITreeview.au3>

Global $sStr = ""

Opt ("GUIOnEventMode", 1)

Global $hGUI = GUICreate ("Menu helper!", 300, 500, -1, -1)
   GUISetOnEvent (-3, "_Exit")

   GUICtrlCreateButton ("=== Generate menu code ===", 48, 2, 200, 20)
      GUICtrlSetOnEvent (-1, "_Generate")

   GUICtrlCreateButton ("=== Generate Treeview code ===", 48, 24, 200, 20)
      GUICtrlSetOnEvent (-1, "_GenerateTree")

   Global $hTrv_Id = GUICtrlCreateTreeview (2, 46, 296, 452, 0x5000020f, 0x00000200)
      $hRoot = GUICtrlCreateContextMenu ($hTrv_Id)
         GUICtrlCreateMenuItem ("Edit", $hRoot)
            GUICtrlSetOnEvent (-1, "_Edit")
         GUICtrlCreateMenuItem ("Delete Selected", $hRoot)
            GUICtrlSetOnEvent (-1, "_Delete")
         GUICtrlCreateMenuItem ("Create Child", $hRoot)
            GUICtrlSetOnEvent (-1, "_CreateChild")
         GUICtrlCreateMenuItem ("Create After", $hRoot)
            GUICtrlSetOnEvent (-1, "_CreateAfter")
         GUICtrlCreateMenuItem ("Create Before", $hRoot)
            GUICtrlSetOnEvent (-1, "_CreateBefore")

      GUICtrlCreateTreeViewItem ("Root", $hTrv_Id)

   GUISetState ()
   GUIRegisterMsg (0x004E, "WM_NOTIFY")

   While 1
      Sleep (1000)
   WEnd

Func _Generate ()
   Local $sRet = ""
   $hRoot = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 0, 0)
   _GetAllDescendantItems($hRoot)
   $sStr = StringTrimRight ($sStr, 1)
   $aParts = StringSplit ($sStr, @LF)
   $nTierOld = 0
   $nTierMin = 0
   $MaxInd = 0
   For $i = 1 to $aParts[0]
      $nTier = StringRegExpReplace ($aParts[$i], "(.*?):.*", "\1") + 0
      $aParts[$i] = StringRegExpReplace ($aParts[$i], ".*?:(.*)", "\1")
      If $aParts[$i] = "-" Then $aParts[$i] = ""
      $Item = "Item"
      $var = ""
      If $i < UBound ($aParts) - 1 Then
         If StringRegExpReplace ($aParts[$i + 1], "(.*?):.*", "\1") + 0 > $nTier Then
            $Item = ""
            $var = "$aMenu[" & $nTier & "] = "
         EndIf
      EndIf
      If $nTier > $nTierMin Then
         $MaxInd += 1
         $sRet &= @CRLF & _GetTab ($nTier) & $var & "GUICtrlCreateMenu" & $Item & " (""" & $aParts[$i] & """, $aMenu[" & $nTier - 1 & "])"
      ElseIf $nTier = $nTierMin Then
         $sRet &= @CRLF & _GetTab ($nTier) & $var & "GUICtrlCreateMenu" & $Item & " (""" & $aParts[$i] & """, $aMenu[" & $nTierOld & "])"
      EndIf
   Next
   $sRet = "Local $aMenu[" & $MaxInd + 1 & "]" & @CRLF & _GetTab (1) & "$aMenu[0] = -1 ; For the root menu!" & $sRet
   ClipPut ($sRet)
   MsgBox (0, "done", "Menu script copied to the clipboard successfully")
Endfunc ; ==> _Generate

Func _GenerateTree ()
   Local $sRet = ""
   $hRoot = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 0, 0)
   _GetAllDescendantItems($hRoot)
   $sStr = StringTrimRight ($sStr, 1)
   $aParts = StringSplit ($sStr, @LF)
   $sStr = ""
   $nTierOld = 0
   $nTierMin = 0
   $MaxInd = 0
   For $i = 1 to $aParts[0]
      $nTier = StringRegExpReplace ($aParts[$i], "(.*?):.*", "\1") + 0
      $aParts[$i] = StringRegExpReplace ($aParts[$i], ".*?:(.*)", "\1")
      $var = ""
      If $i < UBound ($aParts) - 1 Then
         If StringRegExpReplace ($aParts[$i + 1], "(.*?):.*", "\1") + 0 > $nTier Then $var = "$aTreeView[" & $nTier & "] = "
      EndIf
      If $nTier > $nTierMin Then $MaxInd += 1
      $sRet &= @CRLF & _GetTab ($nTier) & $var & "GUICtrlCreateTreeViewItem (""" & $aParts[$i] & """, $aTreeView[" & $nTier - 1 & "])"
   Next
   $sRet = "Local $aTreeView[" & $MaxInd + 1 & "]" & @CRLF & _GetTab (1) & "$aTreeView[0] = GUICtrlCreateTreeView (2, 2, 150, 150)" & $sRet
   ClipPut ($sRet)
   MsgBox (0, "done", "TreeView script copied to the clipboard successfully")
Endfunc ; ==> _Generatetree

Func _GetTab ($n)
   Local $ret = ""
   For $i = 1 to $n
      $ret &= "   "
   Next
   Return $ret
EndFunc ; ==> _GetTab

Func _GetAllDescendantItems($hParent, $nCur = 1)
   $hItem = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 4, $hParent)

   While $hItem
      $sStr &= $nCur & ":" & _GUICtrlTreeView_GetText (GUICtrlGetHandle ($hTrv_Id), $hItem) & @LF
      _GetAllDescendantItems($hItem, $nCur + 1)
      $hItem = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 1, $hItem)
   WEnd
EndFunc

Func _Edit ()
   If ControlTreeView ($hGUI, "", $hTrv_ID, "GetSelected") = "Root" Then Return 1
   $cur = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 9, 0)
   If $cur = 0 Then return
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 14, 0, $cur)
EndFunc ; ==> _Edit

Func _Delete ()
   If ControlTreeView ($hGUI, "", $hTrv_ID, "GetSelected") = "Root" Then Return 1
   $cur = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 9, 0)
   If $cur = 0 Then return
   $cur = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 1, 0, $cur)
EndFunc ; ==> _Delete

Func _CreateChild ()
   $cur = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 9, 0)
   If $cur = 0 Then return
   $new = _GUICtrlTreeView_AddChild (GUICtrlGetHandle ($hTrv_Id), $cur, "")
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 2, 0x0002, $cur)
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 11, 9, $new)
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 14, 0, $new)
EndFunc ; ==> _CreateChild

Func _CreateAfter ()
   If ControlTreeView ($hGUI, "", $hTrv_ID, "GetSelected") = "Root" Then Return 1
   $cur = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 9, 0)
   If $cur = 0 Then return
   $new = _GUICtrlTreeView_Add (GUICtrlGetHandle ($hTrv_Id), $cur, "")
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 2, 0x0002, $cur)
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 11, 9, $new)
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 14, 0, $new)
EndFunc ; ==> _CreateAfter

Func _CreateBefore ()
   If ControlTreeView ($hGUI, "", $hTrv_ID, "GetSelected") = "Root" Then Return 1
   $cur = GUICtrlSendMsg ($hTrv_Id, 0x1100 + 10, 9, 0)
   If $cur = 0 Then return
   $new = _GUICtrlTreeView_AddFirst (GUICtrlGetHandle ($hTrv_Id), $cur, "")
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 2, 0x0002, $cur)
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 11, 9, $new)
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 14, 0, $new)
EndFunc ; ==> _CreateBefore

Func WM_NOTIFY ($hWnd, $msgID, $wParam, $lParam)
   $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
   $HwndFrom = DllStructGetData($tNMHDR, "HwndFrom")
   $iCode = DllStructGetData($tNMHDR, "Code")
   If ($HwndFrom = GUICtrlGetHandle ($hTrv_Id)) And ($iCode <> -17) Then consoleWrite ($iCode)
   If $iCode = -460 Then
      Local $tInfo = DllStructCreate($tagNMTVDISPINFO, $lParam)
      Local $sBuffer = DllStructCreate("wchar Text[" & DllStructGetData($tInfo, "TextMax") & "]")
      DllStructSetData($sBuffer, "Text", DllStructGetData($tInfo, "Text"))
      If StringLen(DllStructGetData($sBuffer, "Text")) Then Return 1
   ElseIf $iCode = -459 Then
      If ControlTreeView ($hGUI, "", $hTrv_ID, "GetSelected") = "Root" Then Return 1
      HotKeySet("{Enter}", "_TextSet")
   EndIf
EndFunc ; ==> WM_NOTIFY

Func _TextSet ()
   GUICtrlSendMsg ($hTrv_Id, 0x1100 + 22, False, 0)
   HotKeySet("{Enter}")
EndFunc ; ==> _TextSet

Func _Exit ()
   Exit
EndFunc ; ==> _Exit

Mat

Edited by Mat
Posted

Nice idea, but...

Have you checked the code before posting? there is few syntax errors. And even after fixing them i got wrong data in my clipboard after generating.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Try again now. I'm not sure what your problem was but the code now posted has just given me a full menu for an app I'm making. As far as i'm aware its identical to the one posted earlier. If not then I posted the wrong one then.

Mat

Posted

It's not identical at all, but thanks for the update, now working fine. Except the syntax checking, i got this error:

WARNING: $sStr possibly not declared/created yet

$sStr &= $nCur & ":" & _GUICtrlTreeView_GetText (GUICtrlGetHandle ($hTrv_Id), $hItem) & @LF

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

You should declare global variables outside any functions.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

It's not identical at all, but thanks for the update, now working fine. Except the syntax checking, i got this error:

You should declare global variables outside any functions.

Is it not identical? My bad... sorry.

I probably should, but I hate those kind of errors, because if you look you look at the code, its impossible that $sStr is not already declared, so its not really a problem...

Zedna, I never knew that! But then... I rarely use Koda at all, when I have, its resulted in me rewriting the code anyway. I tried now to use it, its a lot more complex than this, and more visual.

Mat

Posted

if you look you look at the code, its impossible that $sStr is not already declared, so its not really a problem...

Theoretically it's not, but programatically you should take care of all global vars outside, not inside a function.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...