Re: OnDestroy

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 02/20/05


Date: Sat, 19 Feb 2005 23:49:07 -0500

No. There is no DC, so there is nothing to deselect from.

It is an excellent place to delete the objects, but only if you do not already have them
set up as objects in the window class.

For example, suppose I have a class

class CMyDialog : public CDialog {
     protected:
             CFont somefont;
             CFont anotherfont;
             CBrush RedBrush;
             CBrush BlueBrush;
             CPen DottedGreenPen;
   ... lots of other stuff
};

Then when the destructor for the class is called, the objects will be destroyed.

Why waste time with a GetDC()? It does nothing, other than generate a totally useless DC
that serves no useful purpose.

There is no need to call GetSafeHWnd() because you know that when you are in the OnDestroy
handler, you have a valid window handler.

The CWnd object is being destroyed. That implicitly removes it from the screen. The
Microsoft description is pretty vague.

But there is no reason to destroy the GDI objects at this point. They will be destroyed
with the CDialog object. So there's nothing for you to do.
                                joe

On Fri, 18 Feb 2005 20:35:26 -0500, "Hanna-Barbera" <NULL@excite.com> wrote:

>When we have some GDI object created when the programming was running, and
>the window is now beeing closed, is this function a good place to deselect
>from the DC and destroy them?
>
>Does the window still exist when this function is called?
>
>I called GetSafeHWnd() in a CDialog class and GetDC() before and after the
>call to
>CDialog::OnDestroy();
>
>and they always return valid handles.
>
>MSDN says
>
>"The framework calls this member function to inform the CWnd object that it
>is being destroyed. OnDestroy is called after the CWnd object is removed
>from the screen. "
>
>What do they mean by beeing removed from the screen?
>
>
>
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • OnDestroy
    ... the window is now beeing closed, is this function a good place to deselect ... from the DC and destroy them? ... Does the window still exist when this function is called? ... "The framework calls this member function to inform the CWnd object that it ...
    (microsoft.public.vc.mfc)
  • Re: Startup Button
    ... > correct exit value of -2. ... Otherwise - why would you want to kill your mainwindow? ... The only program that I have coded in a destroy after a certain time has ... been an alert window. ...
    (comp.lang.perl.tk)
  • Re: MDI as SDI
    ... > I decided to "fake" SDI by handling New, Open, Close, etc. such that only ... > // there are cases where destroying the documents may destroy the ... > // main window of the application. ... because the pointer is not NULL ...
    (microsoft.public.vc.mfc)
  • Re: CWindow::DestroyWindow
    ... destroy the object itself. ... The window AddRef-s the object ... e.g. it acts as another client to your object. ... my stack pointer is still at ProcessWindowProc. ...
    (microsoft.public.vc.atl)
  • gtk+-2.0, GtkEntry - set cursor problems.
    ... static gboolean delete_event(GtkWidget *widget, ... * GTK will emit the "destroy" signal. ... you don't want the window to be destroyed. ...
    (comp.lang.c)