Created
August 21, 2024 21:33
-
-
Save ajkessel/d1012bf4e29998aeeac8c33c6db9e6c2 to your computer and use it in GitHub Desktop.
A simple PowerShell GUI clickable form with four buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# it is oddly difficult to find good, easy examples of creating PowerShell GUI forms with non-standard buttons | |
# this is a simple example that displays a four-button form | |
# you can, of course, customize everything in the <grid> | |
# and will need to set the window width and height to match the number of buttons and font | |
# use and adapt freely, subject to unlicense https://choosealicense.com/licenses/unlicense/ | |
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') | |
[xml]$XAML = @' | |
<Window | |
xmlns = | |
"http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" | |
Name = "simpleform" | |
Title = "A Simple Form With Four Selectable Buttons" | |
WindowStartupLocation = "CenterScreen" | |
Width = "200" | |
Height = "150" | |
FontSize = "18" | |
Background = "Blue" | |
Foreground = "White" | |
ShowInTaskbar = "True"> | |
<Grid> | |
<StackPanel x:Name='StackPanel'> | |
<Button x:Name="Show" Content = 'Show'/> | |
<Button x:Name="Force" Content = 'Force'/> | |
<Button x:Name="OK" Content = 'OK'/> | |
<Button x:Name="Stop" Content = 'Stop'/> | |
</StackPanel> | |
</Grid> | |
</Window> | |
'@ | |
[System.Windows.RoutedEventHandler]$Script:Handler = { | |
$result = $_.source | |
$MainForm.Close() | |
} | |
$Reader = New-Object System.Xml.XmlNodeReader $XAML | |
$MainForm = [Windows.Markup.XamlReader]::Load($Reader) | |
$MainForm.AddHandler([System.Windows.Controls.Primitives.ButtonBase]::ClickEvent, $Handler) | |
$MainForm.ShowDialog() | Out-Null | |
if ( $result -match 'show' ) { | |
# code if "show" is selected | |
} elseif ( $result -match 'force' ) { | |
# code if "force" is selected | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment