变量定义:
typedef struct SCREEN_INFO { DWORD dwStyle; DWORD dwExStyle; CRect rect; bool bMaximized; }SreenInfo; SreenInfo m_screenInfo; bool m_bFullScreen = false;
实现代码:
if (!m_bFullScreen) { // Save current window information. We force the window into restored mode // before going fullscreen because Windows doesn't seem to hide the // taskbar if the window is in the maximized state. m_screenInfo.bMaximized = !!::IsZoomed(this->m_hWnd); // if (m_screenInfo.bMaximized) // ::SendMessage(this->m_hWnd, WM_SYSCOMMAND, SC_RESTORE, 0); m_screenInfo.dwStyle = GetWindowLongPtr(this->m_hWnd, GWL_STYLE); m_screenInfo.dwExStyle = GetWindowLongPtr(this->m_hWnd, GWL_EXSTYLE); GetWindowRect(m_screenInfo.rect); } m_bFullScreen = !m_bFullScreen; if (m_bFullScreen) { // Set new window style and size. SetWindowLongPtr(this->m_hWnd, GWL_STYLE, m_screenInfo.dwStyle & ~(WS_CAPTION | WS_THICKFRAME)); SetWindowLongPtr(this->m_hWnd, GWL_EXSTYLE, m_screenInfo.dwExStyle & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); // On expand, if we're given a window_rect, grow to it, otherwise do // not resize. bool for_metro = false; if (!for_metro) { MONITORINFO monitor_info; monitor_info.cbSize = sizeof(monitor_info); GetMonitorInfo(MonitorFromWindow(this->m_hWnd, MONITOR_DEFAULTTONEAREST), & monitor_info); CRect window_rect(monitor_info.rcMonitor); SetWindowPos(NULL, window_rect.left, window_rect.top, window_rect.Width(), window_rect.Height(), SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); } } else { // Reset original window style and size. The multiple window size/moves // here are ugly, but if SetWindowPos() doesn't redraw, the taskbar won't be // repainted. Better-looking methods welcome. SetWindowLongPtr(this->m_hWnd, GWL_STYLE, m_screenInfo.dwStyle); SetWindowLongPtr(this->m_hWnd, GWL_EXSTYLE, m_screenInfo.dwExStyle); // On restore, resize to the previous saved rect size. CRect new_rect(m_screenInfo.rect); SetWindowPos(NULL, new_rect.left, new_rect.top, new_rect.Width(), new_rect.Height(), SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); }
参考:
1. https://stackoverflow.com/questions/2382464/win32-full-screen-and-hiding-taskbar,第一个高赞回答。
2.https://github.com/chromium/chromium/blob/master/ui/views/win/fullscreen_handler.cc, chromium 浏览器源代码。