Solved

Attached HWND

Posted on 1998-10-06
2
252 Views
Last Modified: 2013-11-20


Hi, experts
Here a easy question to you, and a difficult one for me.

In the following code
why they say
the dialog has no attched HWND in the constructor



BOOL CDerEditDlg::OnInitDialog()
      // create children on InitDialog
      //  (not in CDerEditDlg constructor since the dialog has
      //    no attached HWND in the constructor)   <------------------here is the question
{
      // This is an example of the _incorrect_ way to create a dialog
      // The following code show you what you should _not_ do:
      //    1) do not use hard coded numbers for coordinates and sizes
      //     (these will break when the font sizes changes and are
      //     hard to edit and maintain).
      //    2) do not put strings in code, they should be in resources.
      //    3) as you can see below the programming steps required
      //     to create controls, pass the correct creation parameters,
      //     and set the appropriate font, is complicated and error prone.
      //    4) localization of the controls would require changes to the
      //     sources for the captions, font, coordinates, and sizes.

      const int yStart = 8;
      const int height = 30;

      CPoint whereLabel(10, yStart);
      CSize sizeLabel(80, 24);

      CPoint whereEdit(90, yStart);
      CSize sizeEdit(140, 24);

      m_static1.Create(_T("Letters:"), WS_VISIBLE | WS_CHILD | SS_LEFT,
            CRect(whereLabel, sizeLabel), this, (UINT)-1);
      m_static1.SetFont(m_pFont);
      whereLabel.y += height;
      [....]
      return FALSE;   // focus set
}
0
Comment
Question by:nightingale
2 Comments
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 1323028
It's the way MFC works.

When you create a C++ object, such as an instance of your CDerEdit dialog, you're creating only the C++ object.  Some memory gets allocated, and the constructor runs.

That _doesn't_ create the actual Windows object. INstead, you need to take the separate step of calling the Create(), or DoModal() or CreateEx() members to get the Windows object associated with the C++ object created.

After you call one of those functions on a dialog box, Windows will send the dialog box window a WM_INITDIALOG message. So, the OnInitDialog() handler is the first place where a dialog object will have a valid m_hWnd after creation.

Similarly, when the dialog window is destroyed, it's destroyed before the C++ object.  The C++ object may realize the dialog is going away and delete itself, but it also may live on for a long time after the actual window is gone.

B ekiM

0
 

Author Comment

by:nightingale
ID: 1323029
Thank you so much,mikeblas
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now