Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Problem with COLORREF type


I am trying to give the user the option to change the background colour of a List Box and store the selected colour in the ini file, so that it will re-appear when they logon on again.  The code I am using is as follows:

void CPatientView::OnTextCol()
            // TODO: Add your control notification handler code here
CPatientDoc* pDoc = GetDocument();
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
      COLORREF color = dlg.GetColor();
      //CString Temp;
      //Temp.Format("%s", (LPCTSTR)color);
      OUR_APP->WriteProfileString( "Colours", "Background", LPCTSTR(color));


The main problem is that the entry written in the ini file is always null, whichever colour the user selects.

As far as I am aware the COLORREF type is a RGB value, can I store this in anyway in the INI file.

I very much look forward to a reply

Thanks in advance.

  • 2
  • 2
1 Solution
COLORREF is a 32-bit unsigned integer so simply casting it to a string cannot be done.

In your source code you have already the correct thought:

>>    //CString Temp;                                 // <-- right
>>    //Temp.Format("%s", (LPCTSTR)color);  // <-- wrong(cast from DWORD to string)
>>    OUR_APP->WriteProfileString( "Colours", "Background", LPCTSTR(color)); //<-- wrong

CString temp;

To retrieve it again:
CString temp=OUR_APP->GetProfileString("Colours","Background");
color=(COLORREF)atol(temp); //atol() returns the string to int value

Don't give me credit for this, but you're going about it the hard way!!!

How about just...

WriteProfileInt( "Colours", "Background", color );


color = GetProfileInt( "Colours", "Background", RGB( default ));

Sorry you are right. I don't have to do much with these WriteProfile... things, so i didn't see that
there is a function for writing an integer.
I think you should get the points :(
Well, I feel guilty for answering an answered question!


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now