|
Creating a Modeless Dialog Box
You create a modeless dialog box by using the CreateDialog function, specifying the identifier or name of a dialog box template resource
and the address of the dialog box procedure. CreateDialog loads the template, creates the dialog box, and optionally displays it. Your
application is responsible for retrieving and dispatching user input messages
to the dialog box procedure.
In the following example, the application displays a modeless dialog box if it is not already displayed when the user chooses a Go To command from an application menu. The dialog
box contains an edit control, a check box, and OK and Cancel buttons. The dialog
box template is a resource in the application's executable file and has the
resource identifier DLG_GOTO. The user enters a line number in the edit control
and checks the check box to specify that the line number is relative to the
current line. The control identifiers are ID_LINE, ID_ABSREL, IDOK, and IDCANCEL.
The statements in the first part of the example create the modeless dialog
box. These statements, in the window procedure for the application's main window,
create the dialog box when the window procedure receives a WM_COMMAND message having the IDM_GOTO command identifier, but only if the global
variable hwndGoto does not already contain a valid handle. The second part of the example is
the application's main message loop. The loop includes the IsDialogMessage function to ensure that the user can use the dialog box keyboard interface in
this modeless dialog box. The third part of the example is the dialog box
procedure. The procedure retrieves the contents of the edit control and check box
when the user chooses the OK button. The procedure destroys the dialog box when
the user chooses the Cancel button.
HWND hwndGoto = NULL; // window handle of dialog box
.
.
.
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_GOTO:
if (!IsWindow(hwndGoto)) {
hwndGoto = CreateDialog(hinst,
MAKEINTRESOURCE(DLG_GOTO),
hwnd, (DLGPROC) GoToProc);
ShowWindow(hwndGoto, SW_SHOW);
}
break;
}
return 0L;
In the preceding statements, CreateDialog is called only if hwndGoto does not contain a valid window handle. This ensures that the application does not
display two dialog boxes at the same time. To support this method of checking, the
dialog procedure must set hwndGoto to NULL when it destroys the dialog box.
The message loop for an application consists of the following statements:
while (GetMessage(&msg, NULL, NULL, NULL)) {
if (!IsWindow(hwndGoto) || !IsDialogMessage(hwndGoto, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
The loop checks the validity of the window handle for the dialog box and only
calls the IsDialogMessage function if the handle is valid. IsDialogMessage only processes the message if it belongs to the dialog box. Otherwise, it
returns FALSE and the loop dispatches the message to the appropriate window.
The following statements define the dialog box procedure:
int iLine; // receives line number
BOOL fRelative; // receives check box status
.
.
.
BOOL CALLBACK GoToProc(hwndDlg, message, wParam, lParam)
HWND hwndDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
BOOL fError;
switch (message) {
case WM_INITDIALOG:
CheckDlgButton(hwndDlg, ID_ABSREL, fRelative);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
fRelative = IsDlgButtonChecked(hwndDlg,
ID_ABSREL);
iLine = GetDlgItemInt(hwndDlg, ID_LINE,
&fError, fRelative);
if (fError) {
MessageBox(hwndDlg, SZINVALIDNUMBER,
SZGOTOERR, MB_OK);
SendDlgItemMessage(hwndDlg, ID_LINE,
EM_SETSEL, 0, -1L);
} else
.
. // Notify the owner window to carry
. // out the command.
.
return TRUE;
case IDCANCEL:
DestroyWindow(hwndDlg);
hwndGoto = NULL;
return TRUE;
}
}
return FALSE;
}
In the preceding statements, the procedure processes the WM_INITDIALOG and WM_COMMAND messages. During WM_INITDIALOG processing, the procedure initializes the
check box by passing the current value of the global variable fRelative to CheckDlgButton. The procedure then returns TRUE to direct Windows to set the default input
focus.
During WM_COMMAND processing, the procedure closes the dialog box only if the
user chooses the Cancel button that is, the button having the IDCANCEL identifier. The procedure must call DestroyWindow to close a modeless dialog box. Notice that the procedure also sets the hwndGoto variable to NULL to ensure that other statements that depend on this variable
operate correctly.
If the user chooses the OK button, the procedure retrieves the current state
of the check box and assigns it to the fRelative variable. It then uses the variable to retrieve the line number from the edit
control. GetDlgItemInt translates the text in the edit control into an integer. The value of fRelative determines whether the function interprets the number as a signed or unsigned
value. If the edit control text is not a valid number, GetDlgItemInt sets the value of the fError variable to nonzero. The procedure checks this value to determine whether to
display an error message or carry out the command. In the event of an error,
the dialog box procedure sends a message to the edit control, directing it to
select the text in the control so that the user can easily replace it. If GetDlgItemInt does not return an error, the procedure can either carry out the requested
command itself or send a message to the owner window, directing it to carry out
the command.
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|