CareX
CareX

Reputation: 3

WPF - Incorrect Window resolution after setting Window.Windowstate to Maximized programmatically

So I'm trying to open a maximized Window on my secondary monitor. It all works fine, except it's height is 2 pixels lower than it should be.

My secondary monitor has a resolution of 1280x1024. However once I inspect the window, it's 1280x1022. Does anyone know the problem?

OS: Windows 10

Actual height/width image (Puush)

Here is some code:

SecondaryMonitorWindow smw = new SecondaryMonitorWindow();
smw.Show();

XAML

Loaded="OnWindowLoaded"
WindowStartupLocation="Manual"
WindowStyle="None"
SnapsToDevicePixels="True"
Height="1280" 
Width="1024"

Constructor

public SecondaryMonitorWindow()
{
    InitializeComponent();
    Instance = this;
}

Event

private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    this.MaximizeToSecondaryMonitor();
}

ExtensionMethod

public static void MaximizeToSecondaryMonitor(this Window window)
{
    var secondaryScreen = Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();

    if (secondaryScreen != null)
    {
        if (!window.IsLoaded)
            window.WindowStartupLocation = WindowStartupLocation.Manual;

        window.Left = secondaryScreen.WorkingArea.Left;
        window.Top = secondaryScreen.WorkingArea.Top;
        window.Width = secondaryScreen.WorkingArea.Width;
        window.Height = secondaryScreen.WorkingArea.Height;

        if (window.IsLoaded)
            window.WindowState = WindowState.Maximized;
    }
}

Upvotes: 0

Views: 1124

Answers (1)

Kylo Ren
Kylo Ren

Reputation: 8823

Try setting the parameter grammatically, this way only you'll be able to achieve the perfect size :

 public MainWindow()
    {            
        InitializeComponent();
        this.Height = SystemParameters.WorkArea.Height;
        this.Width = SystemParameters.WorkArea.Width;
    }

however there are bugs around Window 10. if you are using please let me know I'll update the answer.

For Window 10 this post will help. there are no design Border in window 10 so there is some pixel problem. I'm not sure what is Window decoration settings on your OS, but try this.

this.Height = SystemParameters.WorkArea.Height +  SystemParameters.FixedFrameHorizontalBorderHeight;
this.Width = SystemParameters.WorkArea.Width + SystemParameters.FixedFrameVerticalBorderWidth;

If there were some other borders are removed try ResizeFrame border size addition too.

Upvotes: 0

Related Questions