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

There are two separate things happening here that cause the behavior you see: (1) File->New is handled at the document level (so you need to reset your model/state there), and (2) OpenGL clearing only works when a valid GL context is current and you present the cleared buffer. was right that glClear is the right API, and is right that OnNewDocument() is the place to reset state — but you must also force the view to repaint with the GL context active.

Minimal, practical recipe:

  1. Reset the document/model in CDocument::OnNewDocument() and tell views to repaint:

    BOOL CMyDoc::OnNewDocument()
    {
     if (!CDocument::OnNewDocument()) return FALSE;
    
     // reset your scene data / flags here
     m_scene.Clear();
     m_haveScene = FALSE;
    
     // force view(s) to redraw
     UpdateAllViews(NULL);
     SetModifiedFlag(FALSE);
     return TRUE;
    }
  2. Make your view paint do the actual GL clear/draw. Ensure the GL context is current, clear both color and depth if you use depth testing, and SwapBuffers if you use double buffering:

    void CMyView::OnPaint()
    {
     CPaintDC dc(this);
     wglMakeCurrent(dc.m_hDC, m_hglrc);
    
     glViewport(0,0,width,height);
     glClearColor(0,0,0,1);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
     if (GetDocument()->HasScene()) RenderScene();
    
     SwapBuffers(dc.m_hDC);
     wglMakeCurrent(NULL, NULL);
    }
  3. Prevent GDI background erases (they do not affect GL content): override OnEraseBkgnd and return TRUE so Windows does not fill the window with GDI before GL paints.

Troubleshooting tips:

  • If glClear seems to do nothing, verify wglMakeCurrent succeeds and glGetError() returns GL_NO_ERROR.
  • If using double buffering, you must SwapBuffers to show the cleared buffer.
  • If your renderer runs on a timer/idle, stop it or check the document flag so the view paints only what you want after New.
  • Posting/forcing WM_ERASEBKGND from OnNewDocument is not useful for OpenGL; GL clearing must happen in the GL context.

This approach keeps document logic (reset state) in the document and GL painting (actual glClear/SwapBuffers) in the view, which matches MFC routing and reliably yields a blank canvas after File->New.

Recommended Answers

All 5 Replies

Have you used glClear()?

Yes, i tried it, and it doesn't work, i don't know why.
I thought Visual C++ does this by itself like for the Exit function from the File menu, but it seems i have to overwrite it.
I tried to write in OnFileNew : GetParent()->PostMessage(WM_ERASEBKGND) in order to post the erase background message but still no improvement.

Well, that's all I know... sorry.

maybe you should read

when i hit the "New" menu from the File menu it doesn't erase or clear my drawing I don't know why.

A bit difficult to answer because not knowing how your drawing code is setup, so I guess that the drawing code is within the view class/file i.e. the document class is unrelated to the drawing procedure(?). Now when you hit the File/New, MFC calls your document's OnNewDocument() handler, which returns TRUE and the drawing code continues uninterrupted/unchanged.

You might add code to the OnNewDocument(), which tells the view's drawing code to do something else (i.e. blank out the view) in case it detects that the File/New has been invoked. E.g. add a BOOL member variable to the doc class and check e.g. in view's OnDraw() handler whether to draw or not.

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.