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

问题描述 投票:-3回答:1

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

.net wpf winapi maximize
1个回答
0
投票

我找到了解决问题的方法。这是可以为代码添加的挂钩方法,可以在发生Window Maximized事件之前完成所有必要的工作。

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

像这样向WindowProc添加此方法

    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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.