在Windows WPF中垂直最大化

本文关键字:垂直 最大化 WPF Windows | 更新日期: 2023-09-27 18:15:34

我正在做一个WPF应用程序,我在窗口周围添加了一个清晰的边框,这样它就可以从主窗口外调整大小。我已经重写了MINMAXINFO,如下所示。使用下面的代码,当我进行常规最大化时,你看不到不可见的边界。然而,当我尝试垂直最大化(通过将窗口顶部拖动到屏幕顶部)时,显示了不可见的边界。我试着捕捉所有的信息,但我找不到垂直最大化的单独信息。如何在这个案例中移除隐形边界?

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);
}

在Windows WPF中垂直最大化

这条消息有点大,所以我把它贴在这里。

这里是一些关于AeroSnap的有用信息:在WndProc中处理AeroSnap消息这个问题解释了垂直最大化和其他类似功能(Win+Left, Win+Right等)没有单独的消息。但是,您仍然可以分析其他消息(WM_SIZE, WM_WINDOWPOSCHANGING),以过滤掉最大化,同时发现位置和大小的变化,这可能意味着使用了AeroSnap。然后您就可以调整窗口大小了。

这是你如何修改你的代码来开始分析其他消息:

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);
}

这个代码不处理Win+Right对接,并没有过滤掉最大化和最小化,但我相信这是一个很好的起点。如果需要,可以用同样的方法添加WM_SIZE

你也可以尝试禁用AeroSnap: How do You disable AeroSnap in a application .

我最后不得不在窗口周围添加一个不可见的边框。当高度或宽度等于屏幕的高度或宽度时,我将不可见边界的高度和/或宽度设为0。如果不是,那么我将恢复到默认的厚度