• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • 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
 
Dariusz DziaraProgrammerCommented:
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
 
Dariusz DziaraProgrammerCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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
 
Dariusz DziaraProgrammerCommented:
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
 
Dariusz DziaraProgrammerCommented:
"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
 
Dariusz DziaraProgrammerCommented:
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
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

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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