如何将垂直滚动条添加到全屏win32控制台?

时间:2019-05-02 04:42:11

标签: c++ winapi

当我将win32控制台设置为全屏显示时,垂直滚动条消失了。当文本到达屏幕底部时,它不会向上滚动。新写入的输出在下面,因此不会呈现给用户。

这是样式:

if (isFullScreen)
{
    // Set the full screen window style.
    style = GetWindowLong(handle, GWL_STYLE);

    style &= ~(WS_BORDER | WS_CAPTION | WS_THICKFRAME | WS_OVERLAPPEDWINDOW);

    SetWindowLong(handle, GWL_STYLE, style);

    // Minimalize, then show maximized to avoid the cursor blink bug in conhost.exe.
    ShowWindow(handle, SW_MINIMIZE);
    ShowWindow(handle, SW_SHOWMAXIMIZED);

    // Set the font size
    setFontSize(fontSize);
}

我在网上搜索,但不常见。

在全屏模式下如何向其中添加垂直滚动条?

2 个答案:

答案 0 :(得分:0)

在我的计算机上,我可以实现全屏控制台和垂直滚动条的外观。我的系统是Win10,使用vs2017。 这是我的代码。

#include "pch.h"
#include <iostream>
#include <Windows.h>

void full_screen()
{
    HWND hwnd = GetForegroundWindow();
    int cx = GetSystemMetrics(SM_CXSCREEN);            /* Screen width pixels */
    int cy = GetSystemMetrics(SM_CYSCREEN);            /* Screen Height Pixel */

    LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE);   /* Get window information */
    /* Set window information to maximize the removal of title bar and border*/
    SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);

    SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}

int main()
{
    full_screen();
    while(1)
    {
        std::cout << "Hello World!\n"; 
    }
    return 0;
}

调试结果:

2

答案 1 :(得分:0)

将控制台设置为全屏的官方方法是致电SetConsoleDisplayMode()

在Windows 10 Pro版本1803中,以下代码显示了垂直滚动条,而无需多加修改:

#include <iostream>
#include <windows.h>

int main()
{
    HANDLE const hConsole = ::GetStdHandle( STD_OUTPUT_HANDLE );
    if( hConsole == INVALID_HANDLE_VALUE ||
        ! ::SetConsoleDisplayMode( hConsole, CONSOLE_FULLSCREEN_MODE, nullptr ) )
    {
        DWORD const err = ::GetLastError();
        std::cerr << "Failed to set console fullscreen mode. System error: " << err << "\n";
        return 1;
    }

    for( int i = 0; i < 200; ++i )
    {
        std::cout << "Hello World!\n";
    }
    return 0;
}

请注意,如果未将进程附加到控制台(例如,将标志SetConsoleDisplayMode()传递到CREATE_NO_WINDOW)或STDOUT重定向到文件,则CreateProcess()可能会失败。