Get screen resolution in Windows 8 Metro style application

July 24, 2012

Today, someone asked an interesting question in the Israeli Metro forum I’m moderating.

The question was: “How can I know what’s the screen resolution in a Metro style app?”

This is a tricky answer, since:

  1. This capability has been removed from the RC.
  2. We can’t rely on current window Width and Height, since it may not be fully opened (snapping).

Well, searching a bit over the API’s, I didn’t find anything that can help, but, we can always use DirectX.

Creating a C++ Metro style application, here’s a code snippet demonstrating how to extract the screen resolution of a given display device:

image

 

Using DirectX, we can query for the IDXGIAdapter, then make a call the EnumOutputs, which provides information for output devices. From there, we can make a call for IDXGIOutput::GetDesc to get the device description. From there we can extract the screen resolution.

Now, to be able getting this value from a C# or JS application, we should create a simple WinRT object, placing it in a WinRT dll, then use it from other projects.

To create a WinRT object with C++, we should create a class which looks like this:

image

 

Feel free to download the code (MetroScreenRes.zip) for both the WinRT object and a C# tester.

Running the application, you can click on the Button and see your screen resolution. Changing the resolution and click the button again, you may see it updated.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

5 comments

  1. meeraOctober 18, 2012 ב 08:37

    How to get screen size in inches for windows 8 app? Please do reply.

    Reply
  2. meeraOctober 18, 2012 ב 08:37

    How to get screen size in inches for windows 8 app? Please do reply.

    Reply
  3. Emmanuel HunaFebruary 21, 2013 ב 01:20

    Since the C++ project needs Win32, the TestApp can only be compiled in x86. Does this mean that if we use this that the app cannot work on ARM, so it won’t work on the Surface RT?

    Reply
  4. Tomer ShamamApril 24, 2013 ב 01:42

    No. It supposed to work with all builds.

    Reply