Showing WPF Info Message

by May 5, 2014

WPF (Windows Presentation Foundation) is a technology that enables you to create windows and dialogs. The advantage of WPF is that the window design can be separated from program code.

Here is a sample that displays a catchy message. The message is defined in XAML code which works similar to HTML (but is case-sensitive). You can easily adjust font size, text, colors, etc. without the need to touch any code:

$xaml = @"
<Window
 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>

 <Border BorderThickness="20" BorderBrush="Yellow" CornerRadius="9" Background='Red'>
  <StackPanel>
   <Label FontSize="50" FontFamily='Stencil' Background='Red' Foreground='White' BorderThickness='0'>
    System will be rebooted in 15 minutes!
   </Label>

   <Label HorizontalAlignment="Center" FontSize="15" FontFamily='Consolas' Background='Red' Foreground='White' BorderThickness='0'>
    Worried about losing data? Talk to your friendly help desk representative and freely share your concerns!
   </Label>
  </StackPanel>
 </Border>
</Window>
"@

$reader = [System.XML.XMLReader]::Create([System.IO.StringReader] $xaml)
$window = [System.Windows.Markup.XAMLReader]::Load($reader)
$Window.AllowsTransparency = $True
$window.SizeToContent = 'WidthAndHeight'
$window.ResizeMode = 'NoResize'
$Window.Opacity = .7
$window.Topmost = $true
$window.WindowStartupLocation = 'CenterScreen'
$window.WindowStyle = 'None'
# show message for 5 seconds:
$null = $window.Show()
Start-Sleep -Seconds 5
$window.Close()

Twitter This Tip! ReTweet this Tip!