Win32:将黑色边框添加到全屏窗口

时间:2015-03-10 15:43:58

标签: c++ winapi window fullscreen

我正在尝试在Windows中以全屏模式保留内容宽高比。如果显示宽高比与内容宽高比不同,我想将桌面的其余部分隐藏在黑色边框后面。是否可以使用Win32 api创建具有居中内容和黑色边框的全屏窗口?

在OS X中,使用以下代码可以很容易地实现这一点:

CGSize ar;
ar.width = 800;
ar.height = 600;
[self.window setContentAspectRatio:ar];
[self.window center];
[self.window toggleFullScreen:nil];

如果我在16:9显示器上运行上面的代码,我的应用程序进入全屏模式,内容居中(因为它是4:3)并且屏幕两侧都有黑色边框。

我试图在Windows中实现相同的功能,但我开始怀疑它是否可能。我目前的全屏代码保持宽高比和 将内容居中,但如果fullscreenWidthfullscreenHeight不等于displayWidthdisplayHeight,则会在窗口两侧显示桌面:

bool enterFullscreen(int fullscreenWidth, int fullscreenHeight)
{
    DEVMODE fullscreenSettings;
    bool isChangeSuccessful;

    int displayWidth = GetDeviceCaps(m_hDC, HORZRES);
    int displayHeight = GetDeviceCaps(m_hDC, VERTRES);

    int colourBits = GetDeviceCaps(m_hDC, BITSPIXEL);
    int refreshRate = GetDeviceCaps(m_hDC, VREFRESH);

    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(m_hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
    SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
    SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, displayWidth, displayHeight, SWP_SHOWWINDOW);
    isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
    ShowWindow(m_hWnd, SW_MAXIMIZE);

    RECT rcWindow;
    GetWindowRect(m_hWnd, &rcWindow);

    // calculate content position
    POINT ptDiff;
    ptDiff.x = ((rcWindow.right - rcWindow.left) - fullscreenWidth) / 2;
    ptDiff.y = ((rcWindow.bottom - rcWindow.top) - fullscreenHeight) / 2;

    AdjustWindowRectEx(&rcWindow, GetWindowLong(m_hWnd, GWL_STYLE), FALSE, GetWindowLong(m_hWnd, GWL_EXSTYLE));
    SetWindowPos(m_hWnd, 0, ptDiff.x, ptDiff.y, displayWidth, displayHeight, NULL);

    return isChangeSuccessful;
}

1 个答案:

答案 0 :(得分:5)

完成所需内容的最简单方法是创建一个子窗口( C )来呈现您的内容,为父项留下任何多余的空间( P )。

应使用黑色笔刷为其背景创建

P 。注册窗口类((HBRUSH)GetStockObject(BLACK_BRUSH))时,为WNDCLASS structurehbrBackground成员指定RegisterClass。为了防止在删除背景时闪烁, P 应该有WS_CLIPCHILDREN Window Style

每当 P 更改其大小时,WM_SIZE message将被发送到 P 的窗口过程。然后处理程序可以调整 C 的位置和大小以保持宽高比。

要创建无边框子窗口 C ,请在CreateWindow调用中使用WS_CHILD | WS_VISIBLE窗口样式。如果要在父 P 中处理鼠标输入,请添加WS_DISABLED窗口样式。

<小时/> 示例代码(为简洁而省略了错误检查):

#define STRICT 1
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// Globals
HWND g_hWndContent = NULL;

// Forward declarations
LRESULT CALLBACK WndProcMain( HWND, UINT, WPARAM, LPARAM );
LRESULT CALLBACK WndProcContent( HWND, UINT, WPARAM, LPARAM );

