C++
  Home arrow C++ arrow Page 2 - 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  
Moblin 
JMSL Numerical Library 
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 - Creating a modal dialog box


    (Page 2 of 7 )

    Modal dialog boxes are a common way to allow users of your application to interact with it. They are modal, which means that they remain in-focus during the whole time that they exist, and any other part of your application, such as its main window or another dialog box cannot be interacted with until the modal dialog box is closed first.

    Modal dialog boxes are ideal when you need to get some sort of information from a user before allowing them to continue. Take for example, if you were developing a database application. The first time that the user loaded your application, they would need to enter some details about their database and the user credentials needed to connect to it. Using a modal dialog box, we would design our application so that all of the information required in the dialog box MUST be completed before the user can continue, or even close the dialog box.

    To create a modal dialog box, we must first create the dialog box as part of a resource file. As described in article 2, you can create add a new resource file to your project by clicking on the File menu, selecting the New option, and then selecting either a resource file (To create the resource components using point-and-click), or a text file with a .rc extension (To create the resource components manually, using a resource script). We will create our modal dialog box manually.

    We will start by creating the framework of our application. Create a new empty Win32 application named MyDialog. Add a new class to MyDialog named main. This will add two files to your project: main.h and main.cpp. Next, use the File…New menu option to add a text file to your project. Also, add a single header file, ids.h. Name the text file mydialog.rc.

    Fill main.h with the following code:

    class CMainWin : public CFrameWnd

    {

    public:

    CMainWin();

    // Show dialog function

    afx_msg void OnShowDialog();

    DECLARE_MESSAGE_MAP()

    };

    class CApp : public CWinApp

    {

    public:

    BOOL InitInstance();

    };


    Fill main.cpp with the following code:

    #include <afxwin.h>

    #include "main.h"

    #include "ids.h"

    CMainWin::CMainWin()

    {

    Create(NULL, "My Dialog Application", WS_OVERLAPPEDWINDOW,

    rectDefault, NULL, "MainMenu");

    if(!LoadAccelTable("MainKeys"))

    MessageBox("Couldn't load accelerator table");

    }

    BOOL CApp::InitInstance()

    {

    m_pMainWnd = new CMainWin;

    m_pMainWnd->ShowWindow(m_nCmdShow);

    m_pMainWnd->UpdateWindow();

    return TRUE;

    }

    BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)

    ON_COMMAND(IDM_SHOWDIALOG, OnShowDialog)

    END_MESSAGE_MAP()

    afx_msg void CMainWin::OnShowDialog()

    {

    }

    CApp App;


    Next, we will refresh our memory on menus from the second article, by adding one to our application. Double click on mydialog.rc and enter the following code:

    #include <afxres.h>

    #include "ids.h"

    MainMenu MENU

    {

    POPUP "&Dialog"

    {

    MENUITEM "&Show Dialog\tCtrl+S", IDM_SHOWDIALOG

    }

    }

    MainKeys ACCELERATORS

    {

    "s", IDM_SHOWDIALOG, CONTROL, VIRTKEY

    }


    Lastly, enter the following code into ids.h:

    #ifndef _IDS_H__

    #define _IDS_H__

    #define IDM_SHOWDIALOG 101

    #endif


    If any of this code is unfamiliar to you, don’t worry. It’s all explained in article 1 and article 2 of this series. We have just created a main window, with a menu. The menu has one popup, with one item, “Show Dialog”. We have also created an accelerator table, with one key, Ctrl+S, which is linked to the same function as the “Show Dialog” menu item.

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - 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
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






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