Hello,

I have a question. I work in visual c++ in a document/view arhitecture (SDI application) and i draw some stuff with some opengl functions (cubes, cylinders) but when i hit the "New" menu from the File menu it doesn't erase or clear my drawing I don't know why. Can you help me how to ovewrite eventually this : OnFileNew function, and where should i put it? In the CView class or in CMainFrame?
What should i write in it to erase my background after i draw something and to provide a clear canvas ?

Thank you in advance for your help.

Dani AI

Generated

As described, in an MFC SDI app the model (document) should own the scene state and the view should only render it. Rather than putting a "clear" handler in CMainFrame or the view, override the document initialization so a New document resets your scene and then ask the view(s) to repaint. ’s point about forcing a repaint is correct; the pattern below keeps responsibilities separated and avoids visual leftovers.

Example: reset model state in the document (override of OnNewDocument), then notify views.

BOOL CMyDoc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
        return FALSE;

    // reset model data
    m_objects.clear();
    m_isSceneEmpty = true;

    // tell views to refresh
    UpdateAllViews(NULL);

    SetModifiedFlag(FALSE);
    return TRUE;
}

In the view, make the GL context current, clear the buffers and draw only what the document says exists. If you maintain GL resources (textures, lists, VBOs), delete them from the view where the GL context is valid (OnDestroy or a message handler), or post a message from the document so the view does cleanup when ready.

void CMyView::OnDraw(CDC* pDC)
{
    wglMakeCurrent(pDC->GetSafeHdc(), m_hGLRC);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    CMyDoc* pDoc = GetDocument();
    if (pDoc && !pDoc->m_objects.empty())
        DrawScene(pDoc->m_objects);

    SwapBuffers(pDC->GetSafeHdc());
    wglMakeCurrent(NULL, NULL);
}

Quick tips: always call the base CDocument::OnNewDocument, avoid calling GL functions in the document unless the context is current (prefer the view for GL cleanup), and use UpdateAllViews so MFC routes redraws correctly. See the MFC notes on document initialization and view updates (CDocument::OnNewDocument / UpdateAllViews) and the OpenGL buffer-clear reference (glClear).

Recommended Answers

All 2 Replies

Hi
Use Invalidate() function to erase the entire client area. and it also call WM_PAINT message to redraw the client area void Invalidate( BOOL bErase = TRUE );

I did what you said and it works. Thank you very much:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.