Click here to Skip to main content
15,887,822 members
Articles / Programming Languages / C#
Tip/Trick

Get Target Screen Size of the Hosting Window in WPF

Rate me:
Please Sign up or sign in to vote.
4.80/5 (6 votes)
27 Apr 2017CPOL2 min read 29.5K   851   5   2
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.

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

Image 1

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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Shailesh Patel 200011-Jul-17 14:25
Shailesh Patel 200011-Jul-17 14:25 
GeneralMy vote of 4 Pin
KGustafson28-Apr-17 11:53
professionalKGustafson28-Apr-17 11: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.