Solved

Redrawing window to display new message

Posted on 1998-08-25
1
226 Views
Last Modified: 2013-11-19
I wish to repaint my window so that the appropriate
message is sent as soon as a flag is set.
My code follows:
void CTextView::OnDraw(CDC* pDC)
{
      extern int text_out_flag;
      int j;
      CMainDoc* pDoc = GetDocument();

      CRect rect;
      GetClientRect(rect);
      pDC->SetTextAlign(TA_BASELINE | TA_CENTER);
//      pDC->SetTextColor(pDoc->m_colorData);
      pDC->SetBkMode(TRANSPARENT);
      // center in the window
      if (!text_out_flag)
      {
            pDC->TextOut(rect.Width() / 2, rect.Height() / 2,
            (LPCTSTR)pDoc->GetLine(0), pDoc->GetStringSize());
      }
      else //if (text_out_flag) //0824
      {
    CMDIChildWnd *pFrame =
             (CMDIChildWnd*)AfxGetApp()->m_pMainWnd;

      
            pFrame->InvalidateRect(rect, TRUE);//            pDoc->SwitchView();
            //pFrame->ValidateRect(rect);
/*            pFrame->RedrawWindow(rect, NULL, RDW_VALIDATE | RDW_UPDATENOW | RDW_ERASE);*/
            for (j=0; j<2; j++) {
                  pDC->TextOut(130, (j+2) * 30,
                  (LPCTSTR)pDoc->GetLine(j), strlen (pDoc->m_strData[j]) );
            }
      }
}

When my text_out_flag is 0, I want one message and when it is set to 1, I want another message on my View.
I have tried Invalidate(), RedrawWindow(), etc to no avail.
Please advice.
TIA.
vivek
0
Comment
Question by:vmehro
1 Comment
 
LVL 2

Accepted Solution

by:
milenvk earned 50 total points
ID: 1321281
I found some general errors in your code:

1.When text_out_flag is 1 you try to invalidate or redraw the window in the OnDraw() function uncoditionally. In this way you'll end up in an eternal loop. See what happes: if text_out_flag == 1 you invalidate the window, which in turn calls your OnDraw() function which again invalidates the window and so on... Do the invalidation in the place where you change text_out_flag and leave only the drawing functionality in the OnDraw().

2. You invalidate the main frame window, which should invalidate all the children but this will not happen using RedrawWindow() unless you include the RDW_ALLCHILDREN flag.

3. You get the client rectangle of your view and then you use it in the invalidate and redraw functions. The main frame is totally different window and has a rectangle different than the view.

The standard way of handling changes in MFC is when the change occur to call CDocument::UpdateAllViews() function. This will assure proper redraw of all the visible views of the document. And finally in it is a good practice to keep the data in the document. In this way when the data change you can easily handle the change in a document command handler and call CDocument::UpdateAllViews(). In any case avoid having global variables other than theApp since they tend to turn your code into spagetti and they are really against the object oriented idea.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now