views:

119

answers:

1

I'm trying to make an MFC window (a CDialog) go fullscreen whenever the user attempts to maximize it. The window is being used as an OpenGL context. I'm attempting to do everything inside of the CDialog::OnSize callback. Here's the code that I'm using:

void MyCDialogSubclass::OnSize(UINT action, int width, int height) {
    CDialog::OnSize(action, width, height);

    switch (action) {
        case SIZE_MAXIMIZED:
            if (GetStyle() & WS_OVERLAPPEDWINDOW) {
                MONITORINFO screen;
                screen.cbSize = sizeof(screen);
                if (GetMonitorInfo(MonitorFromWindow(GetSafeHwnd(), MONITOR_DEFAULTTOPRIMARY), &screen)) {
                    ModifyStyle(WS_OVERLAPPEDWINDOW, 0, 0);
                    width = screen.rcMonitor.right - screen.rcMonitor.left;
                    height = screen.rcMonitor.bottom - screen.rcMonitor.top;
                    SetWindowPos(&wndTop, screen.rcMonitor.left, screen.rcMonitor.top, width, height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
                }
            }
            break;
        case SIZE_MINIMIZED:
        case SIZE_RESTORED:
            if (!(GetStyle() & WS_OVERLAPPEDWINDOW)) {
                ModifyStyle(0, WS_OVERLAPPEDWINDOW, 0);
                SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
            }
            break;
    }

    if (wglMakeCurrent(my_hdc, my_hglrc))
        my_opengl_reshape_call(width, height);
    wglMakeCurrent(NULL, NULL);
}

If I comment out the ModifyStyle() calls, everything works fine, with the obvious proviso that the window style stays normal, so there's a standard window title bar across the top of the screen that I want to get rid of. If I keep the ModifyStyle() calls and comment out the SetWindowPos() calls, the title bar and everything else disappears, but the window has a black region along the top of the screen that is the exact height of the title bar—as though it is being reserved. If I don't comment out either of the pairs of calls, as shown in the code above, the screen flickers violently. I believe it's flickering back and forth between the black region being present and not being present, but it's difficult to tell. This flickering also appears to corrupt video memory, as I get persistent artifacts in window title bars (in different applications, no less) and, once, the login picture in the Start menu was replaced with one of my OpenGL textures.

The code that I'm using is adapted from the code that Stefan linked in an answer below, from The Old New Thing, which is working better than my original code. I'm assuming this problem isn't arising from my decision not to insert code to save the window placement (per The Old New Thing), because this happens before I ever try to restore the window.

+1  A: 

Don't maximize the window if you want it to be full screen. Use this approach instead.

Stefan
Thanks, that code is working better than my original effort, but it's not working fully. I edited my original post. I can't tell if it's related to attempting to do this in response to maximizing the window or not. Thoughts?
John Calsbeek
if you get artifacts on other windows than your own, you have either a serious memory problem (writing to memory which you don't own) or more likely you're using the wrong DC (always use your own window DC, never the desktop dc).
Stefan
I'm definitely only using my own DC. I'm pretty sure I don't have a problem with pointers getting confused. I'm more inclined to believe that the corruption has something to do with the insane flickering of the window than to my code directly. I don't know why having both of the calls causes the window to violently and frequently resize itself (I'm assuming it's resizing).
John Calsbeek