如何使用 Window 消息等捕获最大化启动事件

How to catch Maximize started event using Window Message, etc

我能够获取最大化和大小更改事件,但无法启动最大化。

我找到了解决问题的方法。 这是挂钩方法,您可以为您的代码添加并在 Window 最大化事件发生之前完成所有需要的工作人员。

    private static void WmWindowPos(IntPtr hwnd, IntPtr lParam)
    {
        WINDOWPOS wpos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
        IntPtr monitorContainingApplication = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

        if (monitorContainingApplication == IntPtr.Zero)
            return;

        var monitorInfo = new MONITORINFO();
        GetMonitorInfo(monitorContainingApplication, monitorInfo);
        var rcWorkArea = monitorInfo.rcWork;

        if (wpos.x == rcWorkArea.left &&
            wpos.y == rcWorkArea.top &&
            wpos.cx == rcWorkArea.right &&
            wpos.cy == rcWorkArea.bottom)
        {
            // Do required staff before windows maximized state has changed
        }
    }

将此方法添加到 Window像这样的过程

    private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            case 0x0046: //  WM_WINDOWPOSCHANGING
                WmWindowPos(hwnd, lParam);
                break;
        }

        return (IntPtr)0;
    }