Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

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?
0
cophi
Asked:
cophi
  • 5
  • 4
  • 2
1 Solution
 
mrblueCommented:
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).
0
 
mahesh1402Commented:
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
0
 
mrblueCommented:
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();
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cophiAuthor Commented:
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?
0
 
mrblueCommented:
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)
0
 
mahesh1402Commented:
Have you tried that using RedrawWindow() as I said above instead of invalidating it ?

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

Why ? Shouldn't it work also ?
0
 
cophiAuthor Commented:
Yes, I tried redrawwindow, no luck
0
 
mrblueCommented:
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.
0
 
mahesh1402Commented:
cophie here also have you tried to use pDC->FillSolidRect() OnEraseBackground with return TRUE ?

Add handler for WM_ERASEBKGND..put following code :

void CatxCtrl::OnEraseBkgnd(CDC* pDC)
{
      // TODO: Add your message handler code here and/or call default

      CRect rect;
                GetClientRect(&rect);

      pDC->FillSolidRect( rect,RGB(255,255,255) ); // fill with FillSolidRect WHITE color
      return true;  // return true
}

-MAHESH

0
 
mahesh1402Commented:
'B' ?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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