Link to home
Start Free TrialLog in
Avatar of jstephe1
jstephe1

asked on

Change color of edit box with dynamic color

I'm writing an application in Visual C++ 6.0 on Windows2000.  I currently have a pushbutton that will invoke the CColorDialog interface.  When the user selects a color, I get the color from the CColorDialog and I want to change the color of an edit in the dialog to that new color.

I know how to change the color of edit boxes in general.  My question is, how do you do it with a dynamic RGB value?

In general you create the brush with CreateSolidBrush() somewhere (probably constructor), then in the OnCtlColor function you do something to the effect:

if ( nCtlColor == CTLCOLOR_STATIC )
{
   int nID = pWnd->GetDlgCtrlID();

   if ( nID == IDC_TEST_EDIT )
   {
     pDC->SetBkColor( RGB(255,0,0) );
     return m_hRedBrush;
   }
}

you need to return a brush somehow....but it's always changing.....any help would be appreciated.

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jstephe1
jstephe1

ASKER

Before I got a response, I somewhat got it working using a variation of what you proposed.  I put in what you proposed and I'm getting the same results.  This is what happens.  I push the button to change the color.  The CColorDialog box comes up, I select a color and press the OK button.  In my code I check the return from the CColorDialog::DoModal to see if it is IDOK.  If the user selects OK, I get the color and call the SetBrushColor.  But the edit box color does not change.   When I click on the edit box, then it changes color.  So there must a be a refresh problem here.  

After I check IDOK, I get the color and SetBrushColor.  Now, I just want to update/refresh that individual edit box.  I tried InvalidateRect like this:

CRect cRect;

CWnd* pWnd = GetDlgItem( IDC_TEST_EDIT );
pWnd->GetWindowRect( cRect );

InvalidateRect( cRect, TRUE );
UpdateWindow();

Like I said, the color does change, but only when I click on that particular edit box.  If I change the color and click on any other edit, the color won't change.  
Before I got a response, I somewhat got it working using a variation of what you proposed.  I put in what you proposed and I'm getting the same results.  This is what happens.  I push the button to change the color.  The CColorDialog box comes up, I select a color and press the OK button.  In my code I check the return from the CColorDialog::DoModal to see if it is IDOK.  If the user selects OK, I get the color and call the SetBrushColor.  But the edit box color does not change.   When I click on the edit box, then it changes color.  So there must a be a refresh problem here.  

After I check IDOK, I get the color and SetBrushColor.  Now, I just want to update/refresh that individual edit box.  I tried InvalidateRect like this:

CRect cRect;

CWnd* pWnd = GetDlgItem( IDC_TEST_EDIT );
pWnd->GetWindowRect( cRect );

InvalidateRect( cRect, TRUE );
UpdateWindow();

Like I said, the color does change, but only when I click on that particular edit box.  If I change the color and click on any other edit, the color won't change.  
I got it to work by calling RedrawWindow after the InvalidateRect.  Thanks for the quick response.