The dialog box is often used, because the dialog box can be created from the template, and the dialog box template can be easily edited using the resource editor.
Mode and no mode dialog box
There are two types of dialog boxes: Mode dialog box and mode-free dialog box.
Mode dialog box
A mode dialog box is a pop-up window with system menus, title bars, and edges. In the create dialog box, specifyWS_POPUP, WS_SYSMENU, WS_CAPTION, and DS_MODALFRAME. The Mode dialog box is displayed even if the WS_VISIBLE style is not specified.
When the dialog box is createdWM_INITDIALOG message (if the DS_SETFONT style of the dialog box is specified, WM_SETFONT message is also provided) to the dialog box process.
Dialog Box Process(Dialog box procedure) is not the Window procedure of the Dialog box Window (Window procedure ). In Win32, the dialog box window process is provided by the Windows system. When you create a dialog box window, a dialog box process is called by the window process.
After the dialog box is created,Windows makes it an activation window, and it remains activated until the dialog box is called: EndDialog function end dialog box running or Windows activates another application. During activation, the user or application cannot activate its Owner window ).
When creating a mode dialog box from a window,Windows automatically disables the Disable window and all its subwindows until the mode dialog box is closed and destroyed. Although the dialog box process can Enable the window to which the dialog box belongs, this will lose the role of the mode dialog box, so we do not encourage this.
In the Windows create mode dialog box, send the message WM_CANCLEMODE to the window that captures the mouse input (if any. After receiving the message, the application should terminate the mouse capture (Release the mouse capture) so that the user can move the mouse to the mode dialog box; otherwise, the program will lose the mouse input because the Owner window is disabled.
To process messages in the mode dialog box,The message loop of the Windows Start dialog box, which temporarily controls the message queue of the entire application. If Windows receives a non-dialog message, it distributes the message to the appropriate window for processing. if it receives the WM_QUIT message, it puts the message back into the message queue of the application, in this way, the main message loop of the application can finally process the message.
When the application's message queue is empty,Windows sends the WM_ENTERIDLE message to the Owner window. When the dialog box is running, the program can use this message for background processing. Of course, you should pay attention to frequent outbound control to the mode dialog box so that it can receive user input. If you do not want the mode dialog box to send the WM_ENTERIDlE message, specify the DS_NOIDLEMSG style when creating the mode dialog box.
An application calls: EndDialog function to destroy a mode dialog box. Generally, when you select the Close command from the system menu or press OK or cancel, the EndDialog is called by the dialog box. When calling: EndDialog, specify the value of its nResult parameter. In Windows, this value is returned after the destruction dialog box window. Generally, the program checks whether the task has been completed or canceled by the user in the dialog box through the return value.
Modeless Dialog Box
A stateless dialog box is a pop-up window with system menus, title bars, and edges. SpecifyWS_POPUP, WS_CAPTION, WS_BORDER, and WS_SYSMENU. If the WS_VISIBLE style is not specified, the dialog box without mode is automatically displayed.
A non-Modal Dialog Box neither disallows a window nor sends messages to it. When you create a mode dialog box,Windows makes it an activity window, but users or programs can change and set the activity window at any time. If the dialog box is not activated, even if the window to which it belongs is active, it remains above the window in the z-axis sequence.
The application obtains and distributes input messages to the dialog box. Most applications use the main message loop for processing, but in order for the user to move between control windows or select control windows using the keyboard, the application should call: IsDialogMessage function.
Here, by the way: IsDialogMessage function. Although this function is designed for a non-modal dialog box, any window that contains a control subwindow can call it to implement keyboard selection operations similar to the dialog box.
When: When IsDialogMessage is used to process a message, it checks the Keyboard Message and converts it to the selection command in the corresponding dialog box. For example, when the Tab key is pressed, the next or next group of controls is selected. When the Down Arrow key is pressed, the next control in the group is selected.
: IsDialogMessage converts and distributes all necessary messages. Therefore, the messages processed by this function must not be transferred to TranslateMessage and DispatchMessage for processing.
A mode-free dialog box cannot return a value to the application as in the mode dialog box. However, the dialog box can be used.: SendMessage sends information to the corresponding window.
Before the application ends, it must destroy all modeless dialogs. Use: DestroyWindow: destroy a modeless dialog box, instead of using: EndDiaLog. In general, the dialog box responds to user input. If you select the "cancel" button, call: DestroyWindow. If the user does not have any action, the application must call: DestroyWindow.
Dialog BoxMFC implementation
InIn MFC, the dialog box window function is mainly implemented by the CWnd and CDialog classes.
Design and Implementation of CDialog
MFC uses CDialog to encapsulate the dialog box function. Cdind inherits the functions of the window class from CWnd (including the functions implemented by CWnd), and adds new member variables and functions to process the dialog box.
CDialog member variables
The CDialog member variables include:
Protected:
UINT m_nIDHelp; // Help ID (0 for none, see HID_BASE_RESOURCE)
LPCTSTR m_lpszTemplateName;// Name or MAKEINTRESOURCE
HGLOBAL m_hDialogTe