0

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;
    }
}
CareX
  • 3
  • 7
  • which OS are you using? – Kylo Ren May 09 '16 at 12:33
  • @KyloRen Added it to the description, but I'm using Windows 10 atm. – CareX May 09 '16 at 12:37
  • did it help? or still not getting your desired output? – Kylo Ren May 10 '16 at 08:34
  • @KyloRen In your [linked post](http://stackoverflow.com/questions/35529885/how-to-create-wpf-window-with-exact-pixel-size/35529943#35529943) they said that once he allowed transparency, it worked. So I did add that to the XAML, and then it worked fine. I also added 'window.WindowState = WindowState.Normal;' in the extension method. To ensure it isn't set to maximized in the XAML (which would still force open it in the primary monitor). However, when I applied your original fix, it still calculated the height with 2 pixels off. But in some sense you've helped me anyway! Thanks! – CareX May 10 '16 at 09:10
  • but are you ok with Window.Style = none? – Kylo Ren May 10 '16 at 09:58
  • @KyloRen My WindowStyle is set to None yea. But I was referring to WindowState tho. (Maximized/Minimized/Normal) – CareX May 10 '16 at 12:29
  • I'm skeptical about the results with mentioned settings but if you are saying then probably you have achieved it. – Kylo Ren May 10 '16 at 13:00

1 Answers1

0

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.

Community
  • 1
  • 1
Kylo Ren
  • 8,551
  • 6
  • 41
  • 66