Display WPF Popups in Lower Right Corner of Screen using PowerShell

I've recently changed my mind on this, but will leave the previous post up in case you want to reposition a WPF popup. If you want a big window to popup in the lower-right corner of the screen, use a WPF Window. WPF Popups are chromeless, it's true, but they are also limiting in how you can move them around, and how they open and close. ToolTips and BalloonTips are useful, too, but have a character limit and are limiting in other ways.

Here is a super basic lower-right hand corner Window that works no matter how many monitors you have, unlike the popup solution below.

# Add the required assemblies Add-Type -AssemblyName PresentationFramework,System.Windows.Forms

Add a super simple form that contains a just with text within a grid

[xml]$xaml = ' '

Turn the xaml into a form

$script:window = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml)) $xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $window.FindName($_.Name) -Scope Script }

Double click the window

$window.Add_MouseDoubleClick({ $window.Close() })

$window.Left = $([System.Windows.SystemParameters]::WorkArea.Width-$window.Width) $window.Top = $([System.Windows.SystemParameters]::WorkArea.Height-$window.Height) $window.ShowDialog() | Out-Null

And here is what it looks like:

popup

If you want to use the WPF Popup object

If you don't want to use a WPF Window and you find yourself limited by notifyicon's BalloonTips functionality, you can use WPF Popups to display information instead.

audiopopup1

This picture is probably inaccurate ;)

Considering the nature of popups, placing them in the lower right should be easy, right? Well, Microsoft's guide for popup placement was confusing to me, and StackExchange's top-rated responses weren't much more straight forward.

So after about twenty minutes of trial and error, I figured out that in order to create a popup in the lower right hand side of the screen, three things are required. First, the Popup's Placement property must be set to AbsolutePoint. Then, the screen's working area has to be determined and its values used to update the popup's VerticalOffset and HorizontalOffset. This is similar to winform's location property.

The following code will create a popup in the lower right hand corner if you have a single monitor. I don't have a consistent solution for multiple monitors and Popups. Note that it will not create a notifyicon. That will be covered in my next post.

# Add a super simple form that contains a just with text within a grid [xml]$xaml = '

'

Add the required assemblies

Add-Type -AssemblyName PresentationFramework,System.Windows.Forms

Turn the xaml into a form

$script:form = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml)) $xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $form.FindName($_.Name) -Scope Script }

Use Windows.Forms.Screen to determine the size of the screen.

Make it -1 so that it will not slide over on multiple monitors

$screen = [Windows.Forms.Screen]::PrimaryScreen.WorkingArea $popup.VerticalOffset=$screen.height $popup.HorizontalOffset=$screen.width-1

Having the popup just set to isOpen won't necessarily make it appear.

Unsure why this is required, but it is.

$popup.Add_Loaded({$popup.IsOpen=$true})

Double click the popup form to make it go away.

$form.Add_MouseDoubleClick({ $popup.IsOpen = $false $form.Close() })

[void]$form.ShowDialog()

And here is what it looks like:

popup

This code will work for any sized popup. Also, this form's Window and Popup objects were stripped down to only the required properties. If you remove any of them, it won't work as demonstrated.

I'm not a GUI pro, but my understanding of this is that an invisible form hosts a popup. You can't see the form, and only the popup is visible. While StaysOpen="False" usually allows the popup to close on LostFocus, it doesn't work that way in this form. If StaysOpen is left out, the popup will never respond to the $form's MouseDoubleClick event. You'll just hear a ding when clicking the popup to close it. I'm assuming that's Windows' way of saying the action is forbidden.