If I open a modeless dialog from within my Powershell script via $WPFdialog.Show()
, the window appears but doesn't react to clicks on the taskbar icon. I want it to be able to minimize and restore from there. If I open it via ShowDialog()
there's no problem with that.
I do not want to use a modal dialog because I need the GUI thread to check for background task activities. My approach to this is to call DoEvents()
next to the background activity handling. This works fine on resizing or closing the window, but the dialog doesn't react to clicks on the taskbar. In fact it doesn't seem to receive the WM_SYSCOMMAND SC_MINIMIZE message from the taskbar at all, so I have no idea where to add an appropriate event handler.
Help appreciated. I am on Windows 7 with Powershell 5.1
Sample script:
param( $ARG1, $ARG2, $ARG3, $ARG4 )
Add-Type -AssemblyName System.Windows.Forms
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
################### XAML-Code ###################
$inputXML = @"
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Test App" Height="200" Width="300" ResizeMode="CanResize" WindowStartupLocation="CenterScreen">
<Grid>
<Label HorizontalAlignment="Center" VerticalAlignment="Center">Welcome</Label>
<Button x:Name="Close" Content="Close" HorizontalAlignment="Center" Margin="0,101,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
"@
############### Parse and use XAML ###############
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[xml]$xaml = $inputXML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$WPFdialog=[Windows.Markup.XamlReader]::Load( $reader )
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $WPFdialog.FindName($_.Name)}
$WPFclose.Add_Click({
$WPFdialog.Close()
})
#$res = $WPFdialog.ShowDialog()
$res = $WPFdialog.Show()
do
{
[System.Windows.Forms.Application]::DoEvents()
# planned: handle some background progress here
Start-Sleep -Milliseconds 20
$hwnd = [Windows.PresentationSource]::FromVisual($WPFdialog)
}
while ($hwnd)