如何从 MFC 中的 COLORREF 中提取 RGB 值?

How to extract RGB value from COLORREF in MFC?

实际上,我正在尝试从 COLORREF 中提取 RGB 值,但没有获得 0 到 255 范围内的值。取而代之的是,我总是得到一些字符串。 例如:0000017153665268 我使用了以下方法:

    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 << ",";

请帮帮我。

问题在于:

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

它打印基于 wchar_tCString 到基于 char 的流。

wchar_t* 不会作为字符串打印到 basic_ostream<char>.

最简单的修复方法可能是完全避免 CString

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

或者甚至:

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