Your browser doesn't support JavaScript The Dialog Box – Windows Programming

The Dialog Box

A dialog box is a temporary pop-up window that prompts the user for additional information or requests input before an application can continue with a task. Dialog boxes are commonly used to display settings, collect user input, confirm actions, or present important messages.

Dialog boxes are typically created using a dialog editor and defined within a program’s resource file (.rc). A dialog box usually contains one or more controls (child windows), such as buttons, edit boxes, check boxes, radio buttons, list boxes, and combo boxes, allowing the user to enter data, choose options, or control the application’s behaviour.

In addition to user-defined dialog boxes, Windows provides several predefined dialog boxes for common tasks, including selecting colours, opening or saving files, choosing fonts, and printing documents.

Modal v Modeless Dialogue Box

A modeless dialog box allows the user to continue interacting with other windows in the application while the dialog box remains open. It stays on the screen until it is explicitly closed by the user or the application.

Typical uses include:

  • Find and Replace windows
  • Toolboxes
  • Formatting palettes
  • Search panels

Modeless dialog boxes are commonly created using the CreateDialog() function

A modal dialog box requires the user to respond before returning to the main application window. While the dialog box is open, the user cannot interact with other application windows. The dialog box must be closed before work can continue.

Typical uses include:

  • Save confirmation dialogs
  • Error and warning messages
  • File Open and Save dialogs
  • Application settings that must be completed before proceeding

Modal dialog boxes are commonly created using the DialogBox() function.

Creating a Modeless Dialog Box

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 dialog box handle. If the function fails, the return value is NULL.

Deactivating a Modeless Dialog Box

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.

Creating a Modal Dialog Box

Modal dialog boxes are created using the APO function DialogBox(). The syntax is

void DialogBox(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.

Deactivating a Modal Dialog Box

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.

Processing Dialog Messages

Like a standard window, each dialog box has its own dialog procedure (also called a dialog callback function) that receives and processes Windows messages.

Example

The code example demonstrates a modal and modeless dialog box. The modal dialog box displays a simple message. The modeless dialog box has 3 radio buttons. Selecting any of the radiobutton changes the background colour of the parent window.

dialog demo image

Dialog-Based Applications

A dialog-based application is a Windows application in which the main window is a dialog box rather than a standard application window. This approach simplifies the development process by allowing controls such as buttons, edit boxes, check boxes, and list boxes to be placed visually on the window using the Resource Editor provided by the development environment.

The behaviour of the dialog box is controlled by a dialog procedure, which processes messages generated by the dialog box and its controls. For example, the dialog procedure can respond to button clicks, edit box input, and other user interactions.

Dialog-based applications require considerably less code than traditional Win32 applications because much of the window creation and message handling is managed automatically by the Windows dialog manager. As a result, they are particularly well suited to small utilities, configuration programs, and applications with simple user interfaces.

Although dialog-based applications are easy to develop, they are generally less flexible than traditional frame-window applications and are therefore best suited to relatively straightforward applications.

Example

The following example demonstrates a simple dialog-based application. The main window is implemented as a dialog box containing an edit box, a button, and a static text control. The user enters text into the edit box and clicks the button to change the title (caption) of the dialog box to the text entered. This example illustrates how a dialog procedure processes control events and updates the dialog box in response to user actions.