Teach Yourself Visual C++ 6 in21 Days phần 3 docx

80 239 0
Teach Yourself Visual C++ 6 in21 Days phần 3 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Working with Text and Fonts 141 7 2. Add a check box to display the font sample in italics, as in Figure 7.5. FIGURE 7.4. Displaying the selected font with the font name. FIGURE 7.5. Displaying the selected font in italics. 009 31240-9 CH07 4/27/00 11:49 AM Page 141 009 31240-9 CH07 4/27/00 11:49 AM Page 142 In Review Well, you’ve made it through the first week. By this point, you’ve gotten a good taste for what’s possible when building applications with Visual C++. Now it’s time to look back over what’s been covered and what you should have learned up to this point. What you might want to do at this point, to cement your understanding of how you can use these elements in your own applications, is to try designing and building a couple of simple applications of your own. You can use a variety of controls and add some additional dialogs, just so you can make sure that you do understand and are comfortable with these topics. In fact, you might want to try out all the topics that I’ve covered up to this point in small applications of your own design. That’s the true test of your understanding of how the concepts work. You might also want to dive into the MFC documentation to learn a little about some of the more advanced functionality that I haven’t covered to see if you can figure out how you can use and incorporate it into your applications. One of the most important things that you should understand at this point is how you can use controls and dialog windows in your applications to get and display information to the user. This is an important part of any Windows application because just about every application interacts with the user in some way. You should be able to place any of the standard controls on a dialog in your application and be able to incor- porate them into your application without any problem. Likewise, you should be comfortable with using the standard message box and dialog windows provided to your applica- tion by the Windows operating system. You should also be WEEK 1 1 2 3 4 5 6 7 010 31240-9 Rev 1 4/27/00 11:50 AM Page 143 144 Week 1 able to create and incorporate your own custom dialog windows into any application you might want to build. If you don’t feel comfortable with any of these topics, you might want to go back and review Day 2 to get a better understanding of how you can use con- trols and Day 5 to understand how you can incorporate standard and custom dialog win- dows into your applications. Another key skill that you will be using in the majority of your applications is the ability to build and incorporate menus into your applications. You need to have a firm under- standing of how to design a good menu, how to make sure that there are no conflicting mnemonics, and how you can attach application functionality to the menu selections. At this point, you should be able to create your own customized menus, with entries for each of the various functions that your application performs, and integrate it with your application with no problems. If you aren’t 100% comfortable with this topic, you might want to go back and study Day 6 a little more. You will find that there are various situations in which you need to have some means of triggering actions on a regular basis or in which you need to keep track of how long some process has been running. For both of these situations, as well as numerous others, you’ll often find yourself turning to the use of timers in your application. If you are even slightly foggy on how you can integrate timers into your applications, you will definitely want to go back and review Day 4. Understanding how you can use text and fonts in your applications will allow you to build more flexibility into the appearance of your applications—to give your users the ability to customize the appearance as they want. You will be able to examine the avail- able fonts on the computer on which your application is running and, if a font that you want to use in your application isn’t available, choose another font that is close to use instead. If you still have any questions on how the font infrastructure in Windows works and how you can use it in your applications, you’ll want to go back and review Day 7 once more. Depending on the nature of your application, being able to capture and track mouse and keyboard actions by the user can be very important. If you are building a drawing appli- cation, this is crucial information. If you are building an application that needs to include drag-and-drop capabilities, this is important once again. There are any number of situa- tions in which you’ll want to include this functionality into your applications. By this point, you should understand how you can capture the various mouse events and deter- mine which mouse buttons are involved in the event. You should also be able to capture keyboard events in situations where the keyboard input isn’t captured by any controls that are on the window. If you don’t feel like you have a complete grasp of this, you should take another look at Day 3. 010 31240-9 Rev 1 4/27/00 11:50 AM Page 144 In Review 145 Finally, you should be familiar with the Visual C++ development environment, the Developer Studio. You should have a good understanding of what each area of the envi- ronment is for and how you can use the various tools and utilities in building your appli- cations. You should be comfortable with using the workspace pane to navigate around your application project, locating and bringing into the various editors and designers any part of your application. You should be comfortable with locating and redesigning the icon that will be displayed to represent your application and with finding any member functions or variables in any of your application’s classes. By now you should be getting fairly comfortable working with Visual C++. If you feel like you understand all the topics that I’ve covered so far, you are ready to continue for- ward, learning more about the various things that you can do, and functionality that you can build, using Visual C++ as your programming tool. With that said, it’s on to the sec- ond week… 010 31240-9 Rev 1 4/27/00 11:50 AM Page 145 010 31240-9 Rev 1 4/27/00 11:50 AM Page 146 At a Glance In the second week, you’ll dive into several more involved topics. These topics are still very much core to building Windows applications. You’ll find yourself using what you learn in this week, along with what you learned during the first week, in just about all the applications that you build with Visual C++. To start the week, on Day 8, you’ll learn how to draw graphics in a Windows application. You’ll learn how to draw simple lines, rectangles, and ellipses. What’s more important—you’ll learn about the device context and how you can use it to draw your graphics without worrying about the graphics hardware your users might or might not have in their computers. On Day 9, you’ll learn how easy it is to incorporate ActiveX controls into your applications. You’ll see how Visual C++ builds custom C++ classes around the controls that you add to your project, enabling you to interact with an added control just as if it were another C++ object. On Day 10, you’ll learn how to build a basic Single Document Interface (SDI) application. You’ll learn about the Document/View architecture that is used with Visual C++ for building this style of application, and you’ll learn how you can use it to build your own applications. On Day 11, you’ll learn how you can apply what you learned about building SDI applications to building Multiple Document Interface (MDI) applications. You’ll see how you can use the same Document/View architecture to create MDI applications, some of the most common style of Windows applications available today. WEEK 2 8 9 10 11 12 13 14 011 31240-9 AAG Wk2 4/27/00 11:53 AM Page 147 148 Week 2 On Day 12, you’ll learn how you can create and modify your own toolbars and status bars. You’ll learn how you can attach toolbar buttons to menus in your application and how you can add additional toolbars. You’ll also learn how you can place your own informational elements on the status bar at the bottom of most Windows applications and how you can keep the status bar updated with the status of your application. On Day 13, you’ll see how you can use the structure provided for you by the Document/View architecture to save and restore the data created in your application. You’ll learn how flexible this facility is and how you can store different data types in the same file, restoring them to your application just as they were when you first saved them. Finally, rounding out the week on Day 14, you’ll learn how easy it is to build a database application with an ODBC database. You’ll learn how to query a set of records from the database and how to allow the user to edit and modify them, saving the changes back to the database. When you finish this week, you’ll be well prepared for tackling most basic application development tasks with Visual C++. You might want to take a short break at that point to experiment a bit—trying to build various types of applications, pushing your skills, and learning what your limits are (and aren’t)—before jumping into the final week of more advanced topics. 011 31240-9 AAG Wk2 4/27/00 11:53 AM Page 148 DAY 8 WEEK 2 Adding Flash— Incorporating Graphics, Drawing, and Bitmaps You’ve probably noticed that a large number of applications use graphics and display images. This adds a certain level of flash and polish to the application. With some applications, graphics are an integral part of their functionality. Having a good understanding of what’s involved in adding these capabilities to your applications is a key part of programming for the Windows platform. You’ve already learned how you can draw lines and how you can string a series of these lines together to make a continuous drawing. Today, you’re going to go beyond that capacity and learn how you can add more advanced graphics capa- bilities to your applications. Today, you will learn • How Windows uses a device context to translate drawing instructions into graphics output. • How you can determine the level of control you have over the graphics output through different mapping modes. 012 31240-9 CH08 4/27/00 11:54 AM Page 149 • How Windows uses pens and brushes to draw different portions of the graphics image. • How you can load and display bitmaps dynamically. Understanding the Graphics Device Interface The Windows operating system provides you with a couple of levels of abstraction for creating and using graphics in your applications. During the days of DOS programming, you needed to exercise a great deal of control over the graphics hardware to draw any kind of images in an application. This control required an extensive knowledge and understanding of the various types of graphics cards that users might have in their com- puters, along with their options for monitors and resolutions. There were a few graphics libraries that you could buy for your applications, but overall, it was fairly strenuous pro- gramming to add this capability to your applications. With Windows, Microsoft has made the job much easier. First, Microsoft provides you with a virtual graphics device for all of your Windows applications. This virtual device doesn’t change with the hardware but remains the same for all possible graphics hard- ware that the user might have. This consistency provides you with the ability to create whatever kind of graphics you want in your applications because you know that the task of converting them to something that the hardware understands isn’t your problem. Device Contexts Before you can create any graphics, you must have the device context in which the graphics will be displayed. The device context contains information about the system, the application, and the window in which you are drawing any graphics. The operating system uses the device context to learn in which context a graphic is being drawn, how much of the area is visible, and where on the screen it is currently located. When you draw graphics, you always draw them in the context of an application win- dow. At any time, this window may be full view, minimized, partly hidden, or complete- ly hidden. This status is not your concern because you draw your graphics on the win- dow using its device context. Windows keeps track of each device context and uses it to determine how much and what part of the graphics you draw to actually display for the user. In essence, the device context you use to display your graphics is the visual context of the window in which you draw them. The device context uses two resources to perform most of its drawing and graphics func- tions. These two resources are pens and brushes. Much like their real-world counterparts, pens and brushes perform similar yet different tasks. The device context uses pens to 150 Day 8 012 31240-9 CH08 4/27/00 11:54 AM Page 150 [...]... lRect.top; 24: pStart.x = lRect.left; continues 8 012 31 240-9 CH08 4/27/00 11:54 AM 166 Page 166 Day 8 LISTING 8.5 25: 26: 27: 28: 29: 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : 39 : 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60 : 61 : 62 : 63 : 64 : 65 : } CONTINUED pEnd.y = pStart.y; pEnd.x = lRect.right; int i; // Loop through the different pens for (i = 0; i < 7; i++) { // Which... 43: lDrawRect.left = lRect.left + 5; 44: lDrawRect.bottom = lDrawRect.top + liHeight; 45: lDrawRect.right = lDrawRect.left + liWidth; 46: // Select the appropriate pen and brush continues 8 012 31 240-9 CH08 4/27/00 11:54 AM 170 Page 170 Day 8 LISTING 8.7 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60 : 61 : 62 : 63 : 64 : 65 : 66 : 67 : 68 : 69 : 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: ... regions 23: CRect lRect; 24: GetClientRect(lRect); 25: lRect.NormalizeRect(); 26: int liVert = lRect.Height() / 2; 27: int liHeight = liVert - 10; 28: int liHorz = lRect.Width() / 4; 29: int liWidth = liHorz - 10; 30 : CRect lDrawRect; 31 : CPen *lOldPen; 32 : CBrush *lOldBrush; 33 : int i; 34 : // Loop through all of the brushes and pens 35 : for (i = 0; i < 7; i++) 36 : { 37 : switch (i) 38 : { 39 : case 0:... FUNCTION 1: BOOL CGraphicsDlg::OnInitDialog() 2: { 3: CDialog::OnInitDialog(); 4: 27: 28: // TODO: Add extra initialization here 29: 30 : /////////////////////// 31 : // MY CODE STARTS HERE 32 : /////////////////////// 33 : 34 : // Initialize the variables and update the dialog window 35 : m_iColor = 0; 36 : m_iShape = 0; 37 : m_iTool = 0; 38 : UpdateData(FALSE); 39 : 40: // Create the second dialog window 41:... Open dialog and capture the result 12: if (m_ldFile.DoModal() == IDOK) 13: { 14: // Get the filename selected 15: m_sBitmap = m_ldFile.GetPathName(); 16: // Load the selected bitmap file continues 8 012 31 240-9 CH08 4/27/00 11:54 AM Page 174 174 Day 8 LISTING 8.9 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30 : 31 : 32 : 33 : 34 : } CONTINUED HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),... can write a single event handler that does these two things with the code in Listing 8 .3 1 63 8 012 31 240-9 CH08 4/27/00 11:54 AM Page 164 164 Day 8 LISTING 8 .3 THE OnRSelection FUNCTION 1: void CGraphicsDlg::OnRSelection() 2: { 3: // TODO: Add your control notification handler code here 4: 5: // Synchronize the data 6: UpdateData(TRUE); 7: // Repaint the second dialog 8: m_dlgPaint.Invalidate(); 9: }... area 13: CRect lRect; 14: GetClientRect(lRect); 15: lRect.NormalizeRect(); 16: 17: // Calculate the distance between each of the lines 18: CPoint pStart; 19: CPoint pEnd; 20: int liDist = lRect.Height() / 8; 21: CPen *lOldPen; 22: // Specify the starting points 23: pStart.y = lRect.top; 24: pStart.x = lRect.left; continues 8 012 31 240-9 CH08 4/27/00 11:54 AM 166 Page 166 Day 8 LISTING 8.5 25: 26: 27:... pdc->SelectObject(&lNullPen); pdc->SelectObject(&lHorizBrush); break; case 6: // Inside - Vertical // Determine the location for this figure 012 31 240-9 CH08 4/27/00 11:54 AM Page 171 Adding Flash—Incorporating Graphics, Drawing, and Bitmaps 95: 96: 97: 98: 99: 100: 101: 102: 1 03: 104: 105: 1 06: 107: 108: 109: 110: 111: 112: 1 13: 114: 115: 1 16: } 171 lDrawRect.left = lDrawRect.left + liHorz; lDrawRect.right... and destructor LISTING 8.4 THE 1: 2: 3: 4: 5: 6: 7: COLOR TABLE const COLORREF CPaintDlg::m_crColors[8] = { RGB( 0, 0, 0), // Black RGB( 0, 0, 255), // Blue RGB( 0, 255, 0), // Green RGB( 0, 255, 255), // Cyan RGB( 255, 0, 0), // Red RGB( 255, 0, 255), // Magenta 012 31 240-9 CH08 4/27/00 11:54 AM Page 165 Adding Flash—Incorporating Graphics, Drawing, and Bitmaps 165 8: RGB( 255, 255, 0), // Yellow 9:... UpdateData(FALSE); 39 : 40: // Create the second dialog window 41: m_dlgPaint.Create(IDD_PAINT_DLG, this); 42: // Show the second dialog window 43: m_dlgPaint.ShowWindow(SW_SHOW); continues 8 012 31 240-9 CH08 4/27/00 11:54 AM Page 162 162 Day 8 LISTING 8.1 44: 45: 46: 47: 48: 49: 50: } CONTINUED /////////////////////// // MY CODE ENDS HERE /////////////////////// return TRUE; // return TRUE unless you set . build, using Visual C++ as your programming tool. With that said, it’s on to the sec- ond week… 010 31 240-9 Rev 1 4/27/00 11:50 AM Page 145 010 31 240-9 Rev 1 4/27/00 11:50 AM Page 1 46 At a Glance. this, you should take another look at Day 3. 010 31 240-9 Rev 1 4/27/00 11:50 AM Page 144 In Review 145 Finally, you should be familiar with the Visual C++ development environment, the Developer. applica- tion by the Windows operating system. You should also be WEEK 1 1 2 3 4 5 6 7 010 31 240-9 Rev 1 4/27/00 11:50 AM Page 1 43 144 Week 1 able to create and incorporate your own custom dialog windows

Ngày đăng: 13/08/2014, 08:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan