Creating a modal dialog in the Windows API generally relies on using the DialogBox family of functions. These functions create a dialog box from a dialog box template resource and then launch it in a modal state. Below is a detailed breakdown of how to use DialogBox and related functions, primarily focusing on DialogBox itself since it's the most straightforward.
Functions and Definitions
- DialogBox: This function creates a modal dialog box from a dialog box template resource.
int DialogBox(
HINSTANCE hInstance,
LPCTSTR lpTemplateName,
HWND hWndParent,
DLGPROC lpDialogFunc
);
- hInstance: Handle to the application instance.
- lpTemplateName: Pointer to a null-terminated string that specifies the dialog box template.
- hWndParent: Handle to the parent window.
- lpDialogFunc: Pointer to a dialog procedure function (callback).
Steps to Create and Display a Modal Dialog
1. Define Dialog in Resource Script
First, ensure your dialog template is defined in your resource script (.rc file).
// Resource script (resources.rc)
#include "resource.h"
IDD_MYDIALOG DIALOGEX 0, 0, 200, 150
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My Dialog"
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "OK",IDOK,50,100,50,14
PUSHBUTTON "Cancel",IDCANCEL,110,100,50,14
END
2. Declare Resource Identifiers
Make sure you have a header file (resource.h) with the necessary identifiers.
// resource.h
#define IDD_MYDIALOG 101
#define IDOK 1
#define IDCANCEL 2
3. Write the Dialog Procedure
The dialog procedure handles messages sent to the dialog window.
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK MyDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
4. Implement the DialogBox Call
In your main application code, use DialogBox to create and display the modal dialog box.
#include <windows.h>
#include "resource.h"
// Declarations of the dialog procedure
INT_PTR CALLBACK MyDialogProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
// Call to create the modal dialog box
int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), NULL, MyDialogProc);
if (ret == IDOK) {
// OK button was pressed.
} else if (ret == IDCANCEL) {
// Cancel button was pressed or DialogBox failed.
} else if (ret == -1) {
// Handle error (use GetLastError() to find out more).
MessageBox(NULL, L"DialogBox failed!", L"Error", MB_OK | MB_ICONERROR);
}
return 0;
}
Compile and Link Resources
In your build process, ensure that your .rc file is compiled and linked. In an IDE like Visual Studio, this is typically handled for you. If you are using a makefile or command line, you may need to explicitly compile the resource script and link it into your application.
# Example command line for compiling resource file (using rc and link)
rc resources.rc
cl main.cpp resources.res /link user32.lib
This concludes the steps necessary to create and display a modal dialog box in a Windows application using the Windows API. Once the dialog box is created by DialogBox, the application waits until the dialog box is closed before continuing.