PowerShell WPF display in center of screen

Viewed 78

I am using the following code to play a video at max size in the center of the screen. Issue is it is playing in the center of all of my monitors instead of the main window. How do I resolve this issue?

WindowStartupLocation="CenterScreen"

EDIT: I have tried WindowStartupLocation="CenterOwner" as well and it closer but not 1 full screen

center of ONE monitor is prefered, not center of all of them

<#
.NOTES 
    This will get the dimension of the targets screen to make the wpf form
#>

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
    [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
"@
$hdc = [PInvoke]::GetDC([IntPtr]::Zero)
$w = [PInvoke]::GetDeviceCaps($hdc, 118) # width
$h = [PInvoke]::GetDeviceCaps($hdc, 117) # height

#############################################################################################################################################

#WPF Library for Playing Movie and some components
Add-Type -AssemblyName PresentationFramework

Add-Type -AssemblyName System.ComponentModel
#XAML File of WPF as windows for playing movie

[xml]$XAML = @"
 
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PowerShell Video Player" Height="$h" Width="$w" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Grid Margin="0,0,2,3">
        <MediaElement Height="$h" Width="$w" Name="VideoPlayer" LoadedBehavior="Manual" UnloadedBehavior="Stop" Margin="8,10,10,61" />
    </Grid>
</Window>
"@
 
#Movie Path
[uri]$VideoSource = "$env:TMP\z\e.mp4"
 
#Devide All Objects on XAML
$XAMLReader=(New-Object System.Xml.XmlNodeReader $XAML)
$Window=[Windows.Markup.XamlReader]::Load( $XAMLReader )
$VideoPlayer = $Window.FindName("VideoPlayer")

 
#Video Default Setting
$VideoPlayer.Volume = 100;
$VideoPlayer.Source = $VideoSource;
$VideoPlayer.Play()
 
 
#Show Up the Window 
$Window.ShowDialog() | out-null
0 Answers
Related