Dehli
Dehli

Reputation: 5960

Maximize Window with Transparent Border

I'm trying to maximize a window which has a transparent border. When maximized, the transparent border shouldn't show. I follow the method found here and by using the below code I can get it to work half way.

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

The screenshot below shows how it's expanded correctly in the horizontal direction but for some reason it won't stretch in the vertical direction.

Screenshot

Upvotes: 0

Views: 684

Answers (1)

sasha_gud
sasha_gud

Reputation: 1735

I've tried the following code with updating MINMAXINFO.ptMaxTrackSize and it works. Related problem is also described here: 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);
}

Upvotes: 1

Related Questions