Link to home
Start Free TrialLog in
Avatar of joseph-r-thomas
joseph-r-thomas

asked on

can't Invalidate a Rect!

hi...
i have a problem in invalidating...
below is my code

code:
--------------------------------------------------------------------------------
void CEditorView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
      if( lHint == 1 ) {
            //get back the rect
            CRect* pRect = ( CRect* ) pHint;
            InvalidateRect(pRect);            
            return;
      }
      CView::OnUpdate(pSender, lHint, pHint);
}
--------------------------------------------------------------------------------

which are taken from the OnUpdate handler of my View..
the way OnUpdate is called from my DOC is as below :

code:
--------------------------------------------------------------------------------
void CEditorDoc::SetSquareColor(int i, int j, COLORREF color)
{
      ASSERT( i >= 0 && i <= GetGridY() && j >= 0 && j <= GetGridX() );
      m_clrGrid.At(i, j) = color;
      SetModifiedFlag(TRUE);
      CRect pRect( 20, -10, 30, -20 );
      UpdateAllViews( NULL, 1, ( CObject* )&pRect);
}
--------------------------------------------------------------------------------

THE PROBLEM
the problem arrives when i do InvalidateRect, the rect passed in as parameter is not getting invalidated!!
Now, to check if my parameter is wrong i added code in the OnUpdate to draw a rectangle with the same coordinates.
so now the same code above becomes :

code:
--------------------------------------------------------------------------------
void CEditorView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
      if( lHint == 1 ) {
            //get back the rect
            CRect* pRect = ( CRect* ) pHint;
            InvalidateRect(pRect);

            CBrush brush(RGB(255,166,166));
            CBrush* pOldBrush = dc.SelectObject(&brush);
      
            CRect jrtRect(20, -10, 30, -20);
            dc.Rectangle(jrtRect);
            dc.SelectObject(pOldBrush);
            Invalidate();
            return;
      }
      CView::OnUpdate(pSender, lHint, pHint);
}
--------------------------------------------------------------------------------

what i found out that with the above code, a rectange is drawn in the correct place where i wanted to invalidate the rect!!!
but the rectangle apprears and quickly dissapears!!
so now i know that the co-ordinates i supplied witht the invalidateRect were correct...
then why doesnt the area get Invalidated??
any help is deeply appreciated and thank you for your time...
Avatar of Member_2_1001466
Member_2_1001466

Is the rect not updated or is something else drawn above? From the second part of your question I would assume something else is draw over it. So yopu should show the code how you draw.
Avatar of joseph-r-thomas

ASKER

which code would u like??
btw this is on draw
code:
--------------------------------------------------------------------------------
void CEditorView::OnDraw(CDC* dc)
{
      CMemDC pDC(dc);
      OnPrepareDC(pDC);
          CEditorDoc* pDoc = GetDocument();
          ASSERT_VALID(pDoc);
          // TODO: add draw code for native data here - use pDC
           //as the device context to draw to
      
      //
      //Set the mapping mode to LOENGLISH
      //
      pDC->SetMapMode(MM_LOENGLISH);

      //
      //Convert arguments to coordinates to MM_LOENGLISH units.
      //
      CSize szTemp;
      szTemp.cx = pDoc->GetGridX();
      szTemp.cy = pDoc->GetGridY();
      pDC->DPtoLP(&szTemp);

//
      //Draw the squares
      //
      for(int i = 0 ; i < 500 ; i ++) {
            for(int j = 0 ; j < 750 ; j ++) {
                  COLORREF color = pDoc->GetSquareColor(i, j);
                  CBrush brush(color);
                  int x1 = (j * 10) + 10;
                  int y1 = (i * -10) - 10;
                  int x2 = x1 + 10;
                  int y2 = y1 - 10;
                  CRect rect(x1, y1, x2, y2);
                  pDC->FillRect(rect, &brush);
            }
      }
      //
      // then draw the grid lines
      //
      CRect clipBoxRect;
      pDC->GetClipBox(&clipBoxRect);
      for(int x = 10 ; x <= pDoc->GetGridX() ; x += 10) {
            pDC->MoveTo(x, -10);
            pDC->LineTo(x, -pDoc->GetGridY());
      }
      
      for(int y = -10 ; y >= -pDoc->GetGridY() ; y -=10) {
            pDC->MoveTo(10, y);
            pDC->LineTo(pDoc->GetGridX(), y);
      }
}
--------------------------------------------------------------------------------

A comment to this problem:
> rectangle apprears and quickly dissapears!!
The Invalidate() method without parameter (default is: BOOL bErase = TRUE) erases the background and also the painted rectangle. What's why, that the rectangle only flashes.

Unfortunalely I don't know, why InvalidateRect() ( in your first OnUpdate() )  doesn't work. May be it helps, id you don't erase the background:  InvalidateRect(pRect, FALSE);




Now I read your new comment. What happens, if you don't use the CMemDC and instead the passed CDC ?
I would guess that in the first case you can't see the flashing because only a small rectangle is drawn. In the second case more has to be redrawn which allows to see the rectangle for a short time.

The problems comes from your call to OnPrepareDC in OnDraw. The framework has called OnPrepareDC before a call to OnDraw (). You should (can) use a memoryDC in the OnDraw without this call. But don't forget to copy the information from the memoryDC into the deviceDC that the screen gets updated as well.
This is related to another Q of you.

You need a seperate function like UpdateMemDC () and a memoryDC which is a member of your CEditorView. In that function you change the memoryDC whenever the document has changed. In OnDraw () you just need to BitBlt () your memoryDC into the DC provided by the framework.

A general advice for you. Try to solve ONE problem at a time. You have several questions here which are all connected and answering one will answer the others as well. When that problem is solved and others remain ask for help on them.

i have none of te questions solved!!!!
what ever answer u gave me is kinda unrelated to my qeustion!!!!
maybe its cause u havent looke at my proj....
is it possible i could mail u my prj??its quite small...
my address is
extremedotnet@yahoo.com.sg
pls drop me a line so that i could send u my proj..then u can have a look at ee the whole thing...i think i failed to state my question clearly....if possible pls take a look at my proj....
hope to hear from u...tks...
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1001466
Member_2_1001466

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