I want to know the screen resolution so that I can set the height of an element according to the resolution in a Windows 8 app.
5 Answers
How about this?
var bounds = Window.Current.Bounds;
double height = bounds.Height;
double width = bounds.Width;

- 2,997
- 1
- 23
- 31
-
4Use this. Don't assume you are in the primary screen as done in Md's answer – Robert Levy Jun 01 '12 at 04:16
-
10This doesn't get the screen size, only your window size. If you are running snapped it will be incorrect. If all you need is the window size this will work. – Jon Tackabury Sep 10 '12 at 19:59
-
it is now a double, not an int. – balint Sep 22 '12 at 01:51
-
does anyone know how to do this in c++/cx? using the suggested way gives the error "Bounds is not a member of Window". – cfischer Nov 16 '12 at 13:18
-
It gives current size of the app windows not the system, if may produce false results when screen is in snapped view – Jayant Varshney Mar 05 '13 at 11:00
-
3This only gives you the app Window Size, NOT the screen resolution. – Josue Yeray Aug 20 '13 at 08:26
Getting the bounds of current window is easy. But say if you want to set a large font size for bigger screen (resolution is same as 10" device, but screen is 27"), this wont help. Refer Scaling to different screens I used the following method to detect screen size & change text block font style appropriately.
void detectScreenType()
{
double dpi = DisplayProperties.LogicalDpi;
var bounds = Window.Current.Bounds;
double h;
switch (ApplicationView.Value)
{
case ApplicationViewState.Filled:
h = bounds.Height;
break;
case ApplicationViewState.FullScreenLandscape:
h = bounds.Height;
break;
case ApplicationViewState.Snapped:
h = bounds.Height;
break;
case ApplicationViewState.FullScreenPortrait:
h = bounds.Width;
break;
default:
return;
}
double inches = h / dpi ;
string screenType = "Slate";
if (inches < 10)
{
screenType = "Slate";
} else if (inches < 14) {
screenType = "WorkHorsePC";
}
else
{
screenType = "FamilyHub";
}
ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["screenType"] = screenType;
}

- 1,380
- 13
- 21
-
This gives size of the window, not full screen size, unless running in full-screen mode. – cyanide Sep 30 '15 at 04:21
Probably the best option for DirectX-enabled apps, however, applicable to all other kinds of metro apps is:
P.S. C'mon, getting window size to determine screen resolution? What about snapped/filled modes? This world is so much broken :-/

- 2,483
- 2
- 23
- 35
-
-
And thanks. Anyways, I made it to work without setting the height. :) – a_rahmanshah Aug 15 '12 at 14:40
apparently i don't have enough rep to reply to posts yet, but in regards to @Krishna's answer, it may be worth noting that his solution requires:
using Windows.UI.Xaml;
probably not an issue for most of you, but in my case (trying to grab resolution of executing app from an imported library), it wasn't there by default.
hope this helps someone else...

- 470
- 5
- 7
Are you using XAML? If so it does not matter. Use the Grid control. It will fill up all available space. Read Jerry's blog as to why you might want to use xaml for WinRT development.

- 12,425
- 1
- 25
- 41
-
I have two rows, one with height 70 and another with *. I have a textBox which is in the second row. I want to the textbox to change it's height according to resolution. – a_rahmanshah May 31 '12 at 06:50
-
1by default a TextBox style will fill the entire area it is given. This is the Silverlight style, but should still apply, or you can assign this style http://msdn.microsoft.com/en-us/library/cc645061%28v=VS.95%29.aspx – Shawn Kendrot May 31 '12 at 07:35