[ Pobierz całość w formacie PDF ]
.To add this function to your project, add a new member functionto your new class, specifying the type as void, the declaration as SetRect(CRectrDrawArea), and the access as public.Edit the function as in Listing 16.1.LISTING 16.1.THE CModArt SetRect FUNCTION.1: void CModArt::SetRect(CRect rDrawArea)2: {3: // Set the drawing area rectangle4: m_rDrawArea = rDrawArea;5: }Creating a New DrawingOne of the key pieces to this module is the ability to generate random squiggles thatappear on the drawing area.By generating a whole series of these squiggles, your mod-ule will be able to create an entire drawing.Starting with the single squiggle, you candesign a function that generates one squiggle and then calls this function a number oftimes to generate the entire drawing.This first function, the squiggle generator, needs to determine how many lines will be inthe squiggle.It needs to determine the color and width of the pen to be used when draw-ing the squiggle.It also needs to determine the starting point for the squiggle.From thispoint, it could loop through the appropriate number of lines, generating a new destinationto continue the squiggle from the previous destination point.To add this functionality to your project, add a new member function to the drawingclass.Specify the function type as void, the definition as NewLine, and the access as pri-vate because this function will only be called by the master loop that is determining howmany of these squiggles will be in the final drawing.Edit the new function with the codein Listing 16.2.LISTING 16.2.THE CModArt NewLine FUNCTION.1: void CModArt::NewLine()2: {3: int lNumLines;4: int lCurLine;5: int nCurColor;6: UINT nCurWidth; 022 31240-9 CH16 4/27/00 12:56 PM Page 387Creating Your Own Classes and Modules 3877: CPoint pTo;8: CPoint pFrom;9:10: // Normalize the rectangle before determining the width and height11: m_rDrawArea.NormalizeRect();12: // get the area width and height13: int lWidth = m_rDrawArea.Width();14: int lHeight = m_rDrawArea.Height();15:16: // Determine the number of parts to this squiggle1617: lNumLines = rand() % 100;18: // Are there any parts to this squiggle?19: if (lNumLines > 0)20: {21: // Determine the color22: nCurColor = rand() % 8;23: // Determine the pen width24: nCurWidth = (rand() % 8) + 1;25: // Determine the starting point for the squiggle26: pFrom.x = (rand() % lWidth) + m_rDrawArea.left;27: pFrom.y = (rand() % lHeight) + m_rDrawArea.top;28: // Loop through the number of segments29: for (lCurLine = 0; lCurLine Delete();62: }63: // Set the starting point to the end point 022 31240-9 CH16 4/27/00 12:56 PM Page 401Creating Your Own Classes and Modules 40164: pFrom = pTo;65: }66: }67: }Now that you ve made all the necessary changes to the library module, compile it so thatit s ready for use in the test application.If you run your test application from the Start |Run Taskbar option, as in Figure 16.6, you ll notice that there is no noticeable difference16in how your application behaves.This is because the application hasn t changed.Theapplication is still using the old version of your library module.To get the test applica-tion to use the new version of the library module, reopen the test application project inVisual C++.Build the project, which should not do anything other than relink the pro-ject, and then run the application.You should see a significant difference in the drawingsthat your application is now generating, as shown in Figure 16.7.FIGURE 16.6.Run the test applica-tion from the Startmenu.FIGURE 16.7.The updated test appli-cation.SummaryToday you learned about how to approach creating and designing new classes for yourapplications.You learned the differences between the different types of classes that areavailable to you through the New Class Wizard in Visual C++.You also learned how youcan create a library module with a set of your functionality that you can hand to other 022 31240-9 CH16 4/27/00 12:56 PM Page 402402 Day 16programmers for including in their applications.You learned how this module will belinked into the actual applications, thus not requiring a separate file to be distributedalong with the applications.Tomorrow you will learn about a different approach to creating reusable packaged func-tionality that you can give to other programmers.You will learn how to create DLLsusing Visual C++, what the differences are between creating library modules and DLL,and how you need to approach each task.Q&AQ Isn t most functionality packaged in DLLs now? Why would I want to createlibrary modules instead of DLLs?A Yes, the trend toward packaging functionality modules has been to create DLLsinstead of library modules for a number of years now.However, there are stillinstances where library modules are preferable.If you are creating a module thatcontains proprietary functionality that you do not want to risk exposing to others,but that is needed for any applications that you or another programmer in yourcompany is building, then you would probably want all that functionality packagedin a library module so that it is internal to the application.Using library modulesmakes it effectively inaccessible to your competition without significant disassem-bly and reverse engineering efforts.Q Why does the header file need to be included in the application that is usingmy library file?A The application needs to know about the objects that are in the library file.In thesample application, you didn t need to include the header file for the CLine classbecause the application didn t directly use or reference the CLine class.However,the application did use the drawing object that was in your library module, so it didneed to know about that object, how it is defined, and what functions are availablefor it [ Pobierz całość w formacie PDF ]

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