int APIENTRY wWinMain( HINSTANCE hInstance,
                       HINSTANCE /*hPrevInstance*/,
                       LPWSTR /*lpCmdLine*/,
                       int nCmdShow ) {

需要注册主窗口类和内容窗口类。注册几乎完全相同,但背景画笔除外。内容窗口使用白色画笔,以便在没有任何其他代码的情况下可见:

    // Register main window class
    const wchar_t classNameMain[] = L"MainWindow";
    WNDCLASSEXW wcexMain = { sizeof( wcexMain ) };
    wcexMain.style = CS_HREDRAW | CS_VREDRAW;
    wcexMain.lpfnWndProc = WndProcMain;
    wcexMain.hCursor = ::LoadCursorW( NULL, IDC_ARROW );
    wcexMain.hbrBackground = (HBRUSH)::GetStockObject( BLACK_BRUSH );
    wcexMain.lpszClassName = classNameMain;
    ::RegisterClassExW( &wcexMain );

    // Register content window class
    const wchar_t classNameContent[] = L"ContentWindow";
    WNDCLASSEXW wcexContent = { sizeof( wcexContent ) };
    wcexContent.style = CS_HREDRAW | CS_VREDRAW;
    wcexContent.lpfnWndProc = WndProcContent;
    wcexContent.hCursor = ::LoadCursorW( NULL, IDC_ARROW );
    wcexContent.hbrBackground = (HBRUSH)::GetStockObject( WHITE_BRUSH );
    wcexContent.lpszClassName = classNameContent;
    ::RegisterClassExW( &wcexContent );

通过注册窗口类,我们可以继续并创建每个类的实例。请注意,内容窗口最初为零大小。实际大小是在父{s} WM_SIZE处理程序中进一步计算的。

    // Create main window
    HWND hWndMain = ::CreateWindowW( classNameMain,
                                     L"Constant AR",
                                     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                                     CW_USEDEFAULT, CW_USEDEFAULT, 800, 800,
                                     NULL,
                                     NULL,
                                     hInstance,
                                     NULL );
    // Create content window
    g_hWndContent = ::CreateWindowW( classNameContent,
                                     NULL,
                                     WS_CHILD | WS_VISIBLE,
                                     0, 0, 0, 0,
                                     hWndMain,
                                     NULL,
                                     hInstance,
                                     NULL );

其余部分是样板Windows应用程序代码:

    // Show application
    ::ShowWindow( hWndMain, nCmdShow );
    ::UpdateWindow( hWndMain );

    // Main message loop
    MSG msg = { 0 };
    while ( ::GetMessageW( &msg, NULL, 0, 0 ) > 0 )
    {
        ::TranslateMessage( &msg );
        ::DispatchMessageW( &msg );
    }

    return (int)msg.wParam;
}

窗口类的行为是在Window Procedure内实现的:

LRESULT CALLBACK WndProcMain( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {

    switch ( message ) {
    case WM_CLOSE:
        ::DestroyWindow( hWnd );
        return 0;

    case WM_DESTROY:
        ::PostQuitMessage( 0 );
        return 0;

    default:
        break;

除了标准的消息处理之外,主窗口的窗口过程会调整内容的大小以适应主窗口大小的变化:

    case WM_SIZE: {
        const SIZE ar = { 800, 600 };
        // Query new client area size
        int clientWidth = LOWORD( lParam );
        int clientHeight = HIWORD( lParam );
        // Calculate new content size
        int contentWidth = ::MulDiv( clientHeight, ar.cx, ar.cy );
        int contentHeight = ::MulDiv( clientWidth, ar.cy, ar.cx );

        // Adjust dimensions to fit inside client area
        if ( contentWidth > clientWidth ) {
            contentWidth = clientWidth;
            contentHeight = ::MulDiv( contentWidth, ar.cy, ar.cx );
        } else {
            contentHeight = clientHeight;
            contentWidth = ::MulDiv( contentHeight, ar.cx, ar.cy );
        }

        // Calculate offsets to center content
        int offsetX = ( clientWidth - contentWidth ) / 2;
        int offsetY = ( clientHeight - contentHeight ) / 2;

        // Adjust content window position
        ::SetWindowPos( g_hWndContent,
                        NULL,
                        offsetX, offsetY,
                        contentWidth, contentHeight,
                        SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER );

        return 0;
    }
    }

    return ::DefWindowProcW( hWnd, message, wParam, lParam );
}

内容窗口的窗口过程没有实现任何自定义行为,只是将所有消息转发到默认实现:

LRESULT CALLBACK WndProcContent( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
    return ::DefWindowProcW( hWnd, message, wParam, lParam );
}