C++中的ReadConsoleOutputAttribute函数用法
ReadConsoleOutputAttribute function usage in c++
我需要在 C++ 中通过已知坐标获取背景颜色。我尝试使用 windows.h 中的 ReadConsoleOutputAttribute,但没有成功。这是我的代码:
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD count;
COORD cursor = { this->X, this->Y };
LPWORD *lpAttr = new LPWORD;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(console, &info);
ReadConsoleOutputAttribute(console, *lpAttr, 1, cursor, &count);
这里出了什么问题,有什么解决方法?我应该从 lpAttr 或什么获取背景颜色?
颜色属性由两个十六进制数字指定 -- 第一个
对应背景;第二个前景。每个数字
可以是以下任何值:
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
我们只需要提取十六进制结果的第一位就可以知道当前coordinate.This的背景颜色是我的测试代码。我修改了鼠标坐标点以简化测试
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
//color set test
system("color F0");
std::cout << "Hello World!\n" << endl;
HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
if (!Console)
return 0;
CONSOLE_SCREEN_BUFFER_INFO buf;
GetConsoleScreenBufferInfo(Console, &buf);
WORD Attr;
DWORD Count;
COORD pos = buf.dwCursorPosition;
ReadConsoleOutputAttribute(Console, &Attr, 1, pos, &Count);
//hexadecimal out
cout << hex << Attr << endl;
if (Attr)
{ //extract the first digit of the hexadecimal result
int color = Attr / 16;
switch (color)
{
case 0:cout << " background color is Black. " << endl; break;
case 1:cout << " background color is Blue. " << endl;break;
case 2:cout << " background color is Green . " << endl; break;
case 3:cout << " background color is Aqua. " << endl; break;
case 4:cout << " background color is Red. " << endl; break;
case 5:cout << " background color is Purple . " << endl; break;
case 6:cout << " background color is Yellow. " << endl; break;
case 7:cout << " background color is White. " << endl; break;
case 8:cout << " background color is Gray . " << endl; break;
case 9:cout << " background color is Light Blue." << endl; break;
case 10:cout << " background color is Light Green." << endl; break; //A
case 11:cout << " background color is Light Aqua ." << endl; break; //B
case 12:cout << " background color is Light Red . " << endl; break; //C
case 13:cout << " background color is Light Purple . " << endl; break; //D
case 14:cout << " background color is Light Yellow . " << endl; break; //E
case 15:cout << " background color is Bright White . " << endl; break; //F
default:
cout << " error color " << endl;
break;
}
}
return 0;
}
我需要在 C++ 中通过已知坐标获取背景颜色。我尝试使用 windows.h 中的 ReadConsoleOutputAttribute,但没有成功。这是我的代码:
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD count;
COORD cursor = { this->X, this->Y };
LPWORD *lpAttr = new LPWORD;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(console, &info);
ReadConsoleOutputAttribute(console, *lpAttr, 1, cursor, &count);
这里出了什么问题,有什么解决方法?我应该从 lpAttr 或什么获取背景颜色?
颜色属性由两个十六进制数字指定 -- 第一个 对应背景;第二个前景。每个数字 可以是以下任何值:
0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White
我们只需要提取十六进制结果的第一位就可以知道当前coordinate.This的背景颜色是我的测试代码。我修改了鼠标坐标点以简化测试
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
//color set test
system("color F0");
std::cout << "Hello World!\n" << endl;
HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
if (!Console)
return 0;
CONSOLE_SCREEN_BUFFER_INFO buf;
GetConsoleScreenBufferInfo(Console, &buf);
WORD Attr;
DWORD Count;
COORD pos = buf.dwCursorPosition;
ReadConsoleOutputAttribute(Console, &Attr, 1, pos, &Count);
//hexadecimal out
cout << hex << Attr << endl;
if (Attr)
{ //extract the first digit of the hexadecimal result
int color = Attr / 16;
switch (color)
{
case 0:cout << " background color is Black. " << endl; break;
case 1:cout << " background color is Blue. " << endl;break;
case 2:cout << " background color is Green . " << endl; break;
case 3:cout << " background color is Aqua. " << endl; break;
case 4:cout << " background color is Red. " << endl; break;
case 5:cout << " background color is Purple . " << endl; break;
case 6:cout << " background color is Yellow. " << endl; break;
case 7:cout << " background color is White. " << endl; break;
case 8:cout << " background color is Gray . " << endl; break;
case 9:cout << " background color is Light Blue." << endl; break;
case 10:cout << " background color is Light Green." << endl; break; //A
case 11:cout << " background color is Light Aqua ." << endl; break; //B
case 12:cout << " background color is Light Red . " << endl; break; //C
case 13:cout << " background color is Light Purple . " << endl; break; //D
case 14:cout << " background color is Light Yellow . " << endl; break; //E
case 15:cout << " background color is Bright White . " << endl; break; //F
default:
cout << " error color " << endl;
break;
}
}
return 0;
}