Hi all,
I'm developing a tray app to dynamically load and run my scripts. Loading directories and their scripts works fine. I also want to include themes, ensuring only one theme can be selected at a time.
However, I'm struggling with getting the items to execute properly using SetMenuHandler or add_Click({}). The items don't seem to respond as expected.
Any ideas on what might be going wrong? I'm also open to suggestions on techniques or concepts I should look into to solve this issue.
Thanks in advance!
I'm developing a tray app to dynamically load and run my scripts. Loading directories and their scripts works fine. I also want to include themes, ensuring only one theme can be selected at a time.
However, I'm struggling with getting the items to execute properly using SetMenuHandler or add_Click({}). The items don't seem to respond as expected.
Any ideas on what might be going wrong? I'm also open to suggestions on techniques or concepts I should look into to solve this issue.
Thanks in advance!
Code:
FUNCTION Build-ContextMenu {
IF ($SAPIENHost -ne $null) {
$Menu = $SAPIENHost.GetContextMenuStrip()
WHILE ($Menu.Items.Count -gt 2) {
$Menu.Items.RemoveAt(0)
}
Add-ThemeItems
Add-ScriptItems
}
}
FUNCTION Add-ThemeItems {
$JsonPath = Join-Path -Path (Get-ScriptDirectory) -ChildPath "Themes.json"
IF (-not (Test-Path -Path $JsonPath -PathType Leaf)) {
$null = [System.Windows.Forms.MessageBox]::Show("$JsonPath not found", "404 Not Found", 'OK', 'Error')
RETURN
}
$Menu = $SAPIENHost.GetContextMenuStrip()
$ThemeMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$ThemeMenuItem.Name = "Themeauswahl"
$ThemeMenuItem.Text = "Themeauswahl"
$Menu.Items.Insert($Menu.Items.Count - 2, $ThemeMenuItem) | Out-Null
$Themes = [System.Collections.Generic.List[System.String]]::new()
$Json = Get-Content $JsonPath | ConvertFrom-Json
FOREACH ($key IN $Json.PSObject.Properties) {
$Themes.Add($key.Name.Trim())
}
FOREACH ($Theme IN $Themes) {
$ThemeSubmenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$ThemeSubmenuItem.Name = $Theme
$ThemeSubmenuItem.Text = $Theme
$ThemeMenuItem.DropDownItems.Add($ThemeSubmenuItem) | Out-Null
$action = {
Write-Log -Text "Theme has changed - $($this.Text)" -PrintOut
$global:Theme = $this.Text
$ThemeMenuItem.DropDownItems | where { $_.Text -ne $this.Text } | foreach { $_.Checked = $false }
$this.Checked = $true
}.GetNewClosure()
Write-Log -Text $ThemeMenuItem.DropDownItems[0].GetType() -PrintOut
$SAPIENHost.SetMenuHandler($ThemeSubmenuItem, $action)
}
$handler = [System.Windows.Forms.ToolStripItemClickedEventHandler]{
PARAM ($sender,
$e)
Write-Log -Text "Theme clicked... $($e.ClickedItem.Text)" -PrintOut
$global:Theme = $e.ClickedItem.Text
$ThemeMenuItem.DropDownItems | where { $_.Text -ne $e.ClickedItem.Text } | foreach { $_.Checked = $false }
$e.ClickedItem.Checked = $true
}
$ThemeMenuItem.add_DropDownItemClicked($handler)
}
FUNCTION Add-ScriptItems {
$Menu = $SAPIENHost.GetContextMenuStrip()
$scriptDirectories = Locate-Scripts
FOREACH ($directory IN $scriptDirectories.Keys) {
$ScriptMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$ScriptMenuItem.Name = $directory
$ScriptMenuItem.Text = $directory.Substring(3)
FOREACH ($script IN $scriptDirectories[$directory]) {
$ScriptSubmenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
$ScriptSubmenuItem.Name = $script
$ScriptSubmenuItem.Text = $script.Substring(3, $script.Length - 3).Replace("-", " ")
$SAPIENHost.SetMenuHandler($ScriptSubmenuItem, { Execute-Skript -Directory $directory -Script $script }.GetNewClosure())
$ScriptMenuItem.DropDownItems.Add($ScriptSubmenuItem) | Out-Null
}
$Menu.Items.Insert($Menu.Items.Count - 2, $ScriptMenuItem) | Out-Null
}
}
Statistics: Posted by Wayssm__ — Wed Oct 23, 2024 3:42 am