【问题标题】:WriteConsoleOutputCharacterW - Where are my new lines?WriteConsoleOutputCharacterW - 我的新行在哪里?
【发布时间】:2019-09-28 19:27:47
【问题描述】:

我正在尝试学习如何使用屏幕缓冲区,但我犯了一个我不明白的错误。这些是我的屏幕缓冲区的设置:

wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
for (int i = 0; i < nScreenWidth * nScreenHeight; i++) {
    screen[i] = L' ';
}

HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;

WriteConsoleOutputCharacterW(hConsole, screen, (nScreenWidth * nScreenHeight), { 0,0 }, &dwBytesWritten);

我设法将我的二维数组打印到它上面,但奇怪的是它平放在我的终端窗口中(请参阅打印屏幕的链接)。

Small print screen of my failed 2D array

好像所有的新行都被删除了。这是我将二维数组打印到“屏幕”的循环。

int g = 0;

while (g < 100) {
    WriteConsoleOutputCharacterW(hConsole, screen, (nScreenWidth * nScreenHeight), { 0,0 }, &dwBytesWritten);

    for (int i = 0; i < field.difficulty; i++) {
        std::this_thread::sleep_for(std::chrono::milliseconds(50));

    }

    for (int y = 0; y < field.nFieldHeight; y++) {
        for (int x = 0; x < field.nFieldWidth; x++) {

            screen[(y + 2) * field.nFieldWidth + (x + 2)] = field.matrix[x][y];
        }

    }
}

是否每次打印字符时都需要写入屏幕缓冲区中的坐标?

【问题讨论】:

    标签: c++ windows console


    【解决方案1】:

    默认情况下,控制台窗口是可调整大小的,这会导致输出换行。您可以使用以下方法来防止这种情况:

    // Get console window
    HWND hwndWindow = GetConsoleWindow();
    
    // Prevent resize & maximize
    LONG lFlags = GetWindowLong(hwndWindow , GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX & ~WS_HSCROLL;
    SetWindowLong(hwndWindow , GWL_STYLE, lFlags);
    
    // Get console handle
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    // Set window and buffer size
    _SMALL_RECT consoleRect = { 0, 0, SCREEN_W - 1, SCREEN_H - 1 };
    SetConsoleScreenBufferSize(hConsole, { SCREEN_W, SCREEN_H });
    SetConsoleWindowInfo(hConsole, TRUE, &consoleRect);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多