How to get the resolution of screen? For a WinRT app?

40,393

Solution 1

How about this?

var bounds = Window.Current.Bounds;

double height = bounds.Height;

double width = bounds.Width;

Solution 2

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;
    }

Solution 3

Probably the best option for DirectX-enabled apps, however, applicable to all other kinds of metro apps is:

http://blogs.microsoft.co.il/blogs/tomershamam/archive/2012/07/24/get-screen-resolution-in-windows-8-metro-style-application.aspx

P.S. C'mon, getting window size to determine screen resolution? What about snapped/filled modes? This world is so much broken :-/

Solution 4

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...

Solution 5

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.

Share:
40,393
a_rahmanshah
Author by

a_rahmanshah

Ambitious, curious geek

Updated on April 16, 2020

Comments

  • a_rahmanshah
    a_rahmanshah about 4 years

    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.