The Modeless Dialog and Windows Common Dialogs
Modeless dialogs allow the user to work elsewhere in the
application while the dialog is active. The common dialog classes are the C++
programming interface to the group of Windows utility dialogs that include File
Open, Page Setup, Color, and so forth and that are supported by the dynamic
link library COMDLG32.DLL. In this Module's first example, you'll build a
simple modeless dialog that is controlled from a view. In the second example,
you'll derive from the COMDLG32 CfileDialog class a class that
allows file deletion
Modeless Dialogs
In the MFC Library version 6.0, modal and modeless dialogs
share the same base class,
CDialog, and
they both use a dialog resource that you can build with the dialog editor. If
you're using a modeless dialog with a view, you'll need to know some
specialized programming techniques
Creating Modeless Dialogs
For modal dialogs, you've already learned that you construct
a dialog object using a
Cdialog constructor
that takes a resource template ID as a parameter, and then you display the
modal dialog window by calling the DoModal()member
function. The window ceases to exist as soon as DoModal()returns.
Thus, you can construct a modal dialog object on the stack, knowing that the dialog
window has been destroyed by the time the C++ dialog object goes out of scope.
Modeless dialogs are more complicated. You start by invoking
the Cdialog default constructor
to construct the dialog object, but then to create the dialog window you need
to call the CDialog::Create member
function instead of DoModal().
Create takes the resource ID as a parameter and returns immediately with the
dialog window still on the screen. You must worry about exactly when to
construct the dialog object, when to create the dialog window, when to destroy
the dialog, and when to process user-entered data.
MessageBox
The most simple type of dialog box is the MessageBox function. The MessageBox function takes 4 parameters: a handle to a parent, a message, a title, and an option. If the parent handle is NULL, the message box is modeless. If you provide a handle for a parent window, the MessageBox can become Modal to the parent window.MessageBox dialog boxes have a number of different options that can be specified: Button types, Icons, modality (modal/modeless), and text justification. These options are specified as bit flags, that can be used by bitwise ORing them together.
Buttons
Message boxes can have standard OK or Cancel buttons, or they can have a "Yes, No, Cancel" configuration, or a number of derivatives. Only one primary button scheme can be used per message box:- MB_ABORTRETRYIGNORE: The message box contains three push buttons: Abort, Retry, and Ignore.
- MB_CANCELTRYCONTINUE: Same as MB_ABORTRETRYIGNORE, but preferred on Windows 2000/XP.
- MB_OK: The message box contains an "OK" button. This is the default.
- MB_OKCANCEL: The message box contains two push buttons: OK and Cancel.
- MB_RETRYCANCEL: The message box contains two push buttons: Retry and Cancel.
- MB_YESNO: The message box contains two push buttons: Yes and No.
- MB_YESNOCANCEL: The message box contains three push buttons: Yes, No, and Cancel.
MessageBox(NULL, "This is a Test", "Test", MB_OKCANCEL|MB_HELP|MB_DEFBUTTON2);This will have a message box with an "OK", a "Cancel", and a "Help" button, and the "Cancel" button will be automatically selected.
Icons
A message box may have no icons, or it may have one. You shouldn't specify a message box to have multiple icons. The different icons, according to MSDN are:- MB_ICONEXCLAMATION: An exclamation point icon appears in the message box.
- MB_ICONWARNING: An exclamation point icon appears in the message box.
- MB_ICONINFORMATION: An icon consisting of a lowercase letter i in a circle appears in the message box.
- MB_ICONASTERISK: An icon consisting of a lowercase letter i in a circle appears in the message box.
- MB_ICONQUESTION: A question
mark icon appears in the message box.
The question mark message icon is no longer recommended because it does not clearly represent a specific type of message and because the phrasing of a message as a question could apply to any message type. In addition, users can confuse the message symbol question mark with Help information. Therefore, do not use this question mark message symbol in your message boxes. The system continues to support its inclusion only for backward compatibility. - MB_ICONSTOP: A stop sign icon appears in the message box.
- MB_ICONERROR: A stop sign icon appears in the message box.
- MB_ICONHAND: A stop sign icon appears in the message box.
Dialog Box Procedures
Dialog box procedures are slightly different from window procedures. Specifically, they return BOOL values, instead of LRESULT values. Also, dialog boxes do not have a default message processing function, because messages don't always need to be handled. Specifically, Windows manages dialog boxes, and Windows will handle the unused messages. If a dialog box processes a certain message, it should return TRUE. If the message is not processed, the function should return FALSE. Also, Dialog boxes do not get a WM_CREATE message, but instead get a WM_INITDIALOG message. Furthermore, when a dialog box has finished its business, it should call the EndDialog function.Here is an example of a skeleton dialog box function:
BOOL CALLBACK MyDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_DESTROY:
EndDialog(hDlg, 0);
return TRUE;
}
return FALSE;
}
Common Dialog Boxes
The Common Dialogs is a library of functions that automatically produce some of the most common dialog boxes in Windows. This is an effort to make some amount of continuity between different programs, so that each different program doesn't create its own proprietary "File Open" dialog, for instance.Each Common Dialog generally has a single function that takes a pointer to a structure. This structure is defined specifically for each different control. The common controls can be added to a project by including the <commdlg.h> header file, and linking to the comdlg32.dll library.
Some of the common controls available through this library are the "Choose Font" dialog box, the "File open" and "File save" boxes, and the "Color Palette" dialog box.
ChooseColor
The ChooseColor function brings up the color palette window, and returns a 32-bit color value to your program.BOOL ChooseColor(LPCHOOSECOLOR lpcc);ChooseColor takes a single argument, in the form of a pointer to a CHOOSECOLOR structure. This structure is initialized with various values, and when the function returns, the CHOOSECOLOR structure contains the color value code.
GetOpenFileName and GetSaveFileName
These two functions bring up the familiar file open and file save dialog boxes that are found in nearly every Windows application.BOOL GetOpenFileName(LPOPENFILENAME lpofn);
BOOL GetSaveFileName(LPOPENFILENAME lpofn);Both of these functions take a pointer to an OPENFILENAME structure. This structure controls such things as the file extensions that may be loaded, and the starting path to look in. When the function returns, the structure will contain the name of the file selected. Once your program has this information, you can use the File I/O API to access the file.
ChooseFont
The ChooseFont function brings up a familiar dialog box that allows the user to select a font and various font attributes such as size, underline/bold/italics, color, etc. This function takes a pointer to a CHOOSEFONT structure.BOOL ChooseFont(LPCHOOSEFONT lpcf);
No comments:
Post a Comment