开发者

How to convert GDI+ Status to string?

Actually, the subject. I haven't found any standard way to convert GDI+ Status (e开发者_高级运维rror status returned by GDI+ methods) to string, something like FormatMessage()


If you want to convert labels in GDI+ Status into string, then the simplest thing you can do is this:

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!

Or if you want more descriptive message, then this:

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
};

Since there are only 22 labels in the Status enum, creating a StatusMsgMap in the above way would not be much task, in my opinion. 5 minute is more than enough!


Here's something I wrote that uses hardcoded values.

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.";
    }
}

Use as follows:

const std::string statusMsg = statusString(theStatus);
//And then just output it.
std::cout << "Operation returned message: " << statusMsg << '\n';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