Using a Dialog Box as the Main Window of an Application

Last reviewed: April 5, 1996
Article ID: Q108936
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

Dialog boxes might be used as the main window of an application for several reasons. When an application uses a dialog box as the main window, the following should be taken into consideration while designing such an application:

  • The dialog box that acts as the main window of an application can be created without an owner.
  • If a modal dialog box is created as a main window, TranslateAccelerator() cannot be used.
  • The icon for the dialog box should be drawn manually when the dialog box is minimized.

MORE INFORMATION

You can create a modal or modeless dialog box as the main window of an application. In doing so, there is no need to have an overlapped window that acts as the owner of the dialog box. Memory for edit controls created with the DS_LOCALEDIT flag set, and static controls, will come from the heap represented by the hInstance passed to the CreateDialog() or DialogBox() call.

When modal dialog boxes are chosen for this purpose, and accelerator keys are defined in an application, Windows enters a modal message loop that does not process accelerators, unless a WH_MSGFILTER hook is installed and TranslateAccelerator() is called from the hook callback function.

One easy way to avoid this limitation is to create a modeless dialog box and call TranslateAccelerator() from the main message loop.

The icon for a window is stored in its class information. Because a dialog box is a window of global class that all applications in the system are using, changing the icon for this application will change the icon for all dialog boxes in the system. Below is one workaround for drawing the icon manually when the dialog box is minimized:

Sample Code

BOOL WINAPI GenericDlgProc (HWND hwnd, UINT msg,

                            WPARAM wParam,  LPARAM lParam)
 {

   RECT rect ;
   switch (msg) {

       case WM_INITDIALOG:
        hIcon = LoadIcon(); // Load the icon that is to be displayed
                            // when minimized.
        return TRUE ;

       case WM_ERASEBKGND:
            if (IsIconic(hwnd) && hIcon) {
              SendMessage( hwnd, WM_ICONERASEBKGND, wParam, 0L );
              return TRUE;
             }
        break;

       case WM_QUERYDRAGICON:
            return (hIcon);

        case WM_PAINT: {
            PAINTSTRUCT ps;

            BeginPaint( hwnd, &ps );

            if (IsIconic(hwnd))   //*** If iconic, paint the icon.
            {
              if (hIcon) {

                //center the icon correctly...
              GetClientRect(hwnd, &rect) ;
              rect.left = (rect.right - GetSystemMetrics(SM_CXICON)) >> 1;
              rect.top = (rect.bottom - GetSystemMetrics(SM_CYICON)) >> 1;
              DrawIcon( ps.hdc, rect.left, rect.top, hIcon );
                  }
            }
            EndPaint( hwnd, &ps );
        }
        break;
    }
    return FALSE;

} //*** GenericDlgProc

The workaround described above assumes that the dialog box belongs to the predefined dialog class. For private dialog box classes, there is no need to manually draw the icon for the dialog box when it is minimized. You can specify the icon while registering the dialog box class. Remember to set the cbWndExtra field to DLGWINDOWEXTRA. When the dialog box is minimized, the icon will be painted automatically.


Additional reference words: 3.00 3.10 3.50 3.51 4.00 95 DLGMAIN
KBCategory: kbui
KBSubcategory: UsrDlgs


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 5, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.