Skip to content

Instantly share code, notes, and snippets.

@katahiromz
Created October 9, 2018 12:08
Show Gist options
  • Save katahiromz/9df012e891fc129efbba531edbbbfd7f to your computer and use it in GitHub Desktop.
Save katahiromz/9df012e891fc129efbba531edbbbfd7f to your computer and use it in GitHub Desktop.
Fullscreen.cpp
HWND m_hWnd = ...;
bool m_fullscreen = false;
bool m_maximized = false;
DWORD m_old_style;
DWORD m_old_exstyle;
RECT m_old_rect;
void SetFullscreen(bool fullscreen)
{
if (m_fullscreen == fullscreen)
return;
if (fullscreen)
{
m_maximized = !!::IsZoomed(m_hWnd);
if (m_maximized)
{
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
}
m_old_style = GetWindowLong(m_hWnd, GWL_STYLE);
m_old_exstyle = GetWindowLong(m_hWnd, GWL_EXSTYLE);
GetWindowRect(m_hWnd, &m_old_rect);
DWORD style = m_old_style & ~(WS_CAPTION | WS_THICKFRAME);
DWORD exstyle = m_old_exstyle & ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE |
WS_EX_DLGMODALFRAME | WS_EX_STATICEDGE);
SetWindowLong(m_hWnd, GWL_STYLE, style);
SetWindowLong(m_hWnd, GWL_EXSTYLE, exstyle);
HMONITOR hMonitor = ::MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi;
mi.cbSize = sizeof(mi);
::GetMonitorInfo(hMonitor, &mi);
RECT& rect = mi.rcMonitor;
::SetWindowPos(m_hWnd, NULL, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
else
{
SetWindowLong(m_hWnd, GWL_STYLE, m_old_style);
SetWindowLong(m_hWnd, GWL_EXSTYLE, m_old_exstyle);
RECT& rect = m_old_rect;
::SetWindowPos(m_hWnd, NULL, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
if (m_maximized)
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
m_fullscreen = fullscreen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment