[英]Inter-related c++ struct's - how use color code to look in struct to get name
I'm trying to access the string color name for a color code found through structs.我正在尝试访问通过结构找到的颜色代码的字符串颜色名称。 How do I get the string color name from the color code?
如何从颜色代码中获取字符串颜色名称?
I've seen this done, but I wanted to show the string color name and not color code if I loop through all the items (it would know the color code it is so I shouldn't need to refer to the color by name and should be able to show the string color name:我已经看到这样做了,但是如果我遍历所有项目,我想显示字符串颜色名称而不是颜色代码(它会知道颜色代码是什么,所以我不需要通过名称和应该能够显示字符串颜色名称:
_stprintf(tempMsg, _T("## InkRest Black = %d, InkDimention Black = %d"), pStatusStruct->InkRest.Black, pStatusStruct->InkDimention.Black);
This is what the code looks like:这是代码的样子:
void ClassName::getInk()
{
ByteBuffer buf = StatusBuffer;
STATUS_02 pStatusStruct = (STATUS_02) & buf[0];
TCHAR tempMsg[MAX_PATH * 2] = { 0 };
for (int i = 0; i < sizeof(&pStatusStruct->CartInk); i++)
{
_stprintf(tempMsg, _T("## Ink Types(i = %d, InkRest = 0x%x, ColorType = 0x%lx, ColorName = 0x%lx"), i, pStatusStruct->CartInk[i].InkRest, pStatusStruct->CartInk[i].ColorType, pStatusStruct->CartInk[i].ColorType.stringnamehere); //the last one needs to return the string name ??
}
}
The Struct's are defined like this: Struct 的定义如下:
typedef struct _INKINFO {
BYTE Black ; //Black
BYTE Cyan ; //Cyan
BYTE Magenta ; //Magenta
BYTE Yellow ; //Yellow
BYTE LightCyan ; //Light cyan
BYTE LightMagenta ; //Light magenta
BYTE LightYellow ; //Light yellow
BYTE DarkYellow ; //Dark yellow
BYTE LightBlack ; //Light black
BYTE Red ; //Red
BYTE Violet ; //Violet
BYTE Clear ; //Clear
BYTE Other ; //Other
BYTE LightLightBlack ; //LightLightBlack
BYTE Orange ; //Orange
BYTE Green ; //Green
BYTE ReserveColor[3] ; //Reserve
}INKINFO, *LPINKINFO ;
typedef struct _CARTINKINFO { //Cart and ink information
BYTE CartType ; //Cart name code
DWORD ColorType ; //Cart color code
BYTE InkRest ; //Ink rest information
BYTE InkDimension ; //Ink dimension information
} CARTANDINKINFO, *LPCARTANDINKINFO ;
typedef struct _INKSTATUS_02{ // INK status structure 02
...
CARTANDINKINFO CartInk[16] ;
... // cart and ink information
}INKSTATUS_02, *STATUS_02 ;
Also, this may be more applicable to the changes made to the ColorType with the _02 round, and is not a struct like the other color struct, but I guess I could make it one:此外,这可能更适用于使用 _02 轮对 ColorType 所做的更改,并且不像其他颜色结构那样是一个结构,但我想我可以把它变成一个:
Color <color bit>
Black 0x00000001
Cyan 0x00000002
Magenta 0x00000004
Yellow 0x00000008
Light cyan 0x00000010
Light magenta 0x00000020
Light yellow 0x00000040
Dark yellow 0x00000080
Light black 0x00000100
Red 0x00000200
Violet 0x00000400
Clear 0x00000800
Unknown 0x00001000
Light Light Black 0x00002000
Orange 0x00004000
Green 0x00008000
White 0x00010000
Cleaning 0x00020000
Silver 0x00040000
Gray 0x00080000
I'm having trouble finding what to google for this question.我找不到要用谷歌搜索这个问题的内容。 I appreciate any ideas!
我感谢任何想法!
You can use a map to look up a name for a color ID, something like this:您可以使用 map 查找颜色 ID 的名称,如下所示:
std::map<DWORD, std::string> colorNames {
{0x00000001, "Black" },
{0x00000002, "Cyan" },
{0x00000004, "Magenta" },
{0x00000008, "Yellow" },
{0x00000010, "Light cyan" },
{0x00000020, "Light magenta" },
{0x00000040, "Light yellow" },
{0x00000080, "Dark yellow" },
{0x00000100, "Light black" },
{0x00000200, "Red" },
{0x00000400, "Violet" },
{0x00000800, "Clear" },
{0x00001000, "Unknown" },
{0x00002000, "Light Light Black" },
{0x00004000, "Orange" },
{0x00008000, "Green" },
{0x00010000, "White" },
{0x00020000, "Cleaning" },
{0x00040000, "Silver" },
{0x00080000, "Gray" }
};
...which you'd use something like this: ...你会使用这样的东西:
for (int i = 0; i < sizeof(&pStatusStruct->CartInk); i++) {
auto name = colorNames.find(pStatusStruct->CartInk[i].ColorType);
_stprintf(tempMsg, _T("## Ink Types(i = %d, InkRest = 0x%x, ColorType = 0x%lx, ColorName = %s"),
i,
pStatusStruct->CartInk[i].InkRest,
pStatusStruct->CartInk[i].ColorType,
name->second.c_str());
}
You probably also should check that name.= colorNames.end()
before you try to use it--that's what'll happen if you pass a value that's not in the map. Presumably that should never happen, but...您可能还应该在尝试使用
name.= colorNames.end()
之前检查它——如果您传递的值不在 map 中,就会发生这种情况。大概永远不会发生这种情况,但是...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.