Solved

Redrawing window to display new message

Posted on 1998-08-25
1
233 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 55
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 666
only14 challenge 19 76
sumHeights  challenge 17 70
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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