tags:

views:

2719

answers:

4

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

Clairification: The window in question is WPF, not WinForms.

+7  A: 

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);
  }
}
Jeff Yates
Perhaps my tagging didn’t make it clear that I am using WPF windows, not WinForms. I do not have the System.Windows.Forms.dll referenced, and it wouldn’t work anyway as WPF has its own inheritance tree.
chilltemp
Screen.FromHandle... did excactly what I needed. Thanks
chilltemp
You're welcome. My apologies for not getting straight to the answer - I had to investigate what was available in WPF before I updated my post.
Jeff Yates
This works to put a window on the right-hand edge: var bounds = this.GetScreen().WorkingArea;this.Left = bounds.Right - this.Width;But it requires references to System.Windows.Forms and System.Drawing, which is not ideal.
Anthony
Why is that not ideal? Those assemblies are likely to be present when WPF is present as they are .NET 2.0 and below.
Jeff Yates
+1 Thanks for this! Yeah I've found it pretty impossible to get by without System.Windows.Forms in my WPF app--MS just didn't include enough of the same functionality in WPF.
chaiguy
+3  A: 

Add on to ffpf

Screen.FromControl(this).Bounds
faulty
Thanks, I added this into my example.
Jeff Yates
+5  A: 

You can use this:

System.Windows.SystemParameters.WorkArea

This is also usefull:

System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
Pyttroll
+2  A: 

Also you may need:

SystemParameters.VirtualScreenWidth
SystemParameters.VirtualScreenHeight

to get the combined size of all monitors and not one in particular.

742