1
votes

I have a WPF window which runs on multiple monitors having different resolutions. It'll be good but not required to be smart enough to change max height when I move the window from high-resolution monitor to low-resolution monitor or vice-versa.

Current requirement is simple enough to set the max height of my modal window based on my current monitor's height.

I have tested a few things like

Screen.PrimaryScreen.WorkingArea.Height
Screen.PrimaryScreen.WorkingArea.Width

But it gives the height of only primary screen of system where I need the height of screen where window currently resides.

Also a major concern in multiple monitors is the top property of windows, in high resolution monitors it is fine but in low-resolution or secondary monitors it starts from a different number like 160.

2
In our app we handle the similar problem with resizing of window on monitors with different resolution/dpi via P/Invoke using the following functions bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi); IntPtr MonitorFromWindow(IntPtr handle, int flags); and IntPtr MonitorFromPoint(POINT point, int flags); from user32.dllPavel Anikhouski
@PavelAnikhouski Can you share the sample code along with the references used, I am having a hard time locating MONITORINFO structMegaMind
@MegaMind: Did you see this?mm8
I created a helper class to get screen parameters which you can use to adjust your window size. Look at this SO answer here: Screen ParametersJeff

2 Answers

1
votes

You can use the Win32 API MonitorFromWindow or Forms API Screen.FromHandle to determine the area of the monitor your window is currently on.

In the two examples below, I set the max height to be 50% of the working area height of the monitor/screen the window is currently on. The two examples below are in methods on a System.Windows.Window sub-class (so this refers to a Window).

The full example source code is here.

Win32

var window = new System.Windows.Interop.WindowInteropHelper(this);
IntPtr hWnd = window.Handle;
var screen = System.Windows.Forms.Screen.FromHandle(hWnd);
MaxHeight = 0.5 * screen.WorkingArea.Height;

Forms

var window = new System.Windows.Interop.WindowInteropHelper(this);
IntPtr hWnd = window.Handle;
IntPtr hMonitor = Win32.MonitorFromWindow(hWnd, Win32.MONITOR_DEFAULTTONEAREST);

var monitorInfo = new Win32.MONITORINFOEX();
monitorInfo.cbSize = (int)Marshal.SizeOf(monitorInfo);
if (Win32.GetMonitorInfo(hMonitor, ref monitorInfo))
{
    MaxHeight = (monitorInfo.rcWork.Bottom - monitorInfo.rcWork.Top) * 0.5;
}

Note that Win32.MonitorFromWindow is a pinvoke of Win32's MonitorFromWindow function.

0
votes

I think the helper class in my test program will provide the values you need for both DPI aware and Non-DPI aware programs: Screen Parameters.

This uses a couple of Win32 API calls to get various screen parameters. It also provides screen scale factors which will allow you to adjust your window sizes based on the screen the window is currently placed.