Guest User

Untitled

a guest
Nov 16th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. # These are the starting folders
  2. $documentRoots = 'C:\Users\', 'C:\Program Files\'
  3.  
  4.  
  5. # Load Windows forms assemblies
  6. [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  7. [void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  8.  
  9.  
  10. #
  11. # documentsToolStripMenuItem - this is the bit which says "Documents" along the top
  12. #
  13. $documentsToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
  14. $documentsToolStripMenuItem.Name = "documentsToolStripMenuItem"
  15. $documentsToolStripMenuItem.Text = "&Documents"
  16.  
  17.  
  18. #
  19. # documents helper function, this generates submenus on-the-fly.
  20. #
  21. function Add-SubMenuItems {
  22.  
  23. # Only make each submenu once.
  24. # If you move the mouse away and back we don't want to recreate it.
  25. if ($this.DropDownItems.Count -eq 1 -and $this.DropDownItems[0].Text -eq '')
  26. {
  27. $this.DropDownItems.Clear() # Remove placeholder
  28.  
  29. # Add new menu items for each file or directory.
  30. # - directories fill in their contents into their submenu when hovered over.
  31. # - files print their name when clicked.
  32. [array]$items = Get-ChildItem -LiteralPath $this.Tag -ErrorAction SilentlyContinue |
  33. Sort-Object -Property PSIsContainer, Name
  34.  
  35. if ($items.Count -gt 0)
  36. {
  37. $items | ForEach-Object {
  38.  
  39. $tempItem = New-Object System.Windows.Forms.ToolStripMenuItem -ArgumentList $_.Name
  40. $tempItem.Tag = $_.FullName
  41.  
  42. if ($_.PsIsContainer) # Directory - add a blank submenu item, so it has the > hint
  43. {
  44. $tempItem.Add_MouseHover({ Add-SubMenuItems })
  45. $tempSubSubMenu = New-Object System.Windows.Forms.ToolStripMenuItem
  46. [void]$tempItem.DropdownItems.Add($tempSubSubMenu)
  47. }
  48. else # it's a file, add a Click handler
  49. {
  50. $tempItem.Add_Click({
  51. Write-Host -ForegroundColor Magenta "$($this.Tag)"
  52. })
  53. }
  54.  
  55. # add each new item to the menu
  56. [void]$this.DropDownItems.Add($tempItem)
  57. }
  58. }
  59. else
  60. {
  61. $this.Text = $this.Text + ' (empty)'
  62. }
  63. }
  64. }
  65.  
  66. # For each document root,
  67. # Create a menu entry to go under "Documents".
  68. # Give it an empty sub menu, so it has the > arrow.
  69. # Register an event handler so it responds to mouse hover.
  70. # Add it to the document parent menu.
  71. #
  72. # This can't use the helper function because it
  73. # adds to the Documents top-level menu.
  74. foreach ($root in $documentRoots)
  75. {
  76. $tempItem = New-Object System.Windows.Forms.ToolStripMenuItem -ArgumentList ($root)
  77. $tempItem.Add_MouseHover({ Add-SubMenuItems })
  78. $tempItem.Tag = $root
  79. $tempSubMenu = New-Object System.Windows.Forms.ToolStripMenuItem
  80. [void]$tempItem.DropDownItems.Add($tempSubMenu)
  81.  
  82. [void]$documentsToolStripMenuItem.DropDownItems.Add($tempItem)
  83. }
  84.  
  85. #
  86. # Main menu bar
  87. #
  88. $MenuStrip = new-object System.Windows.Forms.MenuStrip
  89. [void]$MenuStrip.Items.Add($documentsToolStripMenuItem)
  90. $MenuStrip.Location = new-object System.Drawing.Point(0, 0)
  91. $MenuStrip.Name = "MenuStrip"
  92. $MenuStrip.Size = new-object System.Drawing.Size(354, 24)
  93. $MenuStrip.TabIndex = 0
  94. $MenuStrip.Text = "menuStrip1"
  95.  
  96.  
  97. #
  98. # Main Form
  99. #
  100. $MenuForm = new-object System.Windows.Forms.form
  101. $MenuForm.ClientSize = new-object System.Drawing.Size(354, 141)
  102. [void]$MenuForm.Controls.Add($MenuStrip)
  103. $MenuForm.MainMenuStrip = $MenuStrip
  104. $MenuForm.Name = "MenuForm"
  105. $MenuForm.Text = "I've got a menu"
  106. function OnFormClosing_MenuForm($Sender,$e){
  107. # $this represent sender (object)
  108. # $_ represent e (eventarg)
  109.  
  110. # Allow closing
  111. ($_).Cancel= $False
  112. }
  113. $MenuForm.Add_FormClosing( { OnFormClosing_MenuForm $MenuForm $EventArgs} )
  114. $MenuForm.Add_Shown({$MenuForm.Activate()})
  115. $MenuForm.ShowDialog()
  116. #Free ressources
  117. $MenuForm.Dispose()
Add Comment
Please, Sign In to add comment