Visual C++ and MFC Fundamentals programming phần 10 doc

86 431 0
Visual C++ and MFC Fundamentals programming phần 10 doc

Đ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

Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals 654 © FunctionX, Inc. 4. To declare and associate a variable for a control, right-click the Container combo box and click Add Variable 5. In the Category combo box, select Value 6. In the Variable Type combo box, select int 7. In the Variable Name, type m_ContainerValue 8. Click Finish 9. On the dialog box, right-click the Flavor combo box and click Add Variable 10. In the Category combo box, accept the Control selected. In the Variable Name, type m_Flavors and press Enter 11. Add the following variables for the other controls (all the Value Variables are CString type): ID Control Variable Value Variable IDC_TOPPING m_Topping IDC_SCOOPS m_Scoops IDC_TAX_RATE m_TaxRate IDC_SUB_TOTAL m_SubTotal IDC_TAX_AMOUNT m_TaxAmount IDC_ORDER_TOTAL m_OrderTotal 12. Save All 20.2.3 Combo Box Methods A combo box is based on the CComboBox class. Therefore, if you want to dynamically create this control, declare a variable or a pointer to CComboBox using its default constructor. To initialize the control, call its Create() method. Here is an example: BOOL CExerciseDlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls © FunctionX, Inc. 655 // TODO: Add extra initialization here CComboBox *Majors = new CComboBox; Majors->Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST, CRect(10, 50, 100, 150), this, 0x1448); return TRUE; // return TRUE unless you set the focus to a control } After creating the control, probably the next action to take consists of creating its items. To add a string to a combo box, you can call the CComboBox::AddString() method that uses the same syntax as the CListBox::AddString() member function. Here is an example: BOOL CExerciseDlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CComboBox *Majors = new CComboBox; Majors->Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST, CRect(10, 50, 100, 150), this, 0x1448); Majors->AddString("Accounting"); Majors->AddString("Art Education"); Majors->AddString("Finance"); Majors->AddString("Biology"); return TRUE; // return TRUE unless you set the focus to a control } Most other methods are implemented as they are for the CListBox class. Practical Learning: Using Combo Box Methods 1. To create the list of flavors, in the OnInitDialog() event of the dialog class, type the following: CClarksvilleIceScream2Dlg::CClarksvilleIceScream2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CClarksvilleIceScream2Dlg::IDD, pParent) , m_ContainerValue(0) , m_TaxRate(_T("0.00")) , m_SubTotal(_T("0.00")) , m_TaxAmount(_T("0.00")) , m_OrderTotal(_T("0.00")) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } . . . Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals 656 © FunctionX, Inc. BOOL CClarksvilleIceScream2Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_Topping.SetCurSel(0); m_Scoops.SetCurSel(0); m_Flavors.AddString("French Vanilla"); m_Flavors.AddString("Cream of Cocoa"); m_Flavors.AddString("Chocolate Chip"); m_Flavors.AddString("Cherry Coke"); m_Flavors.AddString("Butter Pecan"); m_Flavors.AddString("Chocolate Cooky"); m_Flavors.AddString("Chunky Butter"); m_Flavors.AddString("Vanilla Strawberry"); m_Flavors.AddString("Macedoine"); m_Flavors.AddString("Mint Chocolate"); m_Flavors.SetCurSel(6); return TRUE; // return TRUE unless you set the focus to a control } 2. Save All 20.2.4 Combo Box Messages and Events When you add or create a combo box, an amount of space is allocated and during the lifetime of your application, the combo box uses some memory to processing its assignments. If at one time there is not enough memory for these processings, the combo box sends an ON_CBN_ERRSPACE message. Like any control, for the user to use the combo box, it must first have focus. This can be done by the user clicking its edit part, its list part (for a Simple combo box) or its down pointing arrow (for a drop type combo box). When the combo box receives focus, its sends the ON_CBN_SETFOCUS message. Once the control has focus, if the user clicks or had clicked the edit side of the combo box that already had a selected item, if the user starts typing, which would modify the string of the item that was already selected, the combo box would send the ON_CBN_EDITCHANGE message (remember that the user can change the string only if the combo box was not created as Drop List). If the user finishes typing or changing the string, just before the altered string is validated, the combo box sends the ON_CBN_EDITUPDATE message. To select an item from a drop type combo box, the user usually clicks the down pointing arrow button to display the control’s list. When the user clicks that button, just before the list displays, the control sends the ON_CBN_DROPDOWN message. After this click and this message, the list part of a drop combo box displays and the user must make a decision: Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls © FunctionX, Inc. 657 ?? If the user finds the desired item in the list, he or she must let the combo box know. This is done by highlighiting it. To do this, the user can either click (with the mouse) or press the arrow keys (on the keyboard) to indicate his or her choice. When this highlighting occurs, the combo box sends the ON_CBN_SELCHANGE message, notifying the application that the combo box’ selection may be changed soon ?? Once the user has found the desired item and has possibly highlighted it, if using the mouse, he or she can click to select it. If using the keyboard, after locating the item, the user can press Enter. Clicking the desired item or pressing Enter on the highlighted string means that the user has made a definite selection. This causes the control to send an ON_CBN_SELENDOK message, notifying the application that the user has made his or her decision. ?? If the user did not find the desired item, he or she may want to dismiss the combo box without making a selection. There are three main ways the user can invalidate a selection. If the user clicks another control or another application, if the list was displaying, it would retract and not selection would be made, event if the user had already highlighted an item. If the user clicks either the edit box part of the combo box for a Drop List type or the down pointing button, the selection is dismissed and if the list of a drop type was displaying, it would retract. If the user presses Esc, the selection would be dismissed. Any of these actions causes the selection to be dismissed or the user to cancel the selection action. This causes the combo box to send an ON_CBN_SELENDCANCEL message. Once the user has clicked an item or pressed Enter to validate a selection, if the combo box was created not as Simple, the list part of the controls retracts to hide itself. At this time, the combo box sends an ON_CBN_CLOSEUP message. If the user finishes using the combo box and moves to another control, the combo box sends an ON_CBN_KILLFOCUS message, notifying the application that it (the combo box) has lost focus. 20.3 Image Lists 20.3.1 Introduction An image list is an array of pictures of the same size. The pictures are created as a single icon or bitmap and each icon or bitmap can be located using its index. The array is zero- based, meansing that the first picture has an index of 0. The second has an index of 1, etc. An image list is not a traditional control. It does not display to the user who in fact is never aware of it. It is used to complement a control that needs a series of pictures for its own display. Practical Learning: Introducing Image Lists 1. Start a new MFC Application and name it AirCraft1 2. Create it as a Single Document based on CView, without the initial toolbar, and change the Main Frame Caption to Air Craft Review Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals 658 © FunctionX, Inc. 3. Access the PreCreateWindow() event of the CMainFrame class and change it as follows: BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style &= ~FWS_ADDTOTITLE; cs.cx = 420; cs.cy = 280; return TRUE; } 4. Test the application and return to MSVC 20.3.2 Image List Creation In an MFC application, an image list is based on the CImageList class. This object is created in two main steps that do not necessarily follow each other. On one hand, you must have the pictures that will make the list. On the other hand, you must have a CImageList variable or pointer. The easiest way is probaly to first create the picture. There are two kinds: masked or nonmasked. A nonmasked image list is designed as an array of pictures where all pictures have the same width and the same height but the pictures do not have to be square. Here is an example: A masked image list contains two pictures of the same with and height. Unlike the unmasked image list, both pictures of the masked image normally represent the same illustration. The first picture is in color and the second would be monochrome. Here is an example: To actually create an image list, declare a CImageList variable or pointer and call one of its Create() methods to initialize it. It is provided in various versions as follows: BOOL Create(int cx, int cy, UINT nFlags, int nInitial, int nGrow ); BOOL Create(UINT nBitmapID, int cx, int nGrow , COLORREF crMask ); BOOL Create(LPCTSTR lpszBitmapID, int cx, int nGrow , COLORREF crMask ); BOOL Create(CImageList& imagelist1, int nImage1, CImageList& imagelist2, int nImage2, int dx, int dy); BOOL Create(CImageList* pImageList); The first version of this method allows you to describe the type of image list that will be created. This is done by specifying the width (cx) of each picture, the height (cy) of each picture, and a flag for the type of image list to create. The nInitial argument is the number Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls © FunctionX, Inc. 659 of images that the image list will contain. The nGrow argument represents the number of images by which the image list can grow. If you had designed an unmasked bitmap using the image editor and you want to initialize it, call the second or the third versions of the Create() method. The nBitmapID argument is the identifier of the bitmap. If you want to provide the string that contains the identifiers of the images, pass it as the lpszBitmapID argument. The cx value represents the width of each picture. The crMask is the color used to mask the transparency of the picture. Each pixel of the picture that matches this color will turn to black. Here is an example of using this Create() method: BOOL CAnimation1Dlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CImageList ImgList; ImgList.Create(IDB_IMGLIST, 48, 4, RGB(255, 55, 5)); return TRUE; // return TRUE unless you set the focus to a control } Besides, or instead of using, the Create() method, you can call CImageList::Add() to add a bitmap to the CImageList variable. Its syntaxes are: int Add(CBitmap* pbmImage, CBitmap* pbmMask); int A dd(CBitmap* pbmImage, COLORREF crMask); int Add(HICON hIcon ); The pbmImage argument represents the bitmap to be added, unless you want to add an icon, in which case you would use the third version. The pbmMask argument is the bitmap that will be used to mask the image list. You can use a color instead, in which case you would pass a COLORREF value as the second argument to the second version. If you want to remove a picture from the image list, call the CImageList::Remove() method. Its syntax: BOOL Remo ve(int nImage); The nImage argument is the index of the picture to be removed. Instead of removing a picture, you can just replace it with another picture. This is done using the CImageList::Replace() method whose syntaxes are: BOOL Replace(int nImage, CBitmap* pbmImage, CBitmap* pbmMask); int Replace(int nImage, HICON hIcon); Once an image list is ready, you can use it directly in an application or make it available to a control that can use it. One way you can use an image list is to display one or more of its pictures on a dialog box, a form, or a view. To do this, you would call the CImageList::Draw() method. Its syntax is: BOOL Draw(CDC* pdc, int nImage, POINT pt , UINT nStyle); Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals 660 © FunctionX, Inc. The first argument, pdc, specifies the device context on which you are drawing. The nImage argument is the index of the picture you want to draw. The pt argument is a POINT or a CPoint value that specifies the location of the new picture. The nStyle argument is a flag that specifies how the picture will be drawn. Practical Learning: Using an Image List 1. From the AirCraft folder that accompanies this book, import the following bitmaps and change their IDs as follows: File ID AH64.bmp IDB_AH64 AH64SIDE.bmp IDB_AH64SIDE AKIOWA.bmp IDB_AKIOWA COMANCHE.bmp IDB_COMANCHE 2. In the header file of the view class, declare a private CImageList variable and name it ImgList 3. In the header file of the view class, declare another private int variable and name it nImage 4. In the constructor of the view class, initialize the image list and the nImage variable as follows: CAirCraft1View::CAirCraft1View() { // TODO: add construction code here ImgList.Create(400, 180, ILC_COLOR, 4, 1); CBitmap Bmp[4]; Bmp[0].LoadBitmap(IDB_AH64); ImgList.Add(&Bmp[0], RGB(0, 0, 0)); Bmp[1].LoadBitmap(IDB_AH64SIDE); ImgList.Add(&Bmp[1], RGB(0, 0, 0)); Bmp[2].LoadBitmap(IDB_AKIOWA); ImgList.Add(&Bmp[2], RGB(0, 0, 0)); Bmp[3].LoadBitmap(IDB_COMANCHE); ImgList.Add(&Bmp[3], RGB(0, 0, 0)); nImage = 0; } 5. In the OnPaint() event of the view class, display an image by calling the CImageList::Draw() method as follows: void CAirCraft1View::OnDraw(CDC* pDC) { CAirCraft1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); ImgList.Draw(pDC, nImage, CPoint(255, 255, 255), ILD_NORMAL); // TODO: add draw code for native data here } 6. Open the IDR_MAINFRAME menu. Under the Status Bar menu item of View, add a separator, followed by a new menu item captioned as &AH64 and press Enter Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls © FunctionX, Inc. 661 7. Right-click the new menu item and click Add Event Handler… 8. Accept the Message Type as COMMAND. In the Class List, click CAirCraft1View. Accept the name of the function. Then click Add And Edit and implement the event as follows: void CAirCraft1View::OnViewAh64() { // TODO: Add your command handler code here nImage = 0; Invalidate(); } 9. Again, under the View menu, create a new menu item with a caption as A&H64 Side and press Enter 10. Add a COMMAND Event Handler for the new menu item. Associate it with the view class and implement it as follows: void CAirCraft1View::OnViewAh64side() { // TODO: Add your command handler code here nImage = 1; Invalidate(); } 11. Again, under the View menu, create a new menu item with a caption as A&kiowa and press Enter 12. Add a COMMAND Event Handler for the new menu item. Associate it with the view class and implement it as follows: void CAirCraft1View::OnViewAkiowa() { // TODO: Add your command handler code here nImage = 2; Invalidate(); } 13. Again, under the View menu, create a new menu item with a caption as &Commanche and press Enter 14. Add a COMMAND Event Handler for the new menu item. Associate it with the view class and implement it as follows: void CAirCraft1View::OnViewCommanche() { // TODO: Add your command handler code here nImage = 3; Invalidate(); } 15. Test the application Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals 662 © FunctionX, Inc. 16. Close it and return to MSVC Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls © FunctionX, Inc. 663 Chapter 21: Tree and List Controls ? The Tree Control ? The Tree View ? The List Control ? The List View [...]... Tree and List Controls Visual C++ and MFC Fundamentals TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS | TVS_TRACKSELECT, CRect (10, 10, 200, 100 ), this, 0x1221); TreeSoft ->InsertItem("Office Production"); return TRUE; // return TRUE unless you set the focus to a control } In this case, the item would appear as the root You can add as many nodes like that and each... WS_BORDER | WS_TABSTOP, CRect (10, 10, 240, 280), this, 0x1221); } return TRUE; // return TRUE unless you set the focus to a control Practical Learning: Creating a Tree List 1 666 Create a new Dialog Based MFC Application named CarInventory2 without the About Box and set the Dialog Title to Car Inventory © FunctionX, Inc Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls 2 Resize the... FunctionX, Inc Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls Always property to True or create the control with the TVS_SHOWSELALWAYS style Besides selecting an item, when a node has children, to expand it, the user can doubleclick the node or click its button if available Simply selecting the node does not expand it If you want the selected node to automatically expand without the... FunctionX, Inc Visual C++ and MFC Fundamentals SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); Chapter 21: Tree and List Controls // Set big icon // Set small icon // TODO: Add extra initialization here TreeSoft ->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS | TVS_TRACKSELECT, CRect (10, 10, 200, 200),... application © FunctionX, Inc Visual C++ and MFC Fundamentals 7 Chapter 21: Tree and List Controls Close it and return to MSVC 21.2 The Tree View 21.2.1 Overview A tree view is a frame -based application whose view uses the characteristics of a tree control To create such an application, you can work from scratch and derive a class from CTreeView Alternatively, you can use the MFC Application wizard to... tree designer The concept of a tree list is implemented in the MFC library by the CTreeCtrl To create a tree list on a dialog box or a form, at design time, on the Controls toolbox, click the Tree Control button © FunctionX, Inc and click the desired area on a dialog box or a form: 665 Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals Figure 59: A Newly added Tree Control Alternatively,... TRUE; // return TRUE unless you set the focus to a control } 2 Execute the program to test the tree control 3 Close it and return to MSVC © FunctionX, Inc 673 Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals 4 Import the cars from the Cars folder that accompany this book and change their IDs accordingly to their file name An example would be IDB_CIVIC for the civic.mdb 5 Design the rest... TRUE; m_CDPlayer = FALSE; } else if( ItemSelected == "PD-304" ) { m_Make.Format("%s", "Mercury"); © FunctionX, Inc 677 Chapter 21: Tree and List Controls } else { Visual C++ and MFC Fundamentals m_Model.Format("%s", "Grand Marquis"); m_Year = 2000; m_Doors = 4; m_Mileage = 109 442; m_Transmission = 0; m_AC = TRUE; m_AirBags = TRUE; m_CruiseControl = TRUE; m_Convertible = FALSE; m_Cassette = TRUE; m_CDPlayer... application Close it and return to MSVC © FunctionX, Inc Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls 21.1.5 Tree Control With Bitmaps or Icons Bitmaps can be used to enhanced the display of items on a tree control Each tree item can be configured to display or not to display a small picture on its left To do this , you can declare a CImageList variable and add pictures to it... Resource dialog box and double-click Bitmap 2 On the Properties, change its ID to IDB_IMGTREE 3 Design is as follows: 4 In the header file of the dialog, declare a private CImageList variable and name it ImgList 5 To assign the pictures to the tree items, change the OnInitDialog() event of the dialog box as follows: © FunctionX, Inc 679 Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals BOOL . List-Based Controls Visual C++ and MFC Fundamentals 662 © FunctionX, Inc. 16. Close it and return to MSVC Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls . as &AH64 and press Enter Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls © FunctionX, Inc. 661 7. Right-click the new menu item and click Add Event Handler… 8. Accept. List-Based Controls Visual C++ and MFC Fundamentals 654 © FunctionX, Inc. 4. To declare and associate a variable for a control, right-click the Container combo box and click Add Variable

Ngày đăng: 06/08/2014, 17:20

Từ khóa liên quan

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

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

Tài liệu liên quan