Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS 2008, MFC: add OnInitDialog - how?

This is my first MFC application away from VC6, and I feel a little bit stupid:

How do I add the OnInitDialog handler? (I know how to add it manually, but that's a pain in the long run).

double-clicking the dialog - nothing. right click the dialog - "add event handler" is disabled. Properties - Messages has "normal" messages, but not WM_INITDIALOG Properties - Events only holds notifications from contained controls right-clicking in class view - "Add.." only has functions and variables

scratches head

[edit] d'oh - it's a virtual function in MFC, but still...

like image 988
peterchen Avatar asked Jun 07 '09 10:06

peterchen


People also ask

How do I override OnInitDialog?

To override them, you declare an overriding function in your derived dialog class using the MFC Class Wizard. OnInitDialog is called just before the dialog box is displayed. You must call the default OnInitDialog handler from your override — usually as the first action in the handler.

What is OnInitDialog in MFC?

For a modeless dialog box, OnInitDialog is called when Create is called. You typically override OnInitDialog to initialize the dialog box's controls, such as setting the initial text of an edit box. You must call the OnInitDialog member function of the base class, CDialog , from your OnInitDialog override.

Does MFC do data exchange?

The MFC framework provides an efficient mechanism for transferring and validating data in a dialog box through the DDX and DDV routines. Dialog Data Exchange (DDX) is an easy way to initialize the controls in a dialog box and gather data input by the user.


3 Answers

Don't feel stupid, it took me forever to figure this out when I first moved from VC6 to VS2008!

Anyway, and also for my own reference, here are the complete steps for adding a dialog box and overriding the OnInitDialog method:

  1. Select Resource View and expand the .rc file.
  2. Right-click the Dialog entry in the tree view and select Insert Dialog.
  3. Select the Properties window. (Note: If you don't see this window, select the View > Other Windows > Properties Window menu item.)
  4. In the Properties window, set the ID for the dialog, e.g., IDD_MYDIALOG.
  5. Right click the dialog in the resource editor and select Add Class. Note: if you have IE8 installed, this may produce an Internet Explorer Script Error. See this blog post for details on how to correct this.
  6. In the MFC Class Wizard dialog box, enter the class name, e.g., CMyDialog, select CDialog as the base class, then press Finish. This will create files named MyDialog.cpp and MyDialog.h and add them to your project.

To override the OnInitDialog method in the CMyDialog class:

  1. Open the file MyDialog.h.
  2. Select the Properties window.
  3. Place the cursor on the line that reads class CMyDialog : public CDialog. (The top of the Properties window should show "CMyDialog VCCodeClass" - this is important, because the Properties window is highly context sensitive, and you get different options depending on the location of the cursor in the editor.)
  4. Click the Overrides icon in the Properties window.
  5. Scroll down the properties window to find OnInitDialog.
  6. Click the down arrow against this entry and select <Add> OnInitDialog. This should create your CMyDialog::OnInitDialog function.

I hope this helps!

like image 200
ChrisN Avatar answered Oct 06 '22 06:10

ChrisN


It appears that you don't even have to open the header file and mess around placing the cursor... After creatingthe class, select the class in the ClassView pane and you can change overrides/messages/events in the Property pane as described above.

like image 30
wayne Avatar answered Oct 06 '22 05:10

wayne


Open Class Wizard, then go to Virtual Functions Tab. Double Click on which functions you want to override

like image 44
Viet Tang Avatar answered Oct 06 '22 05:10

Viet Tang