实际上,主题是关于如何将GDI+的Status
(GDI+方法返回的错误状态)转换为字符串的标准方法。类似于FormatMessage()
。
如果你想把GDI+状态中的标签转换为字符串,那么你可以做的最简单的事情就是这样:
const char* StatusMsgMap[] =
{
"Ok", //StatusMsgMap[Ok] = "Ok";
"GenericError", //StatusMsgMap[GenericError] = "GenericError";
"InvalidParameter", //StatusMsgMap[InvalidParameter] = "InvalidParameter";
"OutOfMemory", //StatusMsgMap[OutOfMemory] = "OutOfMemory";
//so on
};
//Usage:
std::string error = StatusMsgMap[status]; // where status is Status type!
const char* StatusMsgMap[] =
{
"the method call was successful",
"there was an error on the method call, which is identified as something other than those defined by the other elements of this enumeration",
"one of the arguments passed to the method was not valid",
//so on
};
由于状态枚举中仅有22个标签,按照上述方式创建StatusMsgMap
不会太困难,我认为5分钟足够了!
这是我写的一些使用硬编码数值的内容。
std::string statusString(const Gdiplus::Status status) {
switch (status) {
case Gdiplus::Ok: return "Ok";
case Gdiplus::GenericError: return "GenericError";
case Gdiplus::InvalidParameter: return "InvalidParameter";
case Gdiplus::OutOfMemory: return "OutOfMemory";
case Gdiplus::ObjectBusy: return "ObjectBusy";
case Gdiplus::InsufficientBuffer: return "InsufficientBuffer";
case Gdiplus::NotImplemented: return "NotImplemented";
case Gdiplus::Win32Error: return "Win32Error";
case Gdiplus::Aborted: return "Aborted";
case Gdiplus::FileNotFound: return "FileNotFound";
case Gdiplus::ValueOverflow: return "ValueOverflow";
case Gdiplus::AccessDenied: return "AccessDenied";
case Gdiplus::UnknownImageFormat: return "UnknownImageFormat";
case Gdiplus::FontFamilyNotFound: return "FontFamilyNotFound";
case Gdiplus::FontStyleNotFound: return "FontStyleNotFound";
case Gdiplus::NotTrueTypeFont: return "NotTrueTypeFont";
case Gdiplus::UnsupportedGdiplusVersion: return "UnsupportedGdiplusVersion";
case Gdiplus::GdiplusNotInitialized: return "GdiplusNotInitialized";
case Gdiplus::PropertyNotFound: return "PropertyNotFound";
case Gdiplus::PropertyNotSupported: return "PropertyNotSupported";
default: return "Status Type Not Found.";
}
}
使用方法如下:
const std::string statusMsg = statusString(theStatus);
//And then just output it.
std::cout << "Operation returned message: " << statusMsg << '\n';