在Windows WPF中垂直最大化

4
我正在开发一个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);
}

我已经有几年没用WPF了,记不太清了。但是我认为如果将边框设置为无,那段代码应该可以工作。 - NVM
2个回答

0

这条信息有点长,不适合在评论区发布,所以我在这里发表。

关于 AeroSnap,以下是一些有用的信息:在 WndProc 中处理 AeroSnap 消息。这个问题解释了垂直最大化和其他类似功能(Win+Left、Win+Right 等)没有单独的消息。但是你仍然可以分析其他消息(WM_SIZEWM_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:如何在应用程序中禁用 Aero Snap?


0
我最终不得不在窗口周围添加一个无形边框。当高度或宽度等于屏幕的高度或宽度时,我会将无形边框的高度和/或宽度设置为0。如果不是,则恢复默认厚度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接