[ Pobierz całość w formacie PDF ]
.To fix this problem, reopen the resource file in Notepad and againadd the two separators at the end of the color toolbar definition.Then, reload theproject into Visual C++ and recompile the application.Q The combo box on my toolbars looks too big.How can I get it to fit within thetoolbar a little better?A To make the combo box fit within the toolbar like the combo boxes in the VisualC++ Developer Studio, you need to do a couple of things.First, lower the top ofthe combo box by 3; this places a small border between the top of the combo boxand the edge of the toolbar.Next, set the font in the combo box to a smaller fontthat will fit within the toolbar better.You can experiment with fonts and pitchesuntil you have a font that you like for the combo box in the toolbar. 016 31240-9 CH12 4/27/00 12:30 PM Page 277Adding Toolbars and Status Bars 277Q How can I set the text in the first section of the status bar other than by usingmenu and toolbar prompts?A You can use SetWindowText to set the text in the first pane of the status bar.As adefault setting, the first pane in the status bar is a separator that automaticallyexpands to fill the width of the status bar with the other panes right-justified on thebar.The SetWindowText function, called on the status bar variable, sets the text inthe first pane only.If you want to set the text in any other pane, at any other timethan in the ON_UPDATE_COMMAND_UI event handler, you can use the SetPaneTextfunction.There are two ways that you can set the text in the main part of the statusbar.The first is like this:CString myString =  This is my stringm_wndStatusBar.SetWindowText(myString);The other method isCString myString =  This is my stringm_wndStatusBar.SetPaneText(0, myString);WorkshopThe Workshop provides quiz questions to help you solidify your understanding of thematerial covered and exercises to provide you with experience in using what you velearned.The answers to the quiz questions and exercises are provided in Appendix B, Answers.Quiz121.How do you tie a toolbar button to a menu entry that triggers that same function?2.How do you make sure that a toolbar can be docked with the frame window?3.How can you remove the Num Lock status indicator from the status bar?4.Why do you have to edit the resource file to add a combo box to a toolbar?Exercises1.Add another pane to the status bar to display the current width selected.2.Add a button to the main toolbar that can be used to toggle the color toolbar onand off, as in Figure 12.7. 016 31240-9 CH12 4/27/00 12:30 PM Page 278278 Day 12FIGURE 12.7.The color toolbaron/off button. 017 31240-9 CH13 4/27/00 12:51 PM Page 279WEEK 2DAY 13Saving and RestoringWork File AccessMost applications provide the user the option of saving what has been created.The creation can be a word-processing document, a spreadsheet, a drawing, ora set of data records.Today, you will explore how Visual C++ provides youwith the means to implement this functionality easily.Today, you will learnHow Visual C++ uses C++ streams to save information about your appli-cationHow to store your application data in binary filesHow to make your application objects serializableHow you can store variables of differing data types into a single fileSerializationThere are two parts of serialization.When application data is stored on the sys-tem drive in the form of a file, it is called serialization.When the application 017 31240-9 CH13 4/27/00 12:51 PM Page 280280 Day 13state is restored from the file, it is called deserialization.The combination of these twoparts makes up the serialization of application objects in Visual C++.The CArchive and CFile ClassesSerialization in Visual C++ applications is accomplished through the CArchive class.TheCArchive class is designed to act as an input/output (I/O) stream for a CFile object, asshown in Figure 13.1.It uses C++ streams to enable efficient data flow to and from thefile that is the storage of the application data.The CArchive class cannot exist without aCFile class object to which it is attached.FIGURE 13.1.The CArchive classstores application datain a CFile object.ApplicationObjectSerializeFunctionCArchiveCFileThe CArchive class can store data in a number of types of files, all of which are descen-dants of the CFile class.By default, the AppWizard includes all the functionality to cre-ate and open regular CFile objects for use with CArchive.If you want or need to workwith one of these other file types, you might need to add additional code to your applica-tion to enable the use of these different file types. 017 31240-9 CH13 4/27/00 12:51 PM Page 281Saving and Restoring Work File Access 281The Serialize FunctionThe CArchive class is used in the Serialize function on the document and data objectsin Visual C++ applications.When an application is reading or writing a file, the docu-ment object s Serialize function is called, passing the CArchive object that is used towrite to or read from the file.In the Serialize function, the typical logic to follow is todetermine whether the archive is being written to or read from by calling the CArchiveIsStoring or IsLoading functions.The return value from either of these two functionsdetermines if your application needs to be writing to or reading from the CArchiveclass s I/O stream.A typical Serialize function in the view class looks like Listing 13.1.LISTING 13.1.A TYPICAL Serialize FUNCTION.1: void CAppDoc::Serialize(CArchive& ar)2: {3: // Is the archive being written to?4: if (ar.IsStoring())5: {6: // Yes, write my variable7: ar m_MyVar;13: }14: }You can place a Serialize function in any classes you create so that you can call theirSerialize function from the document Serialize function [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wpserwis.htw.pl