C++
  Home arrow C++ arrow Page 4 - Using MFC in C++ Part 3: Dialog Boxes
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Using MFC in C++ Part 3: Dialog Boxes
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 34
    2001-12-08

    Table of Contents:
  • Using MFC in C++ Part 3: Dialog Boxes
  • Creating a modal dialog box
  • Creating a modal dialog box (contd.)
  • Encapsulating our dialog resource
  • Creating a modeless dialog box
  • The OnInitDialog() function
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using MFC in C++ Part 3: Dialog Boxes - Encapsulating our dialog resource


    (Page 4 of 7 )

    Notice how I have been referring to our dialog as a “resource”. This is because we haven’t actually created a class that can use our dialog resource yet. For our dialog resource to become part of our application, we must derive a class from the MFC CDialog class, just like we have derived classes from CFrameWnd and CWinApp.

    Once we have derived a class from CDialog, we will reference our dialog resource and be able to display it as part of our application. Lets start by creating our new class, which will be derived from the MFC class, CDialog.

    Double-click on main.h, and add the following code at the end of the file:

    class CTestDialog : public CDialog

    {

    public:

    CTestDialog(char* DialogName, CWnd* Owner) :

    CDialog(DialogName, Owner) {}

    DECLARE_MESSAGE_MAP()

    };


    We are declaring a new class named CTestDialog, which is derived publicly from CDialog, meaning that our new class inherits every method and variable declared as either public or protected in its CDialog base class.

    We have named our new class CTestDialog. This is simply the text id of our dialog resource, prefixed with a “C”, for class. This is the standard C++ naming convention and although optional, it should be adhered to.

    Our new CTestDialog class contains a constructor, which is shown below:

    CTestDialog(char* DialogName, CWnd* Owner) : CDialog(DialogName, Owner) {}

    This is an empty constructor, which accepts the name of a dialog resource to create, as well as a pointer to a CWnd* object. A CWnd* object is simply a pointer to a window, such as our main window (which is a CFrameWnd object, derived from the CWnd base class). The constructor simply passes these two arguments to its CDialog() base constructor, which handles the creation and memory allocation of our dialog box.

    Lastly, we have the DECLARE_MESSAGE_MAP() macro, which allows our CTestDialog class to accept messages from windows.

    Next, we need to add the implementation code for our CTestDialog class to the bottom of main.cpp, just before the “CApp App;” line:

    BEGIN_MESSAGE_MAP(CTestDialog, CDialog)

    // Message map macros for CTestDialog will go here

    END_MESSAGE_MAP()


    As described in previous articles, our BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() macros allow us to tell windows which messages our application will handle. The first parameter is the class that we are defining message handlers for, CTestDialog. The second parameter is the base class of the first parameter, CDialog.

    The last step is to actually create the code that will load an instance of our CTestDialog class and display the dialog box. Change the CMainWin::OnShowDialog() function in main.cpp to look like this:

    afx_msg void CMainWin::OnShowDialog()

    {

    CTestDialog testDlg("TestDialog", this);

    testDlg.DoModal();

    }


    We simply create a new instance of our CTestDialog class, passing the id of our dialog resource, “TestDialog” as the first parameter, and “this” as the second parameter. Because the OnShowDialog function is part of the CMainWin class (which is derived from the CFrameWnd MFC class), “this” returns a pointer to the current class, which is CMainWin. CMainWin is derived from the CFrameWnd class, which has CWnd as its base class, which is what the second parameter has to be.

    Compile and run your application by pressing Ctrl+F5. When your application loads, either select the “Show Dialog” menu option, or press Ctrl+S. Your new dialog class will be instantiated and displayed, as shown below:

    Our new, empty modal dialog box

    Notice that you can’t click back to the main window without closing the modal dialog box first. There will be some cases where you will want to have access to the dialog box AND the main window. This is where modeless dialog boxes will come in handy, so let’s take a look at them now.

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT