Practical Learning Logo

Calling a Dialog Box From an SDI

 

Introduction

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.

To provide support and various options to the frame of an SDI, 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. Just as you can add one dialog box to an application, 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.

 

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

  1. Start your programming environment. For this example, we will use Borland C++BuilderX.
    Therefore, start Borland C++BuilderX and, on the main menu, click File -> New...
     
  2. In the Object Gallery dialog box, click New GUI Application and click OK
  3. In the New GUI Application Project Wizard - Step 1 of 3, in the Directory edit box of the Project Settings section, type the path you want. Otherwise, type
    C:\Programs\Win32 Programming
  4. In the Name edit box, type SDI1
     
  5. Click Next
  6. In the New GUI Application Project Wizard - Step 2 of 3, accept the defaults and click Next
  7. In the New GUI Application Project Wizard - Step 3 of 3, click the check box under Create
  8. Select Untitled under the Name column header. Type Exercise to replace the name and press Tab
  9. Click Finish
  10. To create a resource header file, on the main menu, click File -> New File...
  11. In the Create New File dialog box, in the Name, type Resource
  12. In the Type combo box, select h, and click OK
     
  13. In the file, type:
     
    #define IDR_MAIN_MENU     1001
    #define IDM_FILE_EXIT     1002
    #define IDM_HELP_ABOUT    1003
    
    #define IDD_ABOUTDLGBOX   1010
  14. To create a resource script, on the main menu, click File -> New File...
  15. In the Create New File dialog box, in the Name, type SDI1
  16. In the Type combo box, select rc, and click OK
     
  17. In the file, type:
     
    #include "Resource.h"
    
    // Main Menu
    IDR_MAIN_MENU MENU
    BEGIN
      POPUP "&File"
      BEGIN
        MENUITEM "E&xit",      IDM_FILE_EXIT
      END
      POPUP "&Help"
      BEGIN
        MENUITEM "&About...",  IDM_HELP_ABOUT
      END
    END
    
    // Dialog Box: About
    IDD_ABOUTDLGBOX DIALOGEX 0, 0, 184, 81
    STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
        WS_SYSMENU
    CAPTION "About SDI1"
    FONT 8, "MS Shell Dlg"
    BEGIN
        DEFPUSHBUTTON   "OK",IDOK,127,7,50,16,WS_GROUP
    END
  18. Display the Exercise.cpp file and change it as follows:
     
    #include <windows.h>
    #include "Resource.h"
    
    #ifdef __BORLANDC__
      #pragma argsused
    #endif
    //---------------------------------------------------------------------------
    HINSTANCE hInst;
    const char *ClsName = "CallingDlg";
    const char *WndName = "Calling a Dialog Box From an SDI";
    //---------------------------------------------------------------------------
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
                                  WPARAM wParam, LPARAM lParam);
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
                             WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                         LPSTR lpCmdLine, int nCmdShow )
    {
            MSG        Msg;
            HWND       hWnd;
            WNDCLASSEX WndClsEx;
    
            // Create the application window
            WndClsEx.cbSize        = sizeof(WNDCLASSEX);
            WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
            WndClsEx.lpfnWndProc   = WndProcedure;
            WndClsEx.cbClsExtra    = 0;
            WndClsEx.cbWndExtra    = 0;
            WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
            WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
            WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
            WndClsEx.lpszMenuName  = MAKEINTRESOURCE(IDR_MAIN_MENU);
            WndClsEx.lpszClassName = ClsName;
            WndClsEx.hInstance     = hInstance;
            WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
            // Register the application
            RegisterClassEx(&WndClsEx);
            hInst = hInstance;
    
            // Create the window object
            hWnd = CreateWindow(ClsName,
                              WndName,
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
            // Find out if the window was created
            if( !hWnd ) // If the window was not created,
                    return 0; // stop the application
    
            // Display the window to the user
            ShowWindow(hWnd, SW_SHOWNORMAL);
            UpdateWindow(hWnd);
    
            // Decode and treat the messages
            // as long as the application is running
            while( GetMessage(&Msg, NULL, 0, 0) )
            {
                 TranslateMessage(&Msg);
                 DispatchMessage(&Msg);
            }
    
            return Msg.wParam;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
                               WPARAM wParam, LPARAM lParam)
    {
        switch(Msg)
        {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case IDM_FILE_EXIT:
                PostQuitMessage(WM_QUIT);
                break;
            case IDM_HELP_ABOUT:
                DialogBox(hInst,
                          MAKEINTRESOURCE(IDD_ABOUTDLGBOX),
                          hWnd,
                          reinterpret_cast<DLGPROC>(DlgProc));
                break;
            }
            return 0;
    
        // If the user wants to close the application
        case WM_DESTROY:
            // then close it
            PostQuitMessage(WM_QUIT);
            break;
        default:
            // Process the left-over messages
            return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        // If something was not done, let it go
        return 0;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
            switch(Msg)
            {
            case WM_INITDIALOG:
                    return TRUE;
    
            case WM_COMMAND:
                    switch(wParam)
                    {
                    case IDOK:
                            EndDialog(hWndDlg, 0);
                            return TRUE;
                    }
                    break;
            }
    
            return FALSE;
    }
    //---------------------------------------------------------------------------
     
  19. Test the application
 

Home