Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
You can use the functions in Table 3.1 to format your documents as you want them—SetCharFormat() sets the format of selected characters, and SetParaFormat() sets the format of paragraphs. In fact, you can already read and save RTF files, because the document’s Serialize() function is already set up. #### Working with RTF Documents The Serialize() function looks like this:
    void CRichEditViewDoc::Serialize(CArchive& ar)
    {
        if (ar.IsStoring())
        {
            // TODO: add storing code here
        }
        else
        {
            // TODO: add loading code here
        }

        // Calling the base class CRichEditDoc enables serialization
        //  of the container document’s COleClientItem objects.
        // TODO: set CRichEditDoc::m_bRTF = FALSE if you are serializing as text
        CRichEditDoc::Serialize(ar);
    }
Note the comment at the bottom of Serialize(); you can already handle rich text files with our program, because the CRichEditDoc base class’s Serialize() function handles the details. Handling Straight Text Documents If you want to work with straight text (that is, .txt files) instead of rich format text, set the m_bRTF data member to false in Serialize(). To get a look at this first hand, run the program now, as shown in Figure 3.2. We can use the File menu’s Open item to read in a rich text document. As you can see in that figure, all the aspects of rich text in the document are supported, and our rich text example is a success. Drag and Drop The rich edit view supports drag and drop, so you can drag a file to the rich edit view and drop it to open it in that view. Figure 3.2 Supporting rich text. As you can see, installing a new MFC view type is easy. We continue now with form views. ### Form Views Form views provide us with all the functionality of dialog-based applications, except that form views are views and can appear in MDI child windows. As shown in Chapter 1, “Up to Speed in Visual C++,” we can use the Dialog Editor to design our form view. Let’s put this into action now. We can create a small calculator view based on a form view; this calculator appears as a view (possibly one of many) in an MDI program so the user can add numbers. Create a new MDI project named FormView. In Step 6 of the AppWizard, select the view class in the Class name box and select CFormView (this class has only two functions: constructor and destructor) as the base class for that view, then click the Finish button. This creates the FormView project. The next step is to design the form as we want it to appear. Figure 3.3 Our original form view. #### Designing the Form To design the form, simply open it by double-clicking the IDD_FORMVIEW _FORM entry in the Dialog folder of the ResourceView now, as shown in Figure 3.3. This is the form on which we design our calculator. Using the tools in the toolbox, add two edit views, a command button with the caption “=”, and a label with the caption “+”:
                 ------------------------------------------
                |                                                    |
                |                -----------                         |
                |               |           |            |   IDC_EDIT1
                |                -----------                         |
                |                     +                              |
                |                -----------                         |
                |               |           |             |  IDC_EDIT2
                |                -----------                         |
                |                                                    |
                |                -----------                         |
                |               |     =     |           |  IDC_BUTTON1
                |                -----------                         |
                |                                                    |
                |                -----------                         |
                |               |           |             |  IDC_EDIT3
                |                -----------                         |
                |                                                    |
                 ------------------------------------------
When the user places numbers in the first two text boxes and clicks the = button, our program adds the two numbers and places the sum in the bottom text box.
                 ------------------------------------------
                |                                     |
                |                -----------          |
                |               |12         |         |
                |                -----------          |
                |                     +                             |
                |                -----------                        |
                |               |24         |         |
                |                -----------          |
                |                                     |
                |                -----------          |
                |               |     =     |         |
                |                -----------          |
                |                                     |
                |                -----------                        |
                |               |36         |         |
                |                -----------                        |
                |                                                   |
                 ------------------------------------------
When you place those controls into the form view, they should appear as shown in Figure 3.4. Aligning and Spacing Controls in a Form View Similar to placing controls in a dialog box or dialog window, the Layout menu in Visual C++ is very handy, allowing you to size, space, and align a number of controls at once. Now let’s write the code for our calculator. Figure 3.4 The calculator form view. #### Programming the MDI Calculator To write the code for the controls in our calculator, open ClassWizard now and connect a member variable (using ClassWizard’s Member Variables tab) to IDC_EDIT1 named m_value1, IDC_EDIT2 named m_value2, and IDC_EDIT3 named m_sum. Then use ClassWizard to connect a member function, OnButton1(), to the command button with the equals caption This is the function that the program calls when the user clicks the = button; it calculates the sum of the two numbers in the top two text boxes, and the result is shown in the bottom text box.
    void CFormViewView::OnButton1()
    {
        // TODO: Add your control notification handler code here
    }
All that remains is to take the numbers from m_value1 and m_value2, add them, and place the sum in m_sum. We start that process with the UpdateData() function, passing a value of true to update the member variables from the form view.
    void CFormViewView::OnButton1()
    {
        UpdateData(true);
            .
            .
            .
Next, we use the atoi() function to convert the strings in m_value1 and m_value2 to the integers value and value2.
    void CFormViewView::OnButton1()
    {
        UpdateData(true);

        int value1 = atoi(m_value1);               ⇐
        int value2 = atoi(m_value2);               ⇐
            .
            .
            .
Finally, we write the code to add these values and display the result. We create the sum and use wsprintf() to place the sum in a character string that we move to m_sum.
    void CFormViewView::OnButton1()
    {
        UpdateData(true);

        int value1 = atoi(m_value1);
        int value2 = atoi(m_value2);

        char OutputString[20];                                    ⇐

        wsprintf(OutputString, “%ld”, value1 + value2);           ⇐

        m_sum = OutputString;                                     ⇐
            .
            .
            .
    }
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.