Win32:全屏和隐藏任务栏

Lar*_*nto 26 winapi taskbar fullscreen

我有一个窗户,我 SetWindowPos(window, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED);

它覆盖了整个屏幕,确定,但也需要一段时间(0.5秒)来覆盖任务栏.

有没有办法立即过来任务栏?我发现设置HWND_TOPMOST立即执行,但它仍然高于所有其他窗口,即使我切换应用程序 - 这是我不想要的.此外,如果我首先隐藏窗口然后显示它,它会以某种方式强制窗口重绘并立即覆盖任务栏,但它会闪烁(因为隐藏).还有另外一种方法吗?

Cza*_*zak 37

编辑2.甚至还有更好的方法来做全屏,铬方式,来自这里的来源:

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

void FullscreenHandler::SetFullscreenImpl(bool fullscreen, bool for_metro) {
  ScopedFullscreenVisibility visibility(hwnd_);

  // Save current window state if not already fullscreen.
  if (!fullscreen_) {
    // 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.
    saved_window_info_.maximized = !!::IsZoomed(hwnd_);
    if (saved_window_info_.maximized)
      ::SendMessage(hwnd_, WM_SYSCOMMAND, SC_RESTORE, 0);
    saved_window_info_.style = GetWindowLong(hwnd_, GWL_STYLE);
    saved_window_info_.ex_style = GetWindowLong(hwnd_, GWL_EXSTYLE);
    GetWindowRect(hwnd_, &saved_window_info_.window_rect);
  }

  fullscreen_ = fullscreen;

  if (fullscreen_) {
    // Set new window style and size.
    SetWindowLong(hwnd_, GWL_STYLE,
                  saved_window_info_.style & ~(WS_CAPTION | WS_THICKFRAME));
    SetWindowLong(hwnd_, GWL_EXSTYLE,
                  saved_window_info_.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.
    if (!for_metro) {
      MONITORINFO monitor_info;
      monitor_info.cbSize = sizeof(monitor_info);
      GetMonitorInfo(MonitorFromWindow(hwnd_, MONITOR_DEFAULTTONEAREST),
                     &monitor_info);
      gfx::Rect window_rect(monitor_info.rcMonitor);
      SetWindowPos(hwnd_, NULL, window_rect.x(), window_rect.y(),
                   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.
    SetWindowLong(hwnd_, GWL_STYLE, saved_window_info_.style);
    SetWindowLong(hwnd_, GWL_EXSTYLE, saved_window_info_.ex_style);

    if (!for_metro) {
      // On restore, resize to the previous saved rect size.
      gfx::Rect new_rect(saved_window_info_.window_rect);
      SetWindowPos(hwnd_, NULL, new_rect.x(), new_rect.y(),
                   new_rect.width(), new_rect.height(),
                   SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
    }
    if (saved_window_info_.maximized)
      ::SendMessage(hwnd_, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑.这可能是最好创建一个全屏窗口BrendanMcK在这个答案评论指出了这一点,看到这个链接:http://blogs.msdn.com/b/oldnewthing/archive/2005/05/05/414910.aspx("如何使用全屏窗口覆盖任务栏?")

使用上述链接的新代码将是:

HWND CreateFullscreenWindow(HWND hwnd)
{
 HMONITOR hmon = MonitorFromWindow(hwnd,
                                   MONITOR_DEFAULTTONEAREST);
 MONITORINFO mi = { sizeof(mi) };
 if (!GetMonitorInfo(hmon, &mi)) return NULL;
 return CreateWindow(TEXT("static"),
       TEXT("something interesting might go here"),
       WS_POPUP | WS_VISIBLE,
       mi.rcMonitor.left,
       mi.rcMonitor.top,
       mi.rcMonitor.right - mi.rcMonitor.left,
       mi.rcMonitor.bottom - mi.rcMonitor.top,
       hwnd, NULL, g_hinst, 0);
}
Run Code Online (Sandbox Code Playgroud)

下面的旧答案 - 不要使用它,只留下记录如何不这样做.

你必须隐藏任务栏和菜单栏才能立即看到全屏.

这是代码(使用WTL),调用SetFullScreen(true)进入全屏模式:

template <class T, bool t_bHasSip = true>
class CFullScreenFrame
{
public:
    bool m_fullscreen;
    LONG m_windowstyles;
    WINDOWPLACEMENT m_windowplacement;

    CFullScreenFrame() 
        :
        m_fullscreen(false),
        m_windowstyles(0)
    { }

    void SetFullScreen(bool fullscreen)
    {
        ShowTaskBar(!fullscreen);

        T* pT = static_cast<T*>(this);

        if (fullscreen) {
            if (!m_fullscreen) {
                m_windowstyles = pT->GetWindowLongW(GWL_STYLE);
                pT->GetWindowPlacement(&m_windowplacement);
            }

        }

        // SM_CXSCREEN gives primary monitor, for multiple monitors use SM_CXVIRTUALSCREEN.
        RECT fullrect = { 0 };              
        SetRect(&fullrect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));

        WINDOWPLACEMENT newplacement = m_windowplacement;
        newplacement.showCmd = SW_SHOWNORMAL;
        newplacement.rcNormalPosition = fullrect;

        if (fullscreen) {
            pT->SetWindowPlacement(&newplacement);
            pT->SetWindowLongW(GWL_STYLE,  WS_VISIBLE);
            pT->UpdateWindow();
        } else {
            if (m_fullscreen) {
                pT->SetWindowPlacement(&m_windowplacement);
                pT->SetWindowLongW(GWL_STYLE, m_windowstyles);
                pT->UpdateWindow();
            }
        }

        m_fullscreen = fullscreen;
    }

    void ShowTaskBar(bool show)
    {
        HWND taskbar = FindWindow(_T("Shell_TrayWnd"), NULL);
        HWND start = FindWindow(_T("Button"), NULL);

        if (taskbar != NULL) {
            ShowWindow(taskbar, show ? SW_SHOW : SW_HIDE);
            UpdateWindow(taskbar);
        }
        if (start != NULL) { 
            // Vista
            ShowWindow(start, show ? SW_SHOW : SW_HIDE);
            UpdateWindow(start);
        }       
    }
};
Run Code Online (Sandbox Code Playgroud)

您还必须向WM_CLOSE消息添加一些代码:

case WM_CLOSE:
    ShowTaskBar(true);
Run Code Online (Sandbox Code Playgroud)

这个解决方案有一个警告,如果你的应用程序崩溃或被任务管理器杀死,那么用户永久地丢失他系统上的任务栏!(除非他再次运行你的应用程序,进入全屏并退出,然后他将再次看到任务栏).

在我的回答中,我指的是"atlwince.h",但该功能仅适用于Windows CE,我上面粘贴的功能适用于XP,Vista和7.

  • -1因为你不应该搞乱任务栏; 首先不需要它; 请参阅Raymond Chen关于此的博客:[如何使用全屏窗口覆盖任务栏?](http://blogs.msdn.com/b/oldnewthing/archive/2005/05/05/414910.aspx) (2认同)
  • @BrendanMcK您的链接在2016年将不再有效。这是新链接:[我如何用全屏窗口覆盖任务栏?](https://blogs.msdn.microsoft.com/oldnewthing/20050505-04/?p = 35703) (2认同)

War*_*ace 14

是的,HWND_TOPMOST是否适合我.以下是一段代码,可以让我全屏工作(并且快速):


bool enterFullscreen(HWND hwnd, int fullscreenWidth, int fullscreenHeight, int colourBits, int refreshRate) {
    DEVMODE fullscreenSettings;
    bool isChangeSuccessful;
    RECT windowBoundary;

    EnumDisplaySettings(NULL, 0, &fullscreenSettings);
    fullscreenSettings.dmPelsWidth        = fullscreenWidth;
    fullscreenSettings.dmPelsHeight       = fullscreenHeight;
    fullscreenSettings.dmBitsPerPel       = colourBits;
    fullscreenSettings.dmDisplayFrequency = refreshRate;
    fullscreenSettings.dmFields           = DM_PELSWIDTH |
                                            DM_PELSHEIGHT |
                                            DM_BITSPERPEL |
                                            DM_DISPLAYFREQUENCY;

    SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
    SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fullscreenWidth, fullscreenHeight, SWP_SHOWWINDOW);
    isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
    ShowWindow(hwnd, SW_MAXIMIZE);

    return isChangeSuccessful;
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您说错了设置,这将改变分辨率.这是我通常想要的,但是如果你不喜欢这样,你可以通过使用(mainWindow从类似CreateWindow()或返回的地方返回CreateWindowEx())找到你的解决方案:


windowHDC = GetDC(mainWindow);
fullscreenWidth  = GetDeviceCaps(windowHDC, HORZRES);
fullscreenHeight = GetDeviceCaps(windowHDC, VERTRES);
colourBits       = GetDeviceCaps(windowHDC, BITSPIXEL);
refreshRate      = GetDeviceCaps(windowHDC, VREFRESH);
Run Code Online (Sandbox Code Playgroud)

当你想要退出全屏时,你会做这样的事情:


bool exitFullscreen(HWND hwnd, int windowW, int windowY, int windowedWidth, int windowedHeight, int windowedPaddingX, int windowedPaddingY) {
    bool isChangeSuccessful;

    SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_LEFT);
    SetWindowLongPtr(hwnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE);
    isChangeSuccessful = ChangeDisplaySettings(NULL, CDS_RESET) == DISP_CHANGE_SUCCESSFUL;
    SetWindowPos(hwnd, HWND_NOTOPMOST, windowX, windowY, windowedWidth + windowedPaddingX, windowedHeight + windowedPaddingY, SWP_SHOWWINDOW);
    ShowWindow(hwnd, SW_RESTORE);

    return isChangeSuccessful;
}
Run Code Online (Sandbox Code Playgroud)

我设置我的代码使用热键在全屏和窗口模式之间切换,并且我将窗口模式变量保持为全局,以便在更改为窗口模式时保持放置.

这段代码还具有运行相当于"独占模式"的优势(我使用的是XP,并没有在新版本的Windows上试过它),这意味着它会更快,更快.让我知道我是否因为缩小代码而犯了任何错误(来自我更大的代码).

  • 请注意,如果您不需要更改分辨率,则不需要调用ChangeDisplaySettings.CDS_FULLSCREEN标志实际上并没有让应用程序全屏显示,它只是说"这些设置是临时的,就像使用全屏幕的窗口一样".Raymond Chen博客上有关此旗帜的更多详情:[当显示更改是暂时的时,这是什么意思?](http://blogs.msdn.com/b/oldnewthing/archive/2008/01/04/6973747.aspx ) (2认同)

小智 7

Raymond Chen在他的博客中描述了"正确"的方法:

http://blogs.msdn.com/b/oldnewthing/archive/2005/05/05/414910.aspx

建议明确地摆弄任务栏窗口.