简体   繁体   中英

Open WPF Window on top, but don't always keep on top using Powershell

I've ran into an issue while using WPF. When I open the window, I want it to open up on top of all other programs but I don't want it to remain always on top, just the initial opening. I know setting topmost to true will open up on top (which I have in the Xaml), but I can't seem to find a way to change it to false after it has opened.

This is a simple test function with a WPF window.

function foo{
    #Load Assembly and Library
    Add-Type -AssemblyName PresentationFramework

    $inputXaml = @"
    <Window x:Class="SharepointCreateOpportunity.completeWindow"
        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:SharepointCreateOpportunity"
        mc:Ignorable="d"
        Title="Window Title" Height="250" MinHeight="250" MaxHeight="250" Width="500" MinWidth="500" MaxWidth="500" Topmost="True" WindowStartupLocation="CenterScreen" Background="Black" >

    <Grid>
        <Button Name="oKBtn" Content="OK" Margin="0,0,20,20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="72" Height="23"/>
    </Grid>
</Window>
"@
    #Gets rid of elements from the Xaml so it can be converted
    $inputXamlClean = $inputXaml -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"',''
    [xml]$xaml = $inputXamlClean

    #Creates the Window
    $XMLReader = (New-Object System.Xml.XmlNodeReader $xaml)
    $Window = [Windows.Markup.XamlReader]::Load($XMLReader)

    #Creates variables for all the elements on the window
    $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)}

    #OK Button Action
    $okBtn.Add_Click({
        $Window.Close()
    })

    #Show window
    $Window.ShowDialog()
}

foo

If Topmost is not set in the XAML, the window will appear on top when the script is run (assuming there are no other windows with Topmost set) and will behave like a normal desktop window where focusing on other windows with the mouse or keyboard will cause the window to lose foreground position.

So for the window to be on top for "just the initial opening," getting rid of the Topmost property or setting it to false will work, again assuming there are no other windows with Topmost set. If there are in fact other windows vying for the topmost position, you could use SetForegroundWindow to put it temporarily to the top:

https://stackoverflow.com/a/12802050/4195823

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM