使用透明边框最大化窗口

时间:2014-07-31 15:32:22

标签: c# wpf windows

我试图最大化一个有透明边框的窗口。最大化时,透明边框不应显示。我按照here找到的方法,并使用下面的代码,我可以让它在中途工作。

void win_SourceInitialized(object sender, EventArgs e) {
    System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
    WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
}

private static System.IntPtr WindowProc(System.IntPtr hwnd, int msg,
          System.IntPtr wParam, System.IntPtr lParam, ref bool handled) {
    switch (msg) {
        case 0x0024:/* WM_GETMINMAXINFO */
            WmGetMinMaxInfo(hwnd, lParam);
            handled = true;
            break;
    }
    return (System.IntPtr)0;
}

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) - thickness;
        mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top) - thickness;
        mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left) + 2 * thickness; 
        mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top) + 2 * thickness;
    }

    Marshal.StructureToPtr(mmi, lParam, true);
}

下面的屏幕截图显示了它在水平方向上的正确扩展方式,但出于某种原因,它不会在垂直方向上伸展。

Screenshot

1 个答案:

答案 0 :(得分:1)

我已经尝试使用以下代码更新MINMAXINFO.ptMaxTrackSize并且它有效。 此处还介绍了相关问题:Can a window be resized past the screen size/offscreen?

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) - thickness;
        mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top) - thickness;
        mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left) + 2 * thickness; 
        mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top) + 2 * thickness;
        mmi.ptMaxTrackSize = mmi.ptMaxSize;
    }

    Marshal.StructureToPtr(mmi, lParam, true);
}