Practical Learning Logo

Calling a Dialog Box From an SDI



A Single Document Interface (SDI) is the type of application that presents a frame-based window equipped with a title bar, a menu, and thick borders. In most cases the one-frame window is enough to support the application. The advantage of creating an SDI application is the full support of the Document/View architecture.

To provide support and various options to the frame, you can add other dependent windows such as dialog boxes. When such a dialog box is needed, you can provide a menu item that the user would use to display the dialog box. For example, if you create an SDI using the wizard, it would be equipped with an About Box and the main menu would allow the user to open that dialog box. In the same way, you can add as many dialog boxes as you judge necessary to your SDI application and provide a way for the user to display them.

To display a dialog from an SDI, the easiest and most usual way consists of creating a menu item on the main menu. When implementing the event of the menu item, as normally done in C++, in the source file of the class that would call it, include the header file of the dialog box. In the event that calls the dialog box, first declare a variable based on the name of the class of the dialog box. Then call the CDialog::DoModal() method of the dialog box that is being called.

Practical Learning Practical Learning: Calling a Dialog Box From an SDI

  1. Start Microsoft Visual C++ .NET or MS Visual Studio .NET
  2. Create a new MFC Application named SDI1
  3. Create it as Single Document
  4. To add a dialog box, on the main menu, click Project -> Add Resource...
  5. In the Add Resource dialog box, double-click Dialog
  6. Change the ID of the new dialog to IDD_GENERIC_DLG and its Caption to Dependent
  7. Right-click the dialog box and click Add Class...
  8. Set the Class Name to CDependentDlg and base it on CDialog
  9. Click Finish
  10. From the Class View, double-click the IDR_MAINFRAME menu
  11. On the right side of Help, click Type Here and type &Tools
  12. Click the box under Tools and type &Dependents... and press Enter
  13. Click and drag the Tools menu category to the left to position it between View and Help
  14. Right-click the Dependents menu item and click Add Event Handler...
  15. Set the Class List to the view class
  16. Click Add and Edit
  17. Implement the event as follows:
    // SDI1View.cpp : implementation of the CSDI1View class
    #include "stdafx.h"
    #include "SDI1.h"
    #include "SDI1Doc.h"
    #include "SDI1View.h"
    #include ".\sdi1view.h"
    #include "DependentDlg.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    . . . No Change
    // CSDI1View message handlers
    void CSDI1View::OnToolsDependents()
    	// TODO: Add your command handler code here
    	CDependentDlg Dlg;
  18. Test the application

Home Copyright © 2002-2006 FunctionX, Inc.