Reputation: 538
I am working on a fully custom window control and to handle the maximization of it, I have overrided the WindowProc function and handle myself the WM_GETMINMAXINFO message.
I fill and send the MINMAXINFO structure to the window handle and it's done.
It work nicely for my main monitor but on my second monitor the windows is too big ..
What I don't understand is that the MINMAXINFO structure I am sending when maximizing on the second monitor has the correct size information (1920x1080), but once maximized the windows end with a size of 2160x1100..
Anyone have an idea of what's happening ?
Thank a lot.
Upvotes: 2
Views: 2460
Reputation: 6094
It sounds like you're providing the size based on the size of the second monitor. The documentation for WM_GETMINMAXINFO states that the width/height are expressed based on the size of the primary monitor even if the window will be displayed in a different monitor with a different resolution.
Upvotes: 0
Reputation: 3657
@Karnalta - I was doing something similar a while back. Have a look at an old blog post of mine. I account for the taskbar and auto-hiding of the taskbar in the downloadable sample at the link above. Both of my monitors are the same size so it seems to work perfectly for me but give it a try. Here are the two methods you will probably be most interested in.
private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
{
MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
IntPtr monitorContainingApplication = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (monitorContainingApplication != System.IntPtr.Zero)
{
MONITORINFO monitorInfo = new MONITORINFO();
GetMonitorInfo(monitorContainingApplication, monitorInfo);
RECT rcWorkArea = monitorInfo.rcWork;
RECT rcMonitorArea = monitorInfo.rcMonitor;
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
mmi.ptMaxTrackSize.x = mmi.ptMaxSize.x; //maximum drag X size for the window
mmi.ptMaxTrackSize.y = mmi.ptMaxSize.y; //maximum drag Y size for the window
mmi.ptMinTrackSize.x = 800; //minimum drag X size for the window
mmi.ptMinTrackSize.y = 600; //minimum drag Y size for the window
mmi = AdjustWorkingAreaForAutoHide(monitorContainingApplication, mmi); //need to adjust sizing if taskbar is set to autohide
}
Marshal.StructureToPtr(mmi, lParam, true);
}
private static MINMAXINFO AdjustWorkingAreaForAutoHide(IntPtr monitorContainingApplication, MINMAXINFO mmi)
{
IntPtr hwnd = FindWindow("Shell_TrayWnd", null);
if (hwnd == null) return mmi;
IntPtr monitorWithTaskbarOnIt = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (!monitorContainingApplication.Equals(monitorWithTaskbarOnIt)) return mmi;
APPBARDATA abd = new APPBARDATA();
abd.cbSize = Marshal.SizeOf(abd);
abd.hWnd = hwnd;
SHAppBarMessage((int)ABMsg.ABM_GETTASKBARPOS, ref abd);
int uEdge = GetEdge(abd.rc);
bool autoHide = System.Convert.ToBoolean(SHAppBarMessage((int)ABMsg.ABM_GETSTATE, ref abd));
if (!autoHide) return mmi;
switch (uEdge)
{
case (int)ABEdge.ABE_LEFT:
mmi.ptMaxPosition.x += 2;
mmi.ptMaxTrackSize.x -= 2;
mmi.ptMaxSize.x -= 2;
break;
case (int)ABEdge.ABE_RIGHT:
mmi.ptMaxSize.x -= 2;
mmi.ptMaxTrackSize.x -= 2;
break;
case (int)ABEdge.ABE_TOP:
mmi.ptMaxPosition.y += 2;
mmi.ptMaxTrackSize.y -= 2;
mmi.ptMaxSize.y -= 2;
break;
case (int)ABEdge.ABE_BOTTOM:
mmi.ptMaxSize.y -= 2;
mmi.ptMaxTrackSize.y -= 2;
break;
default:
return mmi;
}
return mmi;
}
Upvotes: 1
Reputation: 8319
Instead of using the win32 events, it would probably be easier to let WPF maximize the window himself (with WindowState = System.Windows.WindowState.Maximized;
) and modify your window content template to fit the size you want.
For example perhaps you can encapsulate your window content in a Grid and set margins to this Grid. Eventually, then move the background property of the window (if any) to the grid and set a transparent color for the window background.
Upvotes: 0