![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
You can use the functions in Table 3.1 to format your documents as you want themSetCharFormat() 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 documents Serialize() function is already set up. Working with RTF DocumentsThe 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 documents 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 classs Serialize() function handles the details.
To get a look at this first hand, run the program now, as shown in Figure 3.2. We can use the File menus 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.
As you can see, installing a new MFC view type is easy. We continue now with form views. Form ViewsForm 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. Lets 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.
Designing the FormTo 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.
Now lets write the code for our calculator.
Programming the MDI CalculatorTo write the code for the controls in our calculator, open ClassWizard now and connect a member variable (using ClassWizards 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; ⇐ . . . }
|
![]() |
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. |