Overview
Dialog boxes are a fundamental part of the Win32 API, providing a simple way to collect user input, display messages, and create custom UI components. This article covers creation, template design, message handling, and common pre‑defined dialogs.
Creating a Modal Dialog
Use DialogBoxParam to create a modal dialog from a template resource.
#include <windows.h>
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
int APIENTRY wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int nCmdShow)
{
DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_MYDIALOG), NULL, DialogProc, 0);
return 0;
}
Dialog Template (Resource Script)
IDD_MYDIALOG DIALOGEX 0, 0, 186, 95
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Sample Dialog"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "OK",IDOK,129,74,50,14
PUSHBUTTON "Cancel",IDCANCEL,73,74,50,14
CONTROL "Name:",STATIC,SS_RIGHT,7,10,50,8
EDITTEXT IDC_EDIT_NAME,60,8,110,14,ES_AUTOHSCROLL
END
Common Predefined Dialogs
- OpenFileDialog –
GetOpenFileName - SaveFileDialog –
GetSaveFileName - Color Picker –
ChooseColor - Font Picker –
ChooseFont
Comments