How can I get the active screen dimensions?


Question

What I am looking for is the equivalent of System.Windows.SystemParameters.WorkArea for the monitor that the window is currently on.

Clarification: The window in question is WPF, not WinForm.

1
130
11/30/2015 8:11:38 AM

Accepted Answer

Screen.FromControl, Screen.FromPoint and Screen.FromRectangle should help you with this. For example in WinForms it would be:

class MyForm : Form
{
  public Rectangle GetScreen()
  {
    return Screen.FromControl(this).Bounds;
  }
}

I don't know of an equivalent call for WPF. Therefore, you need to do something like this extension method.

static class ExtensionsForWPF
{
  public static System.Windows.Forms.Screen GetScreen(this Window window)
  {
    return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
  }
}
135
10/31/2008 5:37:49 PM

You can use this to get desktop workspace bounds of the primary screen:

System.Windows.SystemParameters.WorkArea

This is also useful for getting just the size of the primary screen:

System.Windows.SystemParameters.PrimaryScreenWidth System.Windows.SystemParameters.PrimaryScreenHeight


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon