Dialog Boxes |
|
A dialog box is a rectangular window whose main role is to host or hold other Windows controls. For this reason, a dialog box is referred to as a container. It is the primary interface of user interaction with the computer. By itself, a dialog box means nothing. The controls it hosts accomplish the role of dialog between the user and the machine. A dialog box has the following characteristics: |
When using Win32 to create an application, a dialog is created using a text file called a resource file. The file has the extension .rc as we saw in a previous lesson. Microsoft Visual C++ makes it particularly easy to create a dialog box and, behind the scenes, it creates the appropriate section for a dialog box. To create a dialog box in MSVC, from the Add Resources dialog box, simply select the Dialog node and the object is ready. After creating a "physical" dialog box, you should create its class so that other objects of the application can use this control.
Microsoft Visual C++ provides an easier way to create an application that is mainly based on a dialog box. To use this technique, start a new project and specify that you want to create an MFC Application. In the
Like every resource, a dialog box is recognized throughout the program by an identifier. By default, the first dialog box is identified as IDD_DIALOG1. In this name, ID stands for IDentifier. The second D stands for Dialog. In most cases, you should give an explicit identifier to each resource. For example, if a dialog box is used as an employment application, you can identify it as IDD_EMPL_APP or something like that. The top section of a dialog box is made of a long bar that we call the title bar. The main area of this bar contains a word or a group of words forming a sentence called a caption. There are two main ways you can change it. At design time, on the Properties window, change the value of the Caption field. At run time, to change the caption, call the CWnd::SetWindowText() method and provide the necessary string as argument. Here is an example:
By convention, a dialog box is equipped with only the system Close button on its title bar. The Minimize and Maximize buttons are omitted. If you want to add the Minimize button , at design time, set the Minimize Box property to True or checked. If you want the Maximize button , set its property to True or checked. Setting one of these properties to True or checked and not the other would disable the other system button. Therefore, here are the combinations you can get:
All of the windows we will use in this book are created using a style set in the Win32 API as HWND and implement in MFC by the CWnd class. This class, among other things, defines the style used to control the appearance and certainly part of the behavior of an object. Based on this, a window is referred to as popup if it can be displayed on the screen. That is, if it can be "physically" located. A window is referred to as child if its appearance is dependent of the appearance of another window. All of the Windows controls that we will be placing on our dialog boxes are child controls. A window is called overlapped if it has the following characteristics:
The Child, Popup, and Overlapped characteristics can be set at design time using the Style property. Because a dialog box follows these rules, it qualifies as an overlapped window. By design, a dialog box has thick borders and cannot be resized from its borders or corners, even if the system buttons are available. This characteristic of dialog boxes is controlled by the Border property. The default border of a dialog box is Dialog Frame. If you want he user to be able to resize a dialog box, set its Border value to Resizing: A Thin value gives a thin border to the dialog box. If you do not want borders at all on the object, set its Border value to None. This also removes the title bar and thus the system buttons:
As seen above, to create dialog box, derive a class from CDialog and use a constructor to specify the resource that holds the default characteristics of the object. The CDialog class provides three constructors as follows: CDialog(); CDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL); CDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL); The default constructor, CDialog() can be use to create a variable whose behavior is not yet known or, for one reason or another, cannot yet be defined. The resources we will be using in MFC have an identifier that allow them to be recognized by other others of an application. This is why, whenever you visually create a dialog box, you also specify its identifier. This identifier can be used as the first argument, nIDTemplate, of a CDialog() constructor to create a dialog box from an existing resource. If you are using a Win32 template to create your dialog box, pass the name of this template as a string to a CDialog() constructor, lpszTemplateName. All or most of the window we will be creating in this book are owned and made available to the operating system by an application. The second argument of the last two constructors allow you to specify "who" owns the dialog box you are creating. If the dialog box has a parent, which is usually a CWnd type, specify its name as the pParenWnd argument. Otherwise, leave this argument at its default, which is NULL, letting the application own the dialog box. There are two types of dialog boxes: modal and modeless. A Modal dialog box is one that the user must first close in order to have access to any other framed window or dialog box of the same application:
|
|
The Paragraph dialog box of WordPad is a modal dialog box: when it is displaying, the user cannot use any other part of WordPad unless he or she closes this object first |
After creating a dialog box, to display it as modal, call the DoModal() method. Its syntax is:
virtual int DoModal();
This method by itself does nothing more than displaying a dialog box as modal. We will learn that you can use this method to find out how the user had closed such a dialog box.
A modeless dialog box allows the user to access the main application or any other possible object of the application even if this dialog box is displaying. The user can decide to close it when the object is not need anymore or the user can keep it on as long as necessary. Here is an example:
The Find dialog box of WordPad is an example of a modeless dialog box. Although it is displaying on this picture, the user can still click and activate any of the windows in the background |
Because a modeless dialog box is closed as the user judges it necessary, making it possible to forget, the operating system needs to make sure that this object closes when its parent application closes so its memory resource can be freed and made available to other applications. Therefore, the creating of a modeless dialog is a little different. You can first create a dialog resource and associate a class to it. To formally create a modeless dialog box and make it part of the application, you can use the Create() method of the CDialog class. The MFC provides two syntaxes that are:
BOOL Create(UINT nIDTemplate, CWnd* pParentWnd = NULL); BOOL Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
As done with the CDialog constructor, the nIDTemplate parameter is the identifier you used when visually creating the dialog re. If you are using a dialog box from a template, specify its name as the lpszTemplateName argument. In order to effectively use a modeless dialog box, it must be called from another window. Otherwise you can just create a regular dialog box. The object from which you will be calling the dialog box is also responsible for closing it (or "cleaning" it). Therefore, the class that calls the modeless dialog box "owns" i. This means that you can specify it as the pParentWnd argument.
When creating a modeless dialog box, declare a pointer to its class. Then call the Create() method, passing the identifier or the template being used. You can do this in the constructor of the class that will be its parent. Here is an example:
CDialog5Dlg::CDialog5Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDialog5Dlg::IDD, pParent) { CCoolMode* Cool = new CCoolMode(); Cool->Create(IDD_DLG_MDLS,this); }
To formally display the object, you have various options. When creating it, at design time, set its Visible property to True (actually you would be applying the WS_VISIBLE style). If you did not or if the modeless dialog box is hidden at a particular time, to display it, call the CWnd::ShowWindow() method. Its syntax is:
BOOL ShowWindow(int nCmdShow);
This method is used to display or hide any window that is a descendent of CWnd. Its argument, nCmdShow, specifies what to do with the appearance or disappearance of the object. Its possible values are:
Value | Description | |
SW_SHOW | Displays a window and makes it visible | |
SW_SHOWNORMAL | Displays the window in its regular size. In most circumstances, the operating system keeps track of the last location and size a window such as Internet Explorer or My Computer had the last time it was displaying. This value allows the OS to restore it. | |
SW_SHOWMINIMIZED | Opens the window in its minimized state, representing it as a button on the taskbar | |
SW_SHOWMAXIMIZED | Opens the window in its maximized state | |
SW_SHOWMINNOACTIVE | Opens the window but displays only its icon. It does not make it active | |
SW_SHOWNA | As previous | |
SW_SHOWNOACTIVATE | Retrieves the window's previous size and location and displays it accordingly | |
SW_HIDE | Used to hide a window | |
SW_MINIMIZE | shrinks the window and reduces it to a button on the taskbar | |
SW_RESTORE | If the window was minimized or maximized, it would be restored to its previous location and size |
To use one of these constants, pass it to the ShowWindow() method. For example, to minimize a window, you would use code as follows:
ShowWindow(SW_SHOWMINIMIZED);
After creating a dialog box, to initialize it (or the objects it is hosting), use the OnInitDialog() method. Its syntax is:
virtual BOOL OnInitDialog( );
As you can see, this method does not do more than provide a platform to set the default values of the dialog box and/or its controls.
Practical Learning: Using Dialog Box Methods |
BOOL CDialog2aDlg::OnInitDialog() { CDialog::OnInitDialog(); . . . // TODO: Add extra initialization here this->SetWindowText("Personal Information"); return TRUE; // return TRUE unless you set the focus to a control } |
#include "stdafx.h" #include "Dialog2a.h" #include "Dialog2aDlg.h" #include "FloaterDlg.h" |
BOOL CDialog2aDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here this->SetWindowText("Personal Information"); CSecondDlg *Floater = new CSecondDlg; Floater->Create(IDD_DLG_FLOATER, this); Floater->ShowWindow(SW_SHOW); return TRUE; // return TRUE unless you set the focus to a control } |
Combining Dialog Boxes |
It will not be unusual to use various dialog boxes in an application. When in case, you may need to call one dialog box from another. Since a dialog box is created using a class, the first first you must do is to include the header file of the CDialog object whose box you want to call.
To call one dialog box from another window, select the event (or message) from where you would make the call. Declare a variable of the other class and use the CDialog::DoModal() method to display the other object as a modal dialog box.
Practical Learning: Adding Controls to a Dialog Box |
#include "stdafx.h" #include "Dialog2a.h" #include "Dialog2aDlg.h" #include "FloaterDlg.h" #include "SecondDlg.h" |
void CDialog2aDlg::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CSecondDlg Dlg; Dlg.DoModal(); CDialog::OnLButtonDblClk(nFlags, point); } |
|
||
Home | Copyright © 2003-2015, FunctionX | |
|