Link to home
Start Free TrialLog in
Avatar of cophi
cophi

asked on

Change FillRect Color

So on my constructor I have a CBrush which is set to the color red.  In the on draw routine I have pdc->FillRect(rcbounds, &wbrush); So when the activex is first drawn it shows up red.  In another event in the activex OnSize, I want to change the color to Blue.  So I changed wbrush to RGB(0, 0, 255);  And then I did Invalidate();  I also did InvalidateControl();  Nothing happened.  The background stayed Red.  Then when the control ended it was painted blue.  Why can't I switch this in any of my events?
Avatar of Dariusz Dziara
Dariusz Dziara
Flag of Poland image

You probabely did it incorrectly (paste your code please).

1. Create variable of type COLORREF

2. Assign value to the color variable

3. In WM_PAINT handler create (and later destroy) brush using color variable.

4. Paint your color.

5. To repaint window you will have to Invalidate() (defines what need to be redrawn) its area & then UpdateWindow() (its actually sends WM_PAINT message to the window).
try to update using CWnd::RedrawWindow() :

RedrawWindow(NULL,NULL,RDW_FRAME | RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);

http://msdn2.microsoft.com/en-US/library/0fdz8ey6.aspx

-MAHESH
class YourClass : ... {
{
private:
  COLORREF m_crColor;
};

void YourClass::OnPaint(...)
{
  CBrush br, *pbrOld;

  br.CreateSolidBrush(m_crColor);
 
  pbrOld = pDC->SelectObject(&br);

  // draw using your brush
  // ...

  pDC->SelectObject(pbrOld);  // deselect your brush
  br.DestroyObject(); // you don't have to do it yet desctructor will do it for you  
}

{
  wnd.Invalidate();
  wnd.UpdateWindow();
}
Avatar of cophi
cophi

ASKER

void CatxCtrl::OnDraw(
                  CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
      if(m_drawFirst) {
            CBrush bbrush(RGB(0, 255, 0));
            pdc->FillRect(rcBounds, &bbrush);
            m_drawFirst = false;
      }
      else {
            CBrush abrush(RGB(0, 0 , 255));
            pdc->FillRect(rcBounds, &abrush);
      }

      DoSuperclassPaint(pdc, rcBounds);
}

So I Set m_drawFirst = true in my constructor.  That works fine.  When it goes to draw again, it goes into the else but it does not 'overwrite' the green.  It should be a completely blue background but its not.  Whats wrong?
The are 2 explainations:

1. m_drawFirst has incorrect value
You can use (at least I hope so) TRACE macro in debug mode to display m_drawFirst value

2. Window is not repainted (cover it with another window & the uncover it - see what will happen)
Have you tried that using RedrawWindow() as I said above instead of invalidating it ?

-MAHESH
"Have you tried that using RedrawWindow() as I said above instead of invalidating it ?"

Why ? Shouldn't it work also ?
Avatar of cophi

ASKER

Yes, I tried redrawwindow, no luck
Do you have somewhere else drawing with GREEN color ?
And in fact I don't understand why you call:

DoSuperclassPaint(pdc, rcBounds);

It can do own drawing - try to comment it out.
ASKER CERTIFIED SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India 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
'B' ?