A dialog box is a temporary popup window used by an application to prompt the user for additional information input. A dialog box will usually contain one or more controls (child windows) with which the user can enter text, choose options, or direct the action of the application.
In addition to user-defined dialogue boxes, Windows also provides predefined dialog boxes that support common menu items such as colour selection and an open file interface.
Dialog boxes can be either modeless or modal. A modal dialogue box will not allow the user to switch between the dialog box and any other window without explicitly closing the dialog box. A modeless dialog box does not prevent the user form switching to other parts of the application.
Modeless dialog boxes are created using the API function CreateDialog(). The syntax is below
hDlgModeless = CreateDialog (hInstance, lpTemplate, hWndParent, lpDialogFunc) ;
hInstance - A handle to the current application.
lpTemplate - Specifies the resource identifier of the dialog box template.
hWndParent - A handle to the parent window that owns the dialog box.
lpDialogFunc - A pointer to the dialog box procedure.
Return value - If the function succeeds, the return value is the handle to the dialog box. If the function fails, the return value is NULL.
To destroy the modeless dialog box, use the DestroyWindow() function.
BOOL DestroyWindow(HWND hWnd);
Where hWnd is a handle to the window to be destroyed. If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
Modal dialog boxes are created using the APO function DialogBox(). The syntax is
void DialogBoxA(hInstance,lpTemplate,hWndParent,lpDialogFunc);
hInstance - A handle to the current application.
lpTemplate - specifies the resource identifier of the dialog box template.
hWndParent - A handle to the parent window that owns the dialog box.
lpDialogFunc - A pointer to the dialog box procedure.
To deactivate and close a modal dialogue box use the EndDialog() API function call. The syntax of this function is
BOOL EndDialog(HWND hDlg,INT_PTR nResult);
HDlg - A handle to the dialog box to be destroyed.
nResult - The value to be returned to the application from the function that created the dialog box.
Return value - If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
Each dialogue box will have its own version of the window call-back function (wndproc) and receives messages in the same fashion as the main window. The dialog callback function, however, differs from its main windows counterpart in that it returns a boolean value.
The code example demonstrates both a modal and modeless with the modeless dialog box having 3 radio button which change the backround of the parent window.
//modal and modeless dialog box
////////////////////////////////////////////////////////////////////////////
//Microsoft Visual Studio generated resource script.
/////////////////////////////////////////////////////////////////////////////
/*
IDD_MODELESS DIALOG DISCARDABLE 0, 0, 186, 90
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
CONTROL "Red",IDC_RED,"Button",BS_AUTORADIOBUTTON,17,23,84,10
CONTROL "Yellow",IDC_YELLOW,"Button",BS_AUTORADIOBUTTON,17,41,84,
10
CONTROL "Green",IDC_GREEN,"Button",BS_AUTORADIOBUTTON,17,57,84,
10
GROUPBOX "Static",IDC_STATIC,7,15,126,59
END
IDD_MODAL DIALOG DISCARDABLE 0, 0, 186, 90
STYLE DS_SYSMODAL | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
LTEXT "Static",IDC_STATIC1,26,23,75,16
END
*/
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK ModelessDialogProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK ModalDialogProc(HWND, UINT, WPARAM, LPARAM);
void CreateModelessDialogBox(HWND);
void RegisterModelessDialogClass(HWND);
void CreateModalDialogBox(HWND );
void setbackground(HWND);
HBRUSH hBrush;
HINSTANCE ghInstance;
COLORREF bk_color=RGB(255, 255, 255);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PWSTR pCmdLine, int nCmdShow) {
MSG msg;
HWND hwnd;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("Window");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, TEXT("Dialog box demo"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 330, 150, NULL, NULL, hInstance, NULL);
ghInstance = hInstance;
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
RegisterModelessDialogClass(hwnd);
CreateWindow(TEXT("button"), TEXT("Show modeless"), WS_VISIBLE | WS_CHILD ,20, 50, 130, 35, hwnd, (HMENU) IDD_MODELESS, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Show modal"), WS_VISIBLE | WS_CHILD ,160, 50, 130, 35, hwnd, (HMENU) IDD_MODAL, NULL, NULL);
hBrush= GetSysColorBrush(COLOR_3DFACE);
break;
case WM_PAINT:
HDC hdc;
RECT rc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
//Erases window background with colour in hBrush
case WM_ERASEBKGND:
{
hdc = (HDC) wParam;
GetClientRect(hwnd, &rc);
FillRect(hdc, &rc, hBrush );
}
return 1l;
case WM_COMMAND:
switch (LOWORD (wParam))
{
//responds to buttons click
case IDD_MODELESS:
CreateModelessDialogBox(hwnd);
break;
case IDD_MODAL:
CreateModalDialogBox(hwnd);
break;
}
break;
case WM_DESTROY:
{
DeleteObject(hBrush);
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//modeless callback function
BOOL CALLBACK ModelessDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND static mainwnd=GetParent(hwnd);
switch(msg)
{
case WM_INITDIALOG:
SetFocus(hwnd);
// CheckDlgButton(hwndDlg, ID_ABSREL, fRelative);
return TRUE;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED) {
// bool checked = IsDlgButtonChecked(hwnd,IDC_RED);
switch (LOWORD (wParam))
{
//responds to radio button click
case IDC_RED://red button selected.
bk_color=RGB(255, 0, 0);
hBrush = CreateSolidBrush(bk_color);
break;
case IDC_YELLOW://yellow button selected
bk_color=RGB(255, 255, 0);
hBrush = CreateSolidBrush(bk_color);
break;
case IDC_GREEN://grey button selected
bk_color=RGB(0, 255, 0);
hBrush=CreateSolidBrush(bk_color);
break;
}
InvalidateRect(mainwnd, NULL,TRUE);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);//destroy dialog window
return 1 ;
break;
}
return 0;
}
//modal callback function
BOOL CALLBACK ModalDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND static mainwnd=GetParent( hwnd);
TCHAR *words = TEXT("A simple demonstration of a modal dialog window.");
HWND static staticbox;
switch(msg)
{
case WM_INITDIALOG:
staticbox = GetDlgItem(hwnd,IDC_STATIC1 );
SetWindowText(staticbox, words);
break;
return 0;
case WM_CLOSE:
EndDialog (hwnd, 0);//destroy dialog window
return TRUE ;
break;
}
return 0;
}
//register modeless dialog class
void RegisterModelessDialogClass(HWND hwnd) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = (WNDPROC) ModelessDialogProc;
wc.hInstance = ghInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszClassName = TEXT("DialogClass");
RegisterClassEx(&wc);
}
//register modal dialog class
void RegisterModalDialogClass(HWND hwnd) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = (WNDPROC) ModalDialogProc;
wc.hInstance = ghInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszClassName = TEXT("DialogClass");
RegisterClassEx(&wc);
}
void CreateModelessDialogBox(HWND hwnd)
{
//creates and shows modeless dialog window
HWND hDlgModeless = CreateDialog( ghInstance,MAKEINTRESOURCE(IDD_MODELESS), hwnd, (DLGPROC)ModelessDialogProc);
ShowWindow (hDlgModeless, SW_SHOW) ;
}
//creates and shows modal dialog window
void CreateModalDialogBox(HWND hwnd)
{
DialogBox( ghInstance,MAKEINTRESOURCE(IDD_MODAL), hwnd, ModalDialogProc);
}