如何将COLORREF变量值如RGB(0,0,0)转换为CString类型的值?
反向又如何转化

解决方案 »

  1.   

    我想在ini文件中写入一个颜色值,应该用什么样的方式?
      

  2.   

    写入三个long,用逗号隔开好了。
      

  3.   

    // 写入
    void LIni::WriteColRef(CString strEntry, COLORREF cr, LPCSTR strSection)
    {
        CString strBuffer;
        strBuffer.Format("RGB(%d,%d,%d)",GetRValue(cr), GetGValue(cr), GetBValue(cr));
        WritePrivateProfileString(m_strSection, strEntry, strBuffer, m_strIniFile);
    }
    // 读取
    COLORREF LIni::GetColRef(CString strEntry, COLORREF crDefault, LPCSTR strSection)
    {
        int temp[3]={GetRValue(crDefault),GetGValue(crDefault),GetBValue(crDefault)};    CString strDefault;
        strDefault.Format("RGB(%hd,%hd,%hd)",temp[0],temp[1],temp[2]);    // 注:GetString 用于从 INI 文件中读出文本数据
        CString strColRef = GetString(strEntry,strDefault,strSection);
        sscanf(strColRef,"RGB(%d,%d,%d)", temp, temp+1, temp+2);    return RGB(temp[0],temp[1],temp[2]);
    }
      

  4.   

    先分别用GetRvalue,GetGvalue,GetBvalue得到颜色的分量值,然后用
    strR.Format("%d|",m_R);//m_R,m_G,m_B分别是颜色的分量值
    strG.Format("%d|",m_G);
    strB.Format("%d|",m_B);
    str = strR + strG + strB ;//把这个字符串写到ini文件
    所以得到字符串的形式为:m_R|m_G|m_B...
    当要写入时分析字符串就是了,再用函数atoi()把字符串转换成数字就可以了。