c# - Windows WPF 中的垂直最大化

标签 c# wpf windows

我正在开发一个 WPF 应用程序,我在窗口周围添加了一个清晰的边框,以便可以从主窗口外部调整它的大小。我已经覆盖了 MINMAXINFO如图here .使用下面的代码,当我进行常规最大化时,您看不到不可见的边框。但是,当我尝试垂直最大化(通过将窗口顶部拖动到屏幕顶部)时,会显示不可见的边框。我已经 try catch 所有消息,但找不到用于垂直最大化的单独消息。对于这种情况,我该如何去除不可见的边框?

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

最佳答案

这条消息对于评论来说有点大,所以我把它贴在这里。

这里有一些关于 AeroSnap 的有用信息:Handling AeroSnap message in WndProc这个问题说明垂直最大化和其他类似功能(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:How do you disable Aero Snap in an application? .

关于c# - Windows WPF 中的垂直最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25041416/

相关文章:

windows - 具有多个实例的 Node.js Azure WorkerRole 无法使用同一端口?

c# - 删除 CheckedListBox SelectedIndexChanged 上的动态文本框

c# - 翻页的WPF动画创意

java - 从 java 应用程序导出时,IE6 生成奇怪的工作表名称

c# - 如何在 UWP 应用中以编程方式调暗屏幕

wpf - 基于 LOCBaml 或 Resx 的本地化的最佳本地化方法是什么?

c# - 使用基于 DLR 的语言而不是 C# 来执行脚本任务的原因是什么?

c# - 在扩展 Roles 表后,实体类型 IdentityRole 不是当前上下文模型的一部分

c# - 使用 ANTS Memory Profiler 在 ASP.NET 网站中发现内存泄漏?

wpf - 如何强制关闭 ContextMenu(WPF 项目)?