简体   繁体   中英

How to extract RGB value from COLORREF in MFC?

Actually i am trying to extract RGB value from COLORREF but not getting Value in range of 0 to 255. Instead of that i am getting some string always. eg:0000017153665268 i used below method:

    COLORREF obj_Colorref = obj_CColorDialog.GetColor();
    RGBTRIPLE rgb;
    rgb.rgbtRed = GetRValue(obj_Colorref);
    rgb.rgbtGreen = GetGValue(obj_Colorref);
    rgb.rgbtBlue = GetBValue(obj_Colorref);

    CString sRed;
    CString sGreen;
    CString sBlue;
    sRed.Format(L"%d", rgb.rgbtRed);
    sGreen.Format(L"%d", rgb.rgbtGreen);
    sBlue.Format(L"%d", rgb.rgbtBlue);
    std::ofstream file;
    file.open("..//Projects//Ribbon//x64//color.txt");
    file << "#"<<"\n"<<"#"<< sRed << ",";
    file << sGreen << ",";
    file << sBlue << ",";

please help me out.

The problem is with this:

file << "#"<<"\n"<<"#"<< sRed << ",";
file << sGreen << ",";
file << sBlue << ",";

It prints wchar_t based CString to char -based stream.

wchar_t* does not print as string into basic_ostream<char> .

Easiest way to fix is probably to avoid CString altogether:

file << std::to_string((int)rgb.rgbtBlue) << ",";

Or maybe even:

file << (int)rgb.rgbtBlue << ",";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM