Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/WpfScreenHelper/Enum/WindowPositions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public enum WindowPositions
TopRight,
BottomRight,
BottomLeft,
Maximize
Fill
}
}
4 changes: 3 additions & 1 deletion src/WpfScreenHelper/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static class WindowHelper
/// <param name="height">New height of the window.</param>
public static void SetWindowPosition(this Window window, int x, int y, int width, int height)
{
//Force to normal to allow setting the window position
window.WindowState = WindowState.Normal;
// The first move puts it on the correct monitor, which triggers WM_DPICHANGED
// The +1/-1 coerces WPF to update Window.Top/Left/Width/Height in the second move
NativeMethods.MoveWindow(new WindowInteropHelper(window).Handle, x - 1, y, width + 1, height, false);
Expand Down Expand Up @@ -135,7 +137,7 @@ private static Rect CalculateWindowCoordinates(FrameworkElement window, WindowPo
return new Rect(screen.WpfBounds.X * screen.ScaleFactor, y * screen.ScaleFactor, window.Width * screen.ScaleFactor, window.Height * screen.ScaleFactor);
}

case WindowPositions.Maximize:
case WindowPositions.Fill:
return new Rect(screen.WpfBounds.X * screen.ScaleFactor, screen.WpfBounds.Y * screen.ScaleFactor, screen.WpfBounds.Width * screen.ScaleFactor, screen.WpfBounds.Height * screen.ScaleFactor);

default:
Expand Down
5 changes: 3 additions & 2 deletions src/WpfScreenHelper/WpfScreenHelper.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<TargetFrameworks>net40;net6.0-windows;net8.0-windows</TargetFrameworks>
Expand All @@ -14,7 +14,8 @@
<PackageTags>WPF Screen Monitor Display Helper</PackageTags>
<RepositoryUrl>https://github.com/micdenny/WpfScreenHelper</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>2.1.1</Version>
<Version>2.2.1</Version>
<FileVersion>2025.2.18.3</FileVersion>
</PropertyGroup>

<Target Name="CopyPackage" AfterTargets="Pack">
Expand Down
120 changes: 100 additions & 20 deletions test/WpfScreenHelper.DpiTestWpfApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,111 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Name="Wnd"
Height="400" Width="700" SnapsToDevicePixels="True" WindowStyle="None" ResizeMode="NoResize"
Loaded="MainWindow_OnLoaded" MouseLeftButtonDown="MainWindow_OnMouseLeftButtonDown">
Title="MainWindow"
Name="Wnd"
Height="400"
Width="700"
SnapsToDevicePixels="True"
WindowStyle="None"
ResizeMode="NoResize"
Loaded="MainWindow_OnLoaded"
MouseLeftButtonDown="MainWindow_OnMouseLeftButtonDown">
<Grid>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Width="100" Height="50" Content="Position Maximize" Margin="5" Click="ButtonMaximize_OnClick"/>
<Button Width="100" Height="50" Content="Position Center" Margin="5" Click="ButtonCenter_OnClick"/>
<StackPanel Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center">

<Button Width="210"
Height="50"
Content="Position Center"
Margin="5"
Click="ButtonCenter_OnClick" />

<ComboBox Name="Monitors"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Width="210" />

<StackPanel Orientation="Horizontal">
<Button Width="100"
Height="50"
Content="Fill to Screen"
Margin="5"
Click="ButtonFillToScreen_OnClick" />
<Button Width="100"
Height="50"
Content="origine Size"
Margin="5"
Click="ButtonOrigine_OnClick" />
</StackPanel>
</StackPanel>

<Button Width="200" Height="50" Content="Position Left" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Click="ButtonLeft_OnClick"/>
<Button Width="200" Height="50" Content="Position Right" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Center" Click="ButtonRight_OnClick"/>
<Button Width="200" Height="50" Content="Position Top" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Top" Click="ButtonTop_OnClick"/>
<Button Width="200" Height="50" Content="Position Bottom" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Bottom" Click="ButtonBottom_OnClick"/>
<Button Width="200" Height="50" Content="Position TopLeft" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top" Click="ButtonTopLeft_OnClick"/>
<Button Width="200" Height="50" Content="Position TopRight" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top" Click="ButtonTopRight_OnClick"/>
<Button Width="200" Height="50" Content="Position BottomRight" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="ButtonBottomRight_OnClick"/>
<Button Width="200" Height="50" Content="Position BottomLeft" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="ButtonBottomLeft_OnClick"/>
<Button Width="200"
Height="50"
Content="Position Left"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Click="ButtonLeft_OnClick" />
<Button Width="200"
Height="50"
Content="Position Right"
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Click="ButtonRight_OnClick" />
<Button Width="200"
Height="50"
Content="Position Top"
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Click="ButtonTop_OnClick" />
<Button Width="200"
Height="50"
Content="Position Bottom"
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Click="ButtonBottom_OnClick" />
<Button Width="200"
Height="50"
Content="Position TopLeft"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="ButtonTopLeft_OnClick" />
<Button Width="200"
Height="50"
Content="Position TopRight"
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Click="ButtonTopRight_OnClick" />
<Button Width="200"
Height="50"
Content="Position BottomRight"
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Click="ButtonBottomRight_OnClick" />
<Button Width="200"
Height="50"
Content="Position BottomLeft"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Click="ButtonBottomLeft_OnClick" />

<StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5,0,0,70">
<TextBlock Text="{Binding ElementName=Wnd, Path=Width, StringFormat='Width: {0}'}"/>
<TextBlock Text="{Binding ElementName=Wnd, Path=Height, StringFormat='Height: {0}'}"/>
<TextBlock Text="{Binding ElementName=Wnd, Path=Left, StringFormat='Left: {0}'}"/>
<TextBlock Text="{Binding ElementName=Wnd, Path=Top, StringFormat='Top: {0}'}"/>
<StackPanel HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Margin="5,0,0,70">
<TextBlock Text="{Binding ElementName=Wnd, Path=Width, StringFormat='Width: {0}'}" />
<TextBlock Text="{Binding ElementName=Wnd, Path=Height, StringFormat='Height: {0}'}" />
<TextBlock Text="{Binding ElementName=Wnd, Path=Left, StringFormat='Left: {0}'}" />
<TextBlock Text="{Binding ElementName=Wnd, Path=Top, StringFormat='Top: {0}'}" />
</StackPanel>

<ComboBox Name="Monitors" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="5"/>

</Grid>
</Window>
12 changes: 10 additions & 2 deletions test/WpfScreenHelper.DpiTestWpfApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
Monitors.SelectedIndex = 0;
}

private void ButtonMaximize_OnClick(object sender, RoutedEventArgs e)
private void ButtonFillToScreen_OnClick(object sender, RoutedEventArgs e)
{
this.SetWindowPosition(WindowPositions.Maximize, Screen.AllScreens.ElementAt(Monitors.SelectedIndex));
this.SetWindowPosition(WindowPositions.Fill, Screen.AllScreens.ElementAt(Monitors.SelectedIndex));
}

private void ButtonCenter_OnClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -79,5 +79,13 @@ private void MainWindow_OnMouseLeftButtonDown(object sender, MouseButtonEventArg
{
DragMove();
}

private void ButtonOrigine_OnClick(object sender, RoutedEventArgs e)
{
this.Height = 400;
this.Width = 700;
this.SetWindowPosition(WindowPositions.Center, Screen.AllScreens.ElementAt(Monitors.SelectedIndex));

}
}
}