Search code examples
c++mfcwtl

How to set CWindowImpl FullScreen in WTL application?


I am looking at MFC SDI Cview fullscreen sample application:

https://www.codeproject.com/Articles/9632/Views-in-Full-Screen-Mode

I test the fullscreen setting source code with WTL application, but it doesn't worked.

should i have to use SetWindowPlacement()/GetWindowPlacement() function?

void toggle_fullscreen()
{
  if (b_fullscreen == false)
  {
    b_fullscreen = true;
    save_window = this->GetParent();
    const HWND hwnd = GetDesktopWindow();
    this->SetParent(hwnd);
    RECT rect;
    ::GetWindowRect(hwnd, &rect);
    //m_view.SetWindowPos(hwnd, rect.left, rect.top, rect.right, rect.bottom, SWP_SHOWWINDOW);
    //m_view.SetWindowPos(HWND_TOPMOST, 0, 0, 4096, 2000, SWP_SHOWWINDOW);
    //m_view.ShowWindow(SW_MAXIMIZE);
    this->SetWindowPos(HWND_TOPMOST, 0, 0, 1920, 1080, SWP_SHOWWINDOW);
  }
  else
  {
    b_fullscreen = false;
    //m_view.SetParent(save_window);
    this->SetParent(save_window);
  }
}

EDIT:

thanks! actually i found the source code. (Thanks to Czarek Tomczak)

Win32: full-screen and hiding taskbar

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
  case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
  case WM_RBUTTONDOWN:
    printf("full screen\n");
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
}

HWND CreateFullscreenWindow(HWND hwnd)
{ 

  WNDCLASSEX wc;
  wc.cbSize = sizeof(WNDCLASSEX);
  wc.style = 0;
  wc.lpfnWndProc = WndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = (HINSTANCE)::GetModuleHandle(NULL); 
  wc.hIcon = NULL;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); // (HBRUSH)(COLOR_WINDOW + 1);
  wc.lpszMenuName = NULL;
  wc.lpszClassName = L"fullscreen";
  wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  RegisterClassEx(&wc);

 HMONITOR hmon = MonitorFromWindow(hwnd,
                                   MONITOR_DEFAULTTONEAREST);
 MONITORINFO mi = { sizeof(mi) };
 if (!GetMonitorInfo(hmon, &mi)) return NULL;
 return CreateWindowEx(NULL,
       TEXT("fullscreen"),
       TEXT(""),
       WS_POPUP | WS_VISIBLE,
       mi.rcMonitor.left,
       mi.rcMonitor.top,
       mi.rcMonitor.right - mi.rcMonitor.left,
       mi.rcMonitor.bottom - mi.rcMonitor.top,
       hwnd, NULL, wc.hInstance, 0);
}

Solution

  • Should i have to use SetWindowPlacement()... function?

    You can but you don't have to. This and SetWindowPos and other API functions are all about the same thing: you modify position of your window so that it fully covers coordinates corresponding to specific monitor, and you set the window as topmost. This creates a "full screen effect".

    The code you found uses desktop window handle and docks your window to it as a child. This may work, but I would not do it - I would create a standard popup window, borderless and captionless, instead without any need to re-parent it. I would rather re-parent the child window between "normal" UI and full screen popup frame helper window.