【问题标题】:This code should refresh the screen. Please tell me where I am wrong此代码应刷新屏幕。请告诉我哪里错了
【发布时间】:2021-11-29 17:42:33
【问题描述】:

据我了解,WriteConsole() 函数将一个字符数组写入屏幕缓冲区。在我尝试循环之前它一直有效。

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

int main() {
    HANDLE hScreenBuffer;

    hScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);

    char map[100];
    LPDWORD number_of_chars_to_write;

    for (int i = 0; i < 100; i++) {
        map[i] = '#';
    }
    while (1) {
        
        WriteConsole(
            hScreenBuffer,
            map,
            100,
            number_of_chars_to_write,
            NULL

        );
    }
}

【问题讨论】:

  • 调用 WriteConsoleA 而不是 WriteConsole
  • 没有改变

标签: c++ winapi


【解决方案1】:

您的代码中有两个问题:

首先,正如评论中所指出的,您需要使用WriteConsoleA,因为您写的是狭窄的chars。

其次,你将未初始化的指针LPDWORD number_of_chars_to_write; 传递给WriteConsole,这可能会导致崩溃。

这是更正后的代码:

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

int main() {
    HANDLE hScreenBuffer;

    hScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);

    char map[100];
    DWORD number_of_chars_to_write;

    for (int i = 0; i < 100; i++) {
        map[i] = '#';
    }
    while (1) {
        WriteConsoleA(
            hScreenBuffer,
            map,
            100,
            &number_of_chars_to_write,
            NULL
        );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多