Skip to content

Instantly share code, notes, and snippets.

@kmhuglen
Created September 19, 2016 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmhuglen/e523d442cb2f10a49d3d45b330735b36 to your computer and use it in GitHub Desktop.
Save kmhuglen/e523d442cb2f10a49d3d45b330735b36 to your computer and use it in GitHub Desktop.
Param(
)
### FUNCTIONS - STARTS ###
Function LoadForm
{
#http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.button.aspx
#http://stackoverflow.com/questions/13845124/wpf-formatting-a-label
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$Xaml
)
[xml]$Global:xmlWPF = $Xaml
#Add WPF and Windows Forms assemblies
try{
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
Throw "Failed to load Windows Presentation Framework assemblies."
}
#Create the XAML reader using a new XML node reader
$Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))
#Create hooks to each named object in the XAML
$xmlWPF.SelectNodes("//*[@Name]") | %{
Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
}
}
Function OpenIE
{
Param(
$URL = "http://www.google.com/"
)
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate($Url)
$ie.Visible = $True
#Place IE window in front.
$hwnd = $ie.HWND
Add-Type -Namespace PInvoke -Name SWP '[DllImport("user32.dll", SetLastError=true)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);'
[PInvoke.SWP]::SetWindowPos($hWnd, -1, 0, 0, 0, 0, 3)
}
### FUNCTIONS - END ###
$Form = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="$WindowTitle" Height="366" Width="544"
Icon="$WindowIcon">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64" />
<ColumnDefinition Width="400" />
<ColumnDefinition Width="65" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="64" />
<RowDefinition Height="200" />
<RowDefinition Height="65" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.Column="0" Margin="0,0,0,0" Name="Image" Source="$WindowImage" />
<Label Grid.Row="0" Grid.Column="1" Margin="20,5,0,0" Name="LabelTitle" FontSize="25" FontFamily="Calibri" FontWeight="Bold" Content="$WindowTitle" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Label Grid.Row="0" Grid.Column="1" Margin="20,35,0,0" Name="LabelHeading" FontSize="14" FontFamily="Calibri" FontWeight="Bold" Content="$WindowHeading" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Label Grid.Row="1" Grid.Column="1" Margin="20,20,0,0" Name="Label1" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" />
<ComboBox Grid.Row="1" Grid.Column="1" Margin="20,50,20,0" Name="Combobox1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="360" />
<CheckBox Grid.Row="1" Grid.Column="1" Margin="25,90,0,0" Name="CheckBox1" Content="" IsChecked="true" />
<CheckBox Grid.Row="1" Grid.Column="1" Margin="25,120,0,0" Name="CheckBox2" Content="" IsChecked="true" />
<CheckBox Grid.Row="1" Grid.Column="1" Margin="25,150,0,0" Name="CheckBox3" Content="" IsChecked="true" />
<Label Grid.Row="1" Grid.Column="1" Margin="35,170,0,0" Name="Label2" Content="" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="120,170,0,0" Name="TextBox1" Text="" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="200" />
<Button Grid.Row="2" Grid.Column="1" Margin="20,20,0,0" Name="ButtonClose" Content="Close" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="25"/>
<Button Grid.Row="2" Grid.Column="1" Margin="0,20,0,0" Name="ButtonHelp" Content="Help" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Top" Width="100" Height="25"/>
<Button Grid.Row="2" Grid.Column="1" Margin="20,20,20,0" Name="ButtonSend" Content="Send" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Top" Width="100" Height="25" />
</Grid>
</Window>
"@
LoadForm $Form
$WindowTitle = "Window Title"
$WindowHeading = "Window Heading"
$WindowImage = "http://www.google.com/favicon.ico"
$windowIcon = "http://www.google.com/favicon.ico"
$Hashtable = @{
"1" = "Item 1 Result";
"2" = "Item 2 Result";
"3" = "Item 3 Result";
"4" = "Item 4 Result"
}
ForEach ($Item in ($Hashtable.GetEnumerator() | Sort-Object -Property Name))
{
IF ($Item.Value)
{
$Combobox1Items = $Combobox1.Items.Add($Item.Value)
}
}
$CheckBox1.content = "Question A"
$CheckBox2.content = "Question B"
$CheckBox3.content = "Question C"
$TextBox1.Text = "Some Text"
$ButtonSend.add_Click({
$xamGUI.close()
})
$ButtonHelp.add_Click({
OpenIE -URL "www.google.com"
})
$ButtonClose.add_Click({
$xamGUI.close()
})
$xamGUI.WindowStartupLocation = "CenterScreen"
$xamGUI.ShowDialog() | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment