HOWTO: Use a Dialog Box as the Main Window of an Application
ID: Q108936
|
The information in this article applies to:
-
Microsoft Win32 Software Development Kit (SDK)
-
Microsoft Windows Software Development Kit (SDK) 3.1
-
Microsoft Windows 2000
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 query words:
DLGMAIN
Keywords : kbDlg kbNTOS kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS
Version : WINDOWS:3.1
Platform : WINDOWS
Issue type : kbhowto