cwindowswinapifullscreen

Toggle between normal mode and full screen in Win32


I created a function that toggles the window between normal mode and full screen. If the width parameter equals 0, the window will be in full screen mode. Otherwise, it will be in the normal screen mode and will depend on its size in the height and width parameters.

// width equle 0 = fullscreen, other 0 = normal window
void CL_fulscrn(HWND hwnd, const unsigned short W, const unsigned short H) {
    if (!W) {
        SetWindowLongPtr(hwnd, GWL_EXSTYLE, 0);
        SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | WS_POPUP);
        MoveWindow(hwnd, 0, 0, GetDeviceCaps(GetDC(NULL), HORZRES), GetDeviceCaps(GetDC(NULL), VERTRES), 0);
    }
    else {
        SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW);
        SetWindowLongPtr(hwnd, GWL_STYLE, WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME | WS_VISIBLE | WS_OVERLAPPED);
        MoveWindow(hwnd, (GetDeviceCaps(GetDC(NULL), HORZRES) - W) / 2, (GetDeviceCaps(GetDC(NULL), VERTRES) - H) / 2, W, H, 0);
    }
}

when in full screen mode, the window is fine, but when switching to normal mode: enter image description here

So why?

I tried to change the MoveWindow function to the SetWindowPos function:

// width equle 0 = fullscreen, other 0 = normal window
void CL_fulscrn(HWND hwnd, const unsigned short W, const unsigned short H) {
    if (!W) {
        SetWindowLongPtr(hwnd, GWL_EXSTYLE, 0);
        SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | WS_POPUP);
        SetWindowPos(hwnd, 0, 0, 0, GetDeviceCaps(GetDC(NULL), HORZRES), GetDeviceCaps(GetDC(NULL), VERTRES), SWP_NOZORDER);
    }
    else {
        SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW);
        SetWindowLongPtr(hwnd, GWL_STYLE, WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME | WS_VISIBLE | WS_OVERLAPPED);
        SetWindowPos(hwnd, 0, (GetDeviceCaps(GetDC(NULL), HORZRES) - W) / 2, (GetDeviceCaps(GetDC(NULL), VERTRES) - H) / 2, W, H, SWP_NOZORDER);
    }
}

But same problem.

And I tried this:

void CL_fulscrn(HWND hwnd, const unsigned short W, const unsigned short H) {
    if (!W) {
        SetWindowLongPtr(hwnd, GWL_EXSTYLE, 0);
        SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | WS_POPUP);
        // I tried this too, and same problem
        //SetWindowPos(hwnd, NULL, 0, 0, GetDeviceCaps(GetDC(NULL), HORZRES), GetDeviceCaps(GetDC(NULL), VERTRES), SWP_FRAMECHANGED | SWP_NOZORDER);
         SetWindowPos(hwnd, NULL, 0, 0, GetDeviceCaps(GetDC(NULL), HORZRES), GetDeviceCaps(GetDC(NULL), VERTRES), SWP_FRAMECHANGED);
    }
    else {
        SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW);
        SetWindowLongPtr(hwnd, GWL_STYLE, WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME | WS_VISIBLE | WS_OVERLAPPED);
        // I tried this too, and same problem
        //SetWindowPos(hwnd, NULL, (GetDeviceCaps(GetDC(NULL), HORZRES) - W) / 2, (GetDeviceCaps(GetDC(NULL), VERTRES) - H) / 2, W, H, SWP_FRAMECHANGED | SWP_NOZORDER);
         SetWindowPos(hwnd, NULL, (GetDeviceCaps(GetDC(NULL), HORZRES) - W) / 2, (GetDeviceCaps(GetDC(NULL), VERTRES) - H) / 2, W, H, SWP_FRAMECHANGED);
    }
}


Solution

  • The problem is solved, As he said @Torrecto - MSFT, I just had to add WS_CAPTION with WS_SYSMENU.