Detect the display mode of your UWP Window

Detect the display mode of your UWP Window

It all started with a request from another UWP developer: “How can I detect if a window is maximized?” The question really took me by surprise and piqued my curiosity, especially since it was coming from a talented UWP developer.

First I checked docs.microsoft.com, but to my dismay, nothing, then StackOverflow: nothing, then Google, Bing… no results.

This may surprise you, but UWP doesn’t provide an API to detect if a window is maximized.

So how can we detect it?

UWP provides 3 pieces of information we can use to “guess” if a window is maximized:

  • ApplicationView.IsFullScreen
  • ApplicationView.IsFullScreenMode
  • UIViewSettings.UserInteractionMode

Note: ApplicationView.IsFullScreen is marked as deprecated (and the documentation suggests to use IsFullScreenMode instead) but IsFullScreen provides some valuable information that other API do not.

Here is what we get if we test all the different display modes on Windows 10 Desktop:

Normal:
IsFullScreen = False
IsFullScreenMode = False
UserInteractionMode = Mouse

Maximized:
IsFullScreen = True
IsFullScreenMode = False
UserInteractionMode = Mouse

Tablet Mode:
IsFullScreen = True
IsFullScreenMode = False
UserInteractionMode = Touch

Full screen (Win+Shift+Enter):
IsFullScreen = True
IsFullScreenMode = True
UserInteractionMode = Mouse/Touch (it depends if tablet mode is activated).

Based on these findings, we can conclude that the best way to detect if a window is maximized is to write:

ApplicationView applicationView = ApplicationView.GetForCurrentView();
var isMaximized = AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop" && applicationView.IsFullScreen && !applicationView.IsFullScreenMode && UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Mouse

Note: this code ignores Mixed Reality headsets, a more advanced version is available below.

Helper class to detect the display mode of the current windows

I didn’t want to provide a single line of code as an answer, so instead I developed a small library to not only detect if the window is maximized but also all other modes:

  • Windowed
  • Maximized
  • FullScreen (Win+Shift+Enter)
  • FullScreenTabletMode (Windows 10 tablet mode, the task back stay visible contrary to the previous mode)
  • SnappedLeft
  • SnappedRight
  • CompactOverlay

This helper class supports all types of devices:

  • Desktop (including Mixed Reality Headsets and Kiosk mode)
  • Mobile (including Continuum)
  • Hololens
  • Xbox
  • Surface Hub
  • IOT

Full code available here:

https://github.com/rudyhuyn/blog-samples/blob/master/DetectWindowDisplayMode/Huyn.WindowDisplayMode/WindowDisplayInfo.cs

To detect which display mode your window uses, import the 2 .cs files in your project, or import the library Huyn.WindowDisplayMode and use the following code:

Huyn.WindowDisplayInfo.GetForCurrentView().ToString()

The code+sample is available here:

https://github.com/rudyhuyn/blog-samples/tree/master/DetectWindowDisplayMode

Comments are closed.