Click here to Skip to main content
13,254,373 members (55,874 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.2K views
132 downloads
3 bookmarked
Posted 27 Apr 2017

Get Target Screen Size of the Hosting Window in WPF

, 27 Apr 2017
Rate this:
Please Sign up or sign in to vote.
Retrieves height and width of the screen/monitor which is hosting the WPF window or windows control.

Introduction

The sample application provides a way to retrieve the size (width and height) of the given screen or monitor on which the WPF window is hosing.

It mainly applies when multiple monitors are connected and user wants to control the rendering of the content based on the screen resolution on which the window is hosting.

Background

I had been trying in several ways to find out the hosting screen size of the WPF window to determine whether to render the content or not based on the screen resolution. As part of my experience, I have come to a situation that there are some WPF controls which are not able to render if the resolution of the screen is higher than 2k. So, I need to detect the hosting screen size and stop rendering these controls if it is more than 2K resolution.

Using the Code

Currently, there are no APIs provided by the .NET Framework to get the screen size of hosting window. There is a System.Windows.Forms.Screen class which provides the screen bounds, but it doesn't provide the proper data when dpi (Dots per Inch) settings are modified.

In general, the dpi settings are set to 100% by default, but user can modify them to increase the dpi settings like 125%, 150%, etc.

In my sample application, there is a comboBox which is bind to System.WIndows.Forms.Screen.AllScreen. Whenever selection is changed, the text controls show the corresponding values for the selected screen.

private void ComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
  var monitorInfo = (Screen) this.comboBox.SelectedItem;
  this.txtHeight.Text = monitorInfo.WorkingArea.Height.ToString();
  this.txtWidth.Text = monitorInfo.WorkingArea.Width.ToString();
  this.txtHeight_Copy.Text = monitorInfo.Bounds.Height.ToString();
  this.txtWidth_Copy.Text = monitorInfo.Bounds.Width.ToString();

  // below code gets the screen size using the NativeMethods:
  var size = GetHostingScreenSize(monitorInfo.DeviceName);
  this.txtHeightNative.Text = size.Height.ToString();
  this.txtWidthNative.Text = size.Width.ToString();
}

private System.Windows.Size GetHostingScreenSize(string devicename)
{
  var hdc = NativeMethods.CreateDC(devicename, "", "", IntPtr.Zero);
  int DESKTOPVERTRES = NativeMethods.GetDeviceCaps(hdc, (int)DeviceCap.DESKTOPVERTRES);
  int DESKTOPHORZRES = NativeMethods.GetDeviceCaps(hdc, (int)DeviceCap.DESKTOPHORZRES);
}

Refer to the screenshot below for more reference.

Points of Interest

Initially, I tried using the Screen.Bounds properties to get the size of the monitor/screen. But this is not showing the modified values when screen resolution is modified. Then, I have used the GetDeviceCaps() native method to get actual width and height of the screen with respect to modified screen resolution.

Refer to more details about GetDeviceCaps() method at the below links:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ramachandraiah Pandula
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
sipatel200011-Jul-17 15:25
membersipatel200011-Jul-17 15:25 
GeneralMy vote of 4 Pin
KGustafson28-Apr-17 12:53
professionalKGustafson28-Apr-17 12:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 27 Apr 2017
Article Copyright 2017 by Ramachandraiah Pandula
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid