windows窗口全屏

windows窗口全屏

//head file

#include <shobjidl.h>
#include <wrl/client.h>

struct SavedWindowInfo
{
        LONG style;
        LONG ex_style;
        CRect window_rect;
};
SavedWindowInfo     m_savedWindowInfo;
Microsoft::WRL::ComPtr<ITaskbarList2> task_bar_list_;

 

//.cpp file
void ShowFullScreen(bool bFullScreen)
{
    if(!m_bFullScreen) {
        m_savedWindowInfo.style = GetWindowLong(m_hWnd, GWL_STYLE);
        m_savedWindowInfo.ex_style = GetWindowLong(m_hWnd, GWL_EXSTYLE);
        ::GetWindowRect(m_hWnd, &m_savedWindowInfo.window_rect);
    }

    m_bFullScreen = bFullScreen;

    if (m_bFullScreen) {
        // Set new window style and size.
        ::SetWindowLongPtr(m_hWnd, GWL_STYLE,
            m_savedWindowInfo.style & ~(WS_CAPTION | WS_THICKFRAME));
        ::SetWindowLongPtr(m_hWnd, GWL_EXSTYLE,
            m_savedWindowInfo.ex_style & ~(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.
        MONITORINFO monitor_info;
        monitor_info.cbSize = sizeof(monitor_info);
        GetMonitorInfo(MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST),
            &monitor_info);
        CRect window_rect(monitor_info.rcMonitor);
        SetWindowPos( nullptr, window_rect.left, window_rect.right,
            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(m_hWnd, GWL_STYLE, m_savedWindowInfo.style);
        ::SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, m_savedWindowInfo.ex_style);

        // On restore, resize to the previous saved rect size.
        CRect new_rect(m_savedWindowInfo.window_rect);
        SetWindowPos(nullptr, new_rect.left, new_rect.top, new_rect.Width(),
            new_rect.Height(),
            SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
    }

    if (!task_bar_list_) {
        HRESULT hr =
            ::CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER,
                IID_PPV_ARGS(&task_bar_list_));
        if (SUCCEEDED(hr) && FAILED(task_bar_list_->HrInit()))
            task_bar_list_ = nullptr;
    }

    // As per MSDN marking the window as fullscreen should ensure that the
    // taskbar is moved to the bottom of the Z-order when the fullscreen window
    // is activated. If the window is not fullscreen, the Shell falls back to
    // heuristics to determine how the window should be treated, which means
    // that it could still consider the window as fullscreen. :(
    if (task_bar_list_)
        task_bar_list_->MarkFullscreenWindow(m_hWnd, !!m_bFullScreen);
}

 参考链接:

https://stackoverflow.com/questions/2382464/win32-full-screen-and-hiding-taskbar

https://src.chromium.org/viewvc/chrome/trunk/src/ui/views/win/fullscreen_handler.cc?revision=HEAD&view=markup

https://github.com/chromium/chromium/blob/master/ui/views/win/fullscreen_handler.h

https://github.com/chromium/chromium/blob/master/ui/views/win/fullscreen_handler.cc

posted @ 2020-03-11 17:32  余生以学  阅读(651)  评论(0编辑  收藏  举报