前段时间我注意到有一个新的控制台模式ENABLE_VIRTUAL_TERMINAL_PROCESSING,我决定尝试一下.这是我的示例代码:
// File: test1.c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char * const TEST_STRING = "\x1B[31;1mRed\x1B[0m \x1B[32;1mGreen\x1B[0m \x1B[34;1mBlue\x1B[0m";
void ErrorExit(const char* errorMessage) {
puts(errorMessage);
exit(1);
}
int main(int argc, char** argv) {
if (argc != 2) {
ErrorExit("Usage: program (enable|disable|test|sample)");
}
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE), hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode;
char *cmd = argv[1];
if (!strcmp(cmd, "enable")) {
/*
GetConsoleMode(hInput, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(hInput, dwMode);
*/
GetConsoleMode(hOutput, &dwMode);
dwMode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOutput, dwMode)) {
ErrorExit("SetConsoleMode failed.");
}
}
else if (!strcmp(cmd, "disable")) {
/*
GetConsoleMode(hInput, &dwMode);
dwMode &= ~ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(hInput, dwMode);
*/
GetConsoleMode(hOutput, &dwMode);
dwMode &= ~ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOutput, dwMode)) {
ErrorExit("SetConsoleMode failed.");
}
}
else if (!strcmp(cmd, "test")) {
puts(TEST_STRING);
}
else if (!strcmp(cmd, "sample")) {
SetConsoleTextAttribute(hOutput, 0x0C);
printf("Red");
SetConsoleTextAttribute(hOutput, 0x07);
printf(" ");
SetConsoleTextAttribute(hOutput, 0x0A);
printf("Green");
SetConsoleTextAttribute(hOutput, 0x07);
printf(" ");
SetConsoleTextAttribute(hOutput, 0x09);
printf("Blue");
SetConsoleTextAttribute(hOutput, 0x07);
printf("\n");
}
else {
ErrorExit("Invalid command!");
}
return 0;
}
代码成功编译到test1.exe中,但它没有按预期工作:
我相当肯定我还有其他一切正确.我正在运行最新的Windows 10 Enterprise 64位版本10.0.17763.1.
我也试过这个,但没有区别:
else if (!strcmp(cmd, "test")) {
DWORD dwNumber = strlen(TEST_STRING), dwWritten;
WriteConsole(hOutput, TEST_STRING, dwNumber, &dwWritten, NULL);
puts("");
}
那么为什么我的代码不能正常工作(在运行test1 enable后调用test1测试时仍然生成垃圾)?
最佳答案 问题是,使用SetConsoleMode()进行的设置仅影响正在运行的进程(和潜在的子进程).这意味着,它实际上不是命令行窗口的设置,也不是“传回”父shell进程.你必须在输出之前直接设置它,即:
else if (!strcmp(cmd, "test")) {
GetConsoleMode(hOutput, &dwMode);
dwMode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOutput, dwMode)) {
ErrorExit("SetConsoleMode failed.");
}
puts(TEST_STRING);
}
这应该按照您的预期工作.