Solved

Attached HWND

Posted on 1998-10-06
2
255 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 48
method notes when mouse over in eclipse 5 83
format the code in java 6 86
post4 challenge 28 93
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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