Tuesday, October 7, 2014

Creating your own personalised HBCDMenu - Part 2

In Part 1 of this tutorial, we created our menu's GUI, now in the second part we will add all of our functionality to the menu. Mainly, populate of "Programs" drop-down menu.
We finished last time with the Main GUI of our program, the main part needed being our "Programs" drop-down menu. This will contain all of the programs we have added to our csv file (or the one supplied by Hirens Boot CD.).

This is where it get a little complicated so if you have no interest in learning or don't want to do any work simply go to the end of the article to download the completed product (including the AutoIt script for modification.)

The main features of this script are the two functions we will use to get our menu items. These being:
  1. MenuGet() - This function will get our 'Top-Level' menu items. (The top row of our csv file.)
  2. ParseMenu() - This function will get the rest of our menu items. (Everything under the top row.)
But before we start looking at our functions, lets start by looking at our global variables. These will be defined at the beginning of our script and used throughout our various functions.
#include <File.au3>
Global $aMenuItems[1][2] = [[0]]
Global $aClickItems[1] = [0]
Global $FilePath = ".\Programs\HBCDMenu.csv"
 
;Check if our menu file actually exists
If Not FileExists($FilePath) Then
    MsgBox(0, "Error!", "Unable to locate Menu File!")
    Exit
EndIf
 
;This variable is used to store our file content
Global $content
_FileReadToArray($FilePath, $content)
To explain this, we have 4 variables defined here:
  1. $aMenuItems - This is an array that will store our menu item switches (not the names, just the programmatic switches.)
  2. $aClickItems - This is an array that will store our click item names (this time it is the names, as we need it in our Run() function later.)
  3. $FilePath -This is the path to our menu file beginning with '.\' meaning its relative from our script location.
  4. $content - This is the variable that will hold the contents of our 'Menu' file for processing later.
While we are looking at this section of code, I will verify the last couple of snippets:
  1. #include <File.au3> - We need to include this for our _FileReadToArray function needed later.
  2. If Not FileExists($FilePath) Then - This line and the following three (3) lines are simply checking if our 'Menu' file is present, if it isn't it will alert us.
  3. _FileReadToArray($FilePath, $content) - This line of code will get the content of our 'Menu' file and load them into our content variable.
Next lets look at our MenuGet() function:
Func MenuGet()
 $menuTitles = StringSplit($content[1], ",") ; These are our Top-Level menus
 
 ; Create an array to hold the Menu Names
 Global $MenuTitle[$menuTitles[0] + 1] = [$menuTitles[0]]
 
 ; Create the Top-Level menus
 For $i = 1 To $menuTitles[0]
  ;Don't forget to make our Main Menu the parent
  $MenuTitle[$i] = GUICtrlCreateMenu($menuTitles[$i], $MenuItem1)
 Next
EndFunc   ;==>MenuGet
Now let me explain the various parts in detail:
$menuTitles = StringSplit($content[1], ",") ; These are our Top-Level menus
This is a temporary array that will store our menu item names. We get this array by splitting the first line ($content[1]) by the commas (Because its a comma separated value or csv file.)
; Create an array to hold the Menu Names
Global $MenuTitle[$menuTitles[0] + 1] = [$menuTitles[0]]
This is an array that will store the top-level menu items, these are all going to be dropdown menus. We create this to be the size of our $menuTitles array and adding the array count to $MenuTitle[0].
; Create the Top-Level menus
For $i = 1 To $menuTitles[0]
 ;Don't forget to make our Main Menu the parent
 $MenuTitle[$i] = GUICtrlCreateMenu($menuTitles[$i], $MenuItem1)
Next 
Now we need to loop through each of our array items (menu titles) and create their very own GUI item. We can't forget to make our 'Main Programs Menu' (Labeled 'Programs' in this Tutorial) our parent item.

Next, lets look at the ParseMenu() function, this is where all the magic happens. The function that will parse all of the options from our HBCDMenu.csv file.
Here it is:
Func ParseMenu()
 ; Get rid of the first line as we already have our Top-Level Menus
 _ArrayDelete($content, 1)
 ;Load the first line into an array (for our Count Later)
 $columnsCounter = StringSplit($content[1], ",")
 ;Here we use the row count (from $content) and column count (from $columnsCounter)
 ;We define an array that is the perfect size for our Menu Items
 Dim $MenuItems[$content[0] + 1][$columnsCounter[0] + 1]
 
 ; Now we loop through each row and column
 For $x = 1 To ($content[0]) - 1
  ;Create an array with the row we are looking at
  $oneRow = StringSplit($content[$x], ",")
  ;now loop through each column
  For $y = 1 To ($columnsCounter[0])
   ;Grab the item we want and add it to our menu items array
   $MenuItems[$x][$y] = $oneRow[$y]
  Next
 Next
 
 ;Count our rows
 $rowCount = UBound($MenuItems, 2)
 ;Count our columns
 $columnCount = UBound($MenuItems, 1)
 
 ;Set our initial value and create a 'While' loop, for our rows
 $i = 1
 While $i < $rowCount
  ;create a second initial value and another 'While' loop, for our columns
  $j = 1
  While $j < $columnCount
   ;We need to make sure we aren't working on an empty string
   If Not $MenuItems[$j][$i] = "" Then
    ;Create our menu items
    ;Increase the count of menuitems
    $aMenuItems[0][0] += 1
    ; Increase the array size by adding a new element
    ReDim $aMenuItems[$aMenuItems[0][0] + 1][2]
    ; Add the ControlID and text to the newly created array element
    $aMenuItems[$aMenuItems[0][0]][0] = GUICtrlCreateMenuItem($MenuItems[$j][$i], $MenuTitle[$i])
    $aMenuItems[$aMenuItems[0][0]][1] = $MenuItems[$j][$i]
    ;Increase the count on our global click items array
    $aClickItems[0] += 1
    ;Increase the array size
    ReDim $aClickItems[$aClickItems[0] + 1]
    ;Add our 'Click' command (notice the +1, this is so we get the batch file path)
    $aClickItems[$aClickItems[0]] = $MenuItems[$j + 1][$i]
   EndIf
   ;We increment by 2 so we skip over our 'Click' commands
   $j += 2
  WEnd
  $i += 1
 WEnd
 
EndFunc   ;==>ParseMenu
Now, lets look at what is going on here. Its pretty well commented in the script, so I will explain only the main parts.
The first part:
 ; Now we loop through each row and column
 For $x = 1 To ($content[0]) - 1
  ;Create an array with the row we are looking at
  $oneRow = StringSplit($content[$x], ",")
  ;now loop through each column
  For $y = 1 To ($columnsCounter[0])
   ;Grab the item we want and add it to our menu items array
   $MenuItems[$x][$y] = $oneRow[$y]
  Next
 Next
Gets all of the values from our content array (defined at the beginning of the script) and throws them into an array for us to work with, practically splitting each item into its own cozy little box in an array. This will make it much easier to work on in our next section:
 ;Set our initial value and create a 'While' loop, for our rows
 $i = 1
 While $i < $rowCount
  ;create a second initial value and another 'While' loop, for our columns
  $j = 1
  While $j < $columnCount
   ;We need to make sure we aren't working on an empty string
   If Not $MenuItems[$j][$i] = "" Then
    ;Create our menu items
    ;Increase the count of menuitems
    $aMenuItems[0][0] += 1
    ; Increase the array size by adding a new element
    ReDim $aMenuItems[$aMenuItems[0][0] + 1][2]
    ; Add the ControlID and text to the newly created array element
    $aMenuItems[$aMenuItems[0][0]][0] = GUICtrlCreateMenuItem($MenuItems[$j][$i], $MenuTitle[$i])
    $aMenuItems[$aMenuItems[0][0]][1] = $MenuItems[$j][$i]
    ;Increase the count on our global click items array
    $aClickItems[0] += 1
    ;Increase the array size
    ReDim $aClickItems[$aClickItems[0] + 1]
    ;Add our 'Click' command (notice the +1, this is so we get the batch file path)
    $aClickItems[$aClickItems[0]] = $MenuItems[$j + 1][$i]
   EndIf
   ;We increment by 2 so we skip over our 'Click' commands
   $j += 2
  WEnd
  $i += 1
 WEnd
With this section we use all of the items we have already defined an finally create our "Programs" menu (and it all works too).

Now that is the entire program all looked at. Please keep in mind a few things:

  1. This is the very first draft of the program and may have a couple of bugs
  2. It does not split the programs by the backslash "\" like the official HBCDMenu does. So only 2 level "Programs" menu
  3. This does use a scripting language (not a programming language like .NET or Java) so it might be limited in functionality
  4. I have not tested this in any preinstalled environments (MiniXP or Mini7)
  5. Fell free to take this code and improve on it in any way you want. Let me know of any improvements and I may include them into the next version.
Link to full zip: Download Here