5 out of 19 rated this helpful - Rate this topic

CDialog::OnInitDialog 

This member function is called in response to the WM_INITDIALOG message.


virtual  BOOL OnInitDialog( );

Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.

This message is sent to the dialog box during the Create, CreateIndirect, or DoModal calls, which occur immediately before the dialog box is displayed.

Override this member function if you need to perform special processing when the dialog box is initialized. In the overridden version, first call the base class OnInitDialog but disregard its return value. You will normally return TRUE from your overridden member function.

Windows calls the OnInitDialog function via the standard global dialog-box procedure common to all Microsoft Foundation Class Library dialog boxes, rather than through your message map, so you do not need a message-map entry for this member function.

/* MyDialog.cpp */
#include "MyDialog.h"

BOOL CMyDialog::OnInitDialog() 
{
   CDialog::OnInitDialog();
   
   // TODO: Add extra initialization here
   m_cMyEdit.SetWindowText("My Name"); // Initialize control values
   m_cMyList.ShowWindow(SW_HIDE);      // Show or hide a control, etc.

   return TRUE;   // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Non-modal dialog - open without stealing focus
The documentation says you can return FALSE only if you explicitly set focus to a control on the dialog box. This is true of modal boxes where something must have input focus: in the case of a modeless dialog it is ok if the previous focus window keeps its focus, in fact if the application is opening a modeless dialog not in response to user input it is the correct thing to do.