Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








The Ex21a Example: A Dialog Box–Based Application


For many applications, a dialog box is a sufficient user interface. The dialog box appears when the user starts the application. The user can minimize the dialog box, and as long as the dialog box is not system modal, the user can freely switch to other applications.

In this example, the dialog box functions as a simple calculator, as shown in Figure 21-1. The Add Member Variable Wizard takes care of defining the class data members and generating the DDX (Dialog Data Exchange) function calls—everything but the coding of the compute function. The application's resource script,

Ex21a.rc , defines an icon as well as the dialog box.


Figure 21-1: The Ex21a Calculator dialog box

The MFC Application Wizard gives you the option of generating a dialog box–based application. Here are the steps for building the Ex21a example:



    Run the MFC Application Wizard to produce the Ex21a project. Select the Dialog Based option on the Application Type page, as shown here:


    On the User Interface Features page, enter Ex21a Calculator as the dialog box title.



    Edit the IDD_EX21A_DIALOG resource. Referring to Figure 21-1 as a guide, use the dialog editor to assign IDs to the controls shown in the following table. Then open the dialog box's Properties window. Set the System Menu and Minimize Box properties to True.

























    Control


    ID


    Left operand edit control


    IDC_LEFT


    Right operand edit control


    IDC_RIGHT


    Result edit control


    IDC_RESULT


    First radio button (group property set)


    IDC_OPERATION


    Compute button


    IDC_COMPUTE




    Use the Add Member Variable Wizard to add member variables, and use Class View's Properties window to add a command handler. The MFC Application Wizard has already generated a class CEx21aDlg. Add the following data members:






















    Control ID


    Member Variable


    Type


    IDC_LEFT


    m_dLeft


    Double


    IDC_RIGHT


    m_dRight


    Double


    IDC_RESULT


    m_dResult


    Double


    IDC_OPERATION


    m_nOperation


    int


    Add the message handler OnBnClickedCompute for the IDC_COMPUTE button.



    Code the OnBnClickedCompute member function in the

    Ex21aDlg.cpp file. Add the following boldface code:

    void CEx21aDlg::OnBnClickedCompute()
    {
    UpdateData(TRUE);
    if(m_nOperation == 0) {
    m_dResult = m_dLeft + m_dRight;
    } else if(m_nOperation == 1) {
    m_dResult = m_dLeft - m_dRight;
    } else if(m_nOperation == 2) {
    m_dResult = m_dLeft * m_dRight;
    } else if(m_nOperation == 3) {
    if(m_dRight == 0) {
    AfxMessageBox("Divide by zero");
    } else {
    m_dResult = m_dLeft / m_dRight;
    }
    }
    UpdateData(FALSE);
    }



    Build and test the Ex21a application. Notice that the program's icon appears on the Windows taskbar. Verify that you can minimize the dialog box.




The Application Class InitInstance Function


The critical element of the Ex21a application is the CEx21aApp::InitInstance function generated by the MFC Application Wizard. A normal InitInstance function creates a main frame window and returns TRUE, which allows the program's message loop to run. The Ex21a version constructs a modal dialog object, calls DoModal, and then returns FALSE. This means that the application exits after the user exits the dialog box. The DoModal function lets the Windows dialog procedure get and dispatch messages, as it always does. Note that the MFC Application Wizard does not generate a call to CWinApp::SetRegistryKey.

Here's the generated InitInstance code from

Ex21a.cpp :

BOOL CEx21aApp::InitInstance()
{
// InitCommonControls() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
InitCommonControls();
CWinApp::InitInstance();
AfxEnableControlContainer();
CEx21aDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}



The Dialog Class and the Program Icon


The generated CEx21aDlg class contains these two message map entries:

ON_WM_PAINT()
ON_WM_QUERYDRAGICON()

The associated handler functions take care of displaying the application's icon when the user minimizes the program. This code applies only to Microsoft Windows NT version 3.51, in which the icon is displayed on the desktop. You don't need these handlers for Windows 95/98/Me or Windows NT 4.0/2000/XP because those versions of Windows display the program's icon directly on the taskbar.

There is some icon code that you do need. It's in the dialog box's OnInitDialog handler, which is generated by the MFC Application Wizard. Notice the two SetIcon calls in the OnInitDialog function code shown below. If you selected the About box option, the MFC Application Wizard will generate code to add an About box to the System menu. The variable m_hIcon is a data member of the dialog class that is initialized in the constructor.

 BOOL CEx21aDlg::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);
}
}
// Set the icon for this dialog. The framework does this
// automatically when the application's main window
// is not a dialog.
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}



/ 319