Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I've a problem with the graphics of my mfc MDI software.
Only after a bit hour of running, something goes wrong. the parameter CDC of OnDraw function of an MDIChild in a CView derived class goes crazy, because it draw on desktop not in the frame and it distorce all software graphics: moving menu up and down and many flickerings.
I checked one houndred of times, I verified that I never deleted any HDC retrieved with getDC and I think that I release correctly all graphics device.

I'm developing with VC6.

In your opinion of what depends this behavior?

Thank you in advance.
Posted
Updated 26-Sep-10 2:28am
v2

Make sure the HDC you're using isn't null. If it is, it will draw to the desktop.

I haven't done MFC in a long time, but it's worth investigating. Try sending debug statements to the output window that shows the value of approriate variables just before doing the drawing. You'll probably find your problem.
 
Share this answer
 
you can put your code here if you like, then others can help you.
 
Share this answer
 
Ok, i will put code to check if HDC become null.

this is the code, i cut the lees important code because it's bit long

void CNavionicsView::OnDraw(CDC* pDC)

{
    //TRACE("OnDraw\n");
    CNavionicsDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    int iwkViewOptionApplied=0;
    try
    {
        if (m_CEChartViewAttached !=NULL)
        {
            ...

            int iViewSizeX =m_CEChartViewAttached->GetViewSizeX(); 
            int iViewSizeY =m_CEChartViewAttached->GetViewSizeY();

            if (m_ToBeRedraw == true)
            {            
                int iSysMetricsX = GetSystemMetrics(SM_CXSCREEN);
                int iSysMetricsY = GetSystemMetrics(SM_CYSCREEN);
                if( theApp.GetTraceLevel() & (1 << (TRACE_VIEW-1)) ){
                    CString s; s.Format("CNavionicsView::OnDraw: iSysMetricsX=%d, iSysMetricsY=%d\n",
                        iSysMetricsX, iSysMetricsY);
                    OutputDebugString(s);
                }         

                m_MemDC.SelectObject(&m_Brush);
                m_MemDC.PatBlt(0,0,iSysMetricsX,iSysMetricsY,PATCOPY);
                
                BeginWaitCursor(); // display the hourglass cursor

                TRACE("OnDraw: m_CEChartViewAttached->Draw...\n");
                m_CEChartViewAttached->Draw(m_MemDC.m_hDC);
                TRACE("OnDraw: m_CEChartViewAttached->Draw: Done!\n");

                EndWaitCursor(); // display the hourglass cursor

                if( theApp.GetTraceLevel() & (1 << (TRACE_VIEW-1)) ){
                    CString s; s.Format("CNavionicsView::OnDraw: iViewSizeX=%d, iViewSizeY=%d\n",
                        iViewSizeX, iViewSizeY);
                    OutputDebugString(s);
                }


                //Devo disegnare i marker su m_MemDC così ogni copia conterrà già i marker 
                //e rotte così non dovrò più ridisegnarli.
                CNChartView::DrawMarkers(&m_MemDC);
                CNChartView::DrawRoutes(&m_MemDC);
                CNChartView::DrawAreas(&m_MemDC);
                if(theApp.m_bDrawReticolo)
                    CNChartView::DrawParalelMeridian(&m_MemDC);

                m_MemDC2.BitBlt(0,0,iViewSizeX, iViewSizeY, &m_MemDC, 0,0,SRCCOPY  );
                CNChartView::OnDraw(&m_MemDC2);

                if(m_bViewNocturne){
                    Graphics gx(m_MemDC2);//pDC->m_hDC);
                    SolidBrush blackBrush(Color(180,0,0,0));
                    gx.FillRectangle(&blackBrush,0,0,iViewSizeX,iViewSizeY);
                }
                
                pDC->AssertValid();
                pDC->BitBlt(0,0,iViewSizeX, iViewSizeY, &m_MemDC2, 0,0,SRCCOPY  );

            
#endif
                m_ToBeRedraw = false;
            }
            else
            {
                if( theApp.GetTraceLevel() & (1 << (TRACE_VIEW-1)) ){
                    CString s; s.Format("CNavionicsView::OnDraw: BitBlt\n");
                    OutputDebugString(s);
                }

                m_MemDC2.BitBlt(0,0,iViewSizeX, iViewSizeY, &m_MemDC, 0,0,SRCCOPY  );
                CNChartView::OnDraw(&m_MemDC2);

                if(m_bViewNocturne ){
              
                    Graphics gx(m_MemDC2);
                    SolidBrush blackBrush(Color(180,0,0,0));
                    gx.FillRectangle(&blackBrush,0,0,iViewSizeX,iViewSizeY);
                }                

                pDC->BitBlt(0,0,
                            iViewSizeX, //m_CEChartViewAttached->GetViewSizeX(),
                            iViewSizeY,    //m_CEChartViewAttached->GetViewSizeY(), 
                                &m_MemDC2,0,0,SRCCOPY);
            }

        }
        
    }
    //catch the exceptions here
    catch(CNavionicsException CNEwkNavionicsException)
    {
        //check if its a fatal error
        int iwkErrorNumber
            = CNEwkNavionicsException.GetErrorNumber();
        if(iwkErrorNumber>=9000  && iwkErrorNumber<=9900)
        {
            //fatal error, therefore display
            string swkErrorText;
            CNEwkNavionicsException.MessageFormatted(swkErrorText, false);
            MessageBox((swkErrorText.c_str()), "Fatal Error", MB_OK || MB_ICONEXCLAMATION);
            abort(); //exit
        }
        else
        {
            //for debugging display warning
            int iwkErrorNumber
                = CNEwkNavionicsException.GetErrorNumber();
            //fatal error, therefore display
            string swkErrorText;
            CNEwkNavionicsException.MessageFormatted(swkErrorText, false);
        }
    }

...


m_MemDC and m_MemDC2 are correctly initialized in constructor of class.

This on OnDraw functions is called from MFC framework
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900