4

I'm working on a WPF application and I've added a clear border around the window so that it can be resized from outside the main window. I've overridden MINMAXINFO as shown here. With the below code, when I do a regular maximize you can't see the invisible border. However, when I try to maximize vertically (by dragging the top of the window to the top of the screen) the invisible border is shown. I've tried catching all the messages and I couldn't find a separate message for vertical maximization. How can I remove the invisible border for this case?

private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam) {
    MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

    // Adjust the maximized size and position to fit the work area of the correct monitor
    int MONITOR_DEFAULTTONEAREST = 0x00000002;
    System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

    if (monitor != System.IntPtr.Zero) {
        MONITORINFO monitorInfo = new MONITORINFO();
        GetMonitorInfo(monitor, monitorInfo);
        RECT rcWorkArea = monitorInfo.rcWork;
        RECT rcMonitorArea = monitorInfo.rcMonitor;
        mmi.ptMaxPosition.x = Math.Abs (rcWorkArea.left - rcMonitorArea.left) - borderThickness;
        mmi.ptMaxPosition.y = Math.Abs (rcWorkArea.top - rcMonitorArea.top) - borderThickness;
        mmi.ptMaxSize.x = Math.Abs (rcWorkArea.right - rcWorkArea.left) + 2 * borderThickness;
        mmi.ptMaxSize.y = Math.Abs (rcWorkArea.bottom - rcWorkArea.top) + 2 * borderThickness;
    }

    Marshal.StructureToPtr(mmi, lParam, true);
}
Dehli
  • 5,950
  • 5
  • 29
  • 44
  • Its been more than a couple of years since I worked in WPF I don't remember much. However I think that piece of code works if you set the border to none. – NVM Jul 31 '14 at 05:29

2 Answers2

0

This message is a bit large for comment so I post it here.

Here is some useful info about AeroSnap: Handling AeroSnap message in WndProc This question explains that there is no separate message for vertical maximization and other similar features (Win+Left, Win+Right, etc). But you can still analyze other messages (WM_SIZE, WM_WINDOWPOSCHANGING) to filter out maximization and find changes of position and size at the same time which can mean usage of AeroSnap. You will be able to adjust window size then.

This is how you can modify your code to start analyzing other messages:

public const int WM_WINDOWPOSCHANGING = 0x0046;
public const int WM_SIZE = 0x0005;

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
    public IntPtr hwnd;
    public IntPtr hwndInsertAfter;
    public int x;
    public int y;
    public int cx;
    public int cy;
    public int flags;
}

...

switch (msg)
{
    case 0x0024: /* WM_GETMINMAXINFO */
        WmGetMinMaxInfo(hwnd, lParam);
        handled = true;
        break;
    case WM_WINDOWPOSCHANGING:
        _counter++;
        WmWindowPosChanging(lParam);
        break;
}


private static void WmWindowPosChanging(System.IntPtr lparam)
{
    WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lparam, typeof(WINDOWPOS));

    System.Diagnostics.Trace.WriteLine(string.Format("x: {0}, y: {1}, cx: {2}, cy: {3}, flags: {4:X}", pos.x, pos.y, pos.cx, pos.cy, pos.flags));
    if (pos.x == 0)
    {
        pos.x = -thickness;
        pos.cx += 2*thickness;
    }

    if (pos.y == 0)
    {
        pos.y = -thickness;
        pos.cy += 2*thickness;
    }

    Marshal.StructureToPtr(pos, lparam, true);
}

This code doesn't handle Win+Right docking and doesn't filter out Maximization and Minimization, but I believe that it's a good start point. WM_SIZE can be added the same way if needed.

You can also try to disable AeroSnap: How do you disable Aero Snap in an application?.

Community
  • 1
  • 1
sasha_gud
  • 1,635
  • 13
  • 18
0

What I ended up having to was add an invisible border around the window. When the height or width was equal to the height or width of the screen, then I'd make the invisible border's height and/or width 0. If it wasn't, then I'd revert to the default thickness.

Dehli
  • 5,950
  • 5
  • 29
  • 44