29

I want to set the width & height of a Window dynamically based on the user screens maximum width/height. How can I determine this programmatically?

Sae1962
  • 1,122
  • 15
  • 31
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176

6 Answers6

42

For the primary screen:

System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight

(Note that there are also some other primary screen related properties which depend on various factors, Full* & Maximised*)

Virtual screen:

SystemParameters.VirtualScreenWidth
SystemParameters.VirtualScreenHeight
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 1
    What about based on current resolution? How to get that? – Shamim Hafiz - MSFT Jun 08 '11 at 11:21
  • 2
    I would think this does use the current resolution, have you tried it? – H.B. Jun 08 '11 at 11:22
  • Finally an answer to this question! Does VirtualScreenWidth[Height] not get updated when the user changes scaling while the application open? On Win10 it looks like you have to restart the application for these values to get repopulated. – Hashman Jul 03 '16 at 00:32
  • @Hashman: Cannot tell you anything about that, don't even have a win 10 setup. – H.B. Jul 04 '16 at 15:29
14

If you want the specific dimensions of the monitor your program is running on (if someone is running more than one monitor) you could also use:

var helper = new WindowInteropHelper(this); //this being the wpf form 
var currentScreen = Screen.FromHandle(helper.Handle);

This will return a screen object referencing the monitor the program is running on. From there you can use the currentScreen.Bounds.Width / Height property (for the full size) or the currentScreen.WorkingArea.Width / Height (minus task bar, etc.) depending on what you want.

Alex M
  • 2,756
  • 7
  • 29
  • 35
Dennis Morgan
  • 141
  • 1
  • 2
9

use Screen Object

Screen.PrimaryScreen.Bounds.Width
DeveloperX
  • 4,633
  • 17
  • 22
2

I couldn't use any of the solutions above under .NET 4.0.30319.42000 with Windows 10 Enterprise when calling it from the Ranorex Studio 8.0.1+git.8a3e1a6f, so I used the line

using WinForms = System.Windows.Forms;
[…]
                SetWindowPos(processes[0].MainWindowHandle,
                    0,
                    y,
                    x,
                    WinForms.SystemInformation.PrimaryMonitorSize.Width,
                    WinForms.SystemInformation.PrimaryMonitorSize.Height,
                    SWP.SHOWWINDOW);
Sae1962
  • 1,122
  • 15
  • 31
  • Thanks buddy, I'm using Ranorex too and was trying to adapt the other answers but you've hit the nail on the head... – Ewan Sep 15 '22 at 11:12
2

You can use the SizeChanged event

SizeChanged="MyWindow_SizeChanged"

Then in your event handler,

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (this.MinWidth > 0 && this.MinHeight > 0)
    {
        double heightScaleFactor = e.NewSize.Height / this.MinHeight;
        double widthScaleFactor = e.NewSize.Width / this.MinWidth;            
        mainGrid.LayoutTransform = new ScaleTransform(heightScaleFactor, widthScaleFactor);
    }
}

where MainGrid is a container for all the contents in MyWindow.

Komal12
  • 3,340
  • 4
  • 16
  • 25
lewi
  • 477
  • 1
  • 5
  • 18
0

You can get the screen height and width:

int height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
int width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;

Then set the Window's Height and Width properties to those in the Initialization.

this.Height = height;
this.Width = width;

Works to get the screen's height and width in WinForms or in ASP .NET. No muss, no fuss, except you'll need to reference the System.Windows.Forms assembly in your project if it's not a WinForm project.

vapcguy
  • 7,097
  • 1
  • 56
  • 52