怎样使用TabControl呢?第一次用这东东,好像不太好弄!
或者是在一个基于对话框的应用程序中不用TabControl而达到TabControl的效果也可以,宗旨就是选择一种简单有效的办法了哪位高人指点一下了!据说TabControl要绑定对话框?这个过程怎么实现呢?

解决方案 »

  1.   

    Property Sheets
    You've already seen property sheets in Visual C++ and in many other modern Windows-based programs. A property sheet is a nice UI element that allows you to cram lots of categorized information into a small dialog. The user selects pages by clicking on their tabs. Windows offers a tab control that you can insert in a dialog, but it's more likely that you'll want to put dialogs inside the tab control. The MFC library supports this, and the result is called a property sheet. The individual dialogs are called property pages. Building a Property Sheet
    Follow these general steps to build a property sheet using the Visual C++ tools: Use the resource editor to create a series of dialog templates that are all approximately the same size. The captions are the strings that you want to display on the tabs. Use ClassWizard to generate a class for each template. Select CPropertyPage as the base class. Add data members for the controls. Use ClassWizard to generate a single class derived from CPropertySheet. To the sheet class, add one data member for each page class. In the sheet class constructor, call the AddPage member function for each page, specifying the address of the embedded page object. In your application, construct an object of the derived CPropertySheet class, and then call DoModal. You must specify a caption in the constructor call, but you can change the caption later by calling CPropertySheet::SetTitle. Take care of programming for the Apply button.Property Sheet Data Exchange
    The framework puts three buttons on a property sheet. (See, for example, Figure 13-5.) Be aware that the framework calls the Dialog Data Exchange (DDX) code for a property page each time the user switches to and from that page. As you would expect, the framework calls the DDX code for a page when the user clicks OK, thus updating that page's data members. From these statements, you can conclude that all data members for all pages are updated when the user clicks OK to exit the sheet. All this with no C++ programming on your part!With a normal modal dialog, if the user clicks the Cancel button, the changes are discarded and the dialog class data members remain unchanged. With a property sheet, however, the data members are updated if the user changes one page and then moves to another, even if the user exits by clicking the Cancel button. What does the Apply button do? Nothing at all if you don't write some code. It won't even be enabled. To enable it for a given page, you must set the page's modified flag by calling SetModified(TRUE) when you detect that the user has made changes on the page. If you've enabled the Apply button, you can write a handler function for it in your page class by overriding the virtual CPropertyPage::OnApply function. Don't try to understand property page message processing in the context of normal modal dialogs; it's quite different. The framework gets a WM_NOTIFY message for all button clicks. It calls the DDX code for the page if the OK or Apply button was clicked. It then calls the virtual OnApply functions for all the pages, and it resets the modified flag, which disables the Apply button. Don't forget that the DDX code has already been called to update the data members in all pages, so you need to override OnApply in only one page class. What you put in your OnApply function is your business, but one option is to send a user-defined message to the object that created the property sheet. The message handler can get the property page data members and process them. Meanwhile, the property sheet stays on the screen. 
    就是用属性单,先做几个对话框,从CPropertyPage继承,然后添加CPropertySheet的派生类,把对话框加入到派生类中。VC技术内幕的有例子,对话框就是属性页.
      

  2.   

    The EX13A Example
    This example illustrates the routing of menu and keyboard accelerator commands to both documents and views. The application's view class is derived from CView and contains a rich edit control. View-directed menu commands, originating from a new pop-up menu named Transfer, move data between the view object and the document object, and a Clear Document menu item erases the document's contents. On the Transfer menu, the Store Data In Document item is grayed when the view hasn't been modified since the last time the data was transferred. The Clear Document item, located on the Edit menu, is grayed when the document is empty. Figure 13-4 shows the first version of the EX13A program in use. Figure 13-4. The EX13A program in use. If we exploited the document-view architecture fully, we would tell the rich edit control to keep its text inside the document, but that's rather difficult to do. Instead, we'll define a document CString data member named m_strText, the contents of which the user can transfer to and from the control. The initial value of m_strText is a Hello message; choosing Clear Document from the Edit menu sets it to empty. By running this example, you'll start to understand the separation of the document and the view. The first part of the EX13A example exercises Visual C++'s wysiwyg menu editor and keyboard accelerator editor together with ClassWizard. You'll need to do very little C++ coding. Simply follow these steps: 
    Run AppWizard to generate \vcpp32\ex13a\ex13a. Accept all the default settings but two: select Single Document and deselect Printing and Print Preview. Use the resource editor to edit the application's main menu. Click on the ResourceView tab in the Workspace window. Edit the IDR_MAINFRAME menu resource to add a separator and a Clear Document item to the Edit menu, as shown here. 
    The resource editor's menu resource editor is intuitive, but you might need some help the first time you insert an item in the middle of a menu. A blank item is present at the bottom of each menu. Using the mouse, drag the blank item to the insertion position to define a new item. A new blank item will appear at the bottom when you're finished. 
    Now add a Transfer menu, and then define the underlying items.Use the following command IDs for your new menu items.
    Menu Caption Command ID 
    Edit Clear &Document ID_EDIT_CLEAR_ALL 
    Transfer &Get Data From Document\tF2 ID_TRANSFER_GETDATA 
    Transfer &Store Data In Document\tF3 ID_TRANSFER_STOREDATA 
    The MFC library has defined the first item, ID_EDIT_CLEAR_ALL. (Note: \t is a tab character—but type \t; don't press the Tab key.) When you add the menu items, type appropriate prompt strings in the Menu Item Properties dialog. These prompts will appear in the application's status bar window when the menu item is highlighted. 
    Use the resource editor to add keyboard accelerators. Open the IDR_MAINFRAME accelerator table, and then use the insert key to add the following items. Accelerator ID Key 
    ID_TRANSFER_GETDATA VK_F2 
    ID_TRANSFER_STOREDATA VK_F3 Be sure to turn off the Ctrl, Alt, and Shift modifiers. The Accelerator edit screen and Accel Properties dialog are shown in the illustration below.
     
    Use ClassWizard to add the view class command and update command UI message handlers. Select the CEx13aView class, and then add the following member functions.Object ID Message Member Function 
    ID_TRANSFER_GETDATA COMMAND OnTransferGetData 
    ID_TRANSFER_STOREDATA COMMAND OnTransferStoreData 
    ID_TRANSFER_STOREDATA UPDATE_COMMAND_UI OnUpdateTransferStoreData Use ClassWizard to add the document class command and update command UI message handlers. Select the CEx13aDoc class, and then add the following member functions. Object ID Message Member Function 
    ID_EDIT_CLEAR_ALL COMMAND OnEditClearDocument 
    ID_EDIT_CLEAR_ALL UPDATE_COMMAND_UI OnUpdateEditClearDocument Add a CString data member to the CEx13aDoc class. Edit the file ex13aDoc.h or use ClassView. public:
        CString m_strText;
    Edit the document class member functions in ex13aDoc.cpp. The OnNewDocument function was generated by ClassWizard. As you'll see in Chapter 16, the framework calls this function after it first constructs the document and when the user chooses New from the File menu. Your version sets some text in the string data member. Add the following boldface code: BOOL CEx13aDoc::OnNewDocument()
    {
        if (!CDocument::OnNewDocument())
            return FALSE;
        m_strText = "Hello (from CEx13aDoc::OnNewDocument)";
        return TRUE;
    }
    The Edit Clear Document message handler sets m_strText to empty, and the update command UI handler grays the menu item if the string is already empty. Remember that the framework calls OnUpdateEditClearDocument when the Edit menu pops up. Add the following boldface code: 
    void CEx13aDoc::OnEditClearDocument()
    {
        m_strText.Empty();
    }void CEx13aDoc::OnUpdateEditClearDocument(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(!m_strText.IsEmpty());
    }Add a CRichEditCtrl data member to the CEx13aView class. Edit the file ex13aView.h or use ClassView. public:
        CRichEditCtrl m_rich;
    Use ClassWizard to map the WM_CREATE and WM_SIZE messages in the CEx13aView class. The OnCreate function creates the rich edit control. The control's size is 0 here because the view window doesn't have a size yet. The code for the two handlers is shown below. int CEx13aView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        CRect rect(0, 0, 0, 0);
        if (CView::OnCreate(lpCreateStruct) == -1)
            return -1;
        m_rich.Create(ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN |
                      WS_CHILD | WS_VISIBLE | WS_VSCROLL, rect, this, 1);
        return 0;
    }Windows sends the WM_SIZE message to the view as soon as the view's initial size is determined and again each time the user changes the frame size. This handler simply adjusts the rich edit control's size to fill the view client area. Add the following boldface code:
    void CEx13aView::OnSize(UINT nType, int cx, int cy)
    {
        CRect rect;
        CView::OnSize(nType, cx, cy);
        GetClientRect(rect);
        m_rich.SetWindowPos(&wndTop, 0, 0, rect.right - rect.left,
                            rect.bottom - rect.top, SWP_SHOWWINDOW);
    }Edit the menu command handler functions in ex13aView.cpp. ClassWizard generated these skeleton functions when you mapped the menu commands in step 4. The OnTransferGetData function gets the text from the document data member and puts it in the rich edit control. The function then clears the control's modified flag. There is no update command UI handler. Add the following boldface code: void CEx13aView::OnTransferGetData()
    {
        CEx13aDoc* pDoc = GetDocument();
        m_rich.SetWindowText(pDoc->m_strText);
        m_rich.SetModify(FALSE);
    }The OnTransferStoreData function copies the text from the view's rich edit control to the document string and resets the control's modified flag. The corresponding update command UI handler grays the menu item if the control has not been changed since it was last copied to or from the document. Add the following boldface code: 
    void CEx13aView::OnTransferStoreData()
    {
        CEx13aDoc* pDoc = GetDocument();
        m_rich.GetWindowText(pDoc->m_strText);
        m_rich.SetModify(FALSE);
    }void CEx13aView::OnUpdateTransferStoreData(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(m_rich.GetModify());
    }Build and test the EX13A application. When the application starts, the Clear Document item on the Edit menu should be enabled. Choose Get Data From Document from the Transfer menu. Some text should appear. Edit the text, and then choose Store Data In Document. That menu item should now appear gray. Try choosing the Clear Document command, and then choose Get Data From Document again. 
      

  3.   

    The EX13A Example Revisited
    Now we'll add a property sheet to EX13A that allows the user to change the rich edit control's font characteristics. Of course, we could have used the standard MFC CFontDialog function, but then you wouldn't have learned how to create property sheets. Figure 13-5 shows the property sheet that you'll build as you continue with EX13A. Figure 13-5. The property sheet from EX13A. If you haven't built EX13A, follow the instructions that begin under the EX13A Example to build it. If you already have EX13A working with the Transfer menu commands, just continue on with these steps:
    Use the resource editor to edit the application's main menu. Click on the ResourceView tab in the Workspace window. Edit the IDR_MAINFRAME menu resource to add a Format menu that looks like this. 
    Use the following command IDs for the new Format menu items. Caption Command ID 
    &Default ID_FORMAT_DEFAULT 
    &Selection ID_FORMAT_SELECTION Add appropriate prompt strings for the two menu items.
    Use ClassWizard to add the view class command and update command UI message handlers. Select the CEx13aView class, and then add the following member functions. 
    Object ID Message Member Function 
    ID_FORMAT_DEFAULT COMMAND OnFormatDefault 
    ID_FORMAT_SELECTION COMMAND OnFormatSelection 
    ID_FORMAT_SELECTION UPDATE_COMMAND_UI OnUpdateFormatSelection 
    Use the resource editor to add four property page dialog templates. The templates are shown here with their associated IDs. 
    Use the IDs in the table below for the controls in the dialogs. Set the Auto Buddy and the Set Buddy Integer properties for the spin button control, and set the Group property for the IDC_FONT and IDC_COLOR radio buttons. Set the minimum value of IDC_FONTSIZE to 8 and its maximum value to 24. Use ClassWizard to create the classes CPage1, CPage2, CPage3, and CPage4. In each case, select CPropertyPage as the base class. Click the Change button in ClassWizard's New Class dialog to generate the code for all these classes in the files Property.h and Property.cpp. Then add the data members shown here.Dialog Control ID Type Data Member 
    IDD_PAGE1 First radio button IDC_FONT int m_nFont 
    IDD_PAGE2 Bold check box IDC_BOLD BOOL m_bBold 
    IDD_PAGE2 Italic check box IDC_ITALIC BOOL m_bItalic 
    IDD_PAGE2 Underline check box IDC_UNDERLINE BOOL m_bUnderline 
    IDD_PAGE3 First radio button IDC_COLOR int m_nColor 
    IDD_PAGE4 Edit control IDC_FONTSIZE int m_nFontSize 
    IDD_PAGE4 Spin button control IDC_SPIN1     Finally, use ClassWizard to add an OnInitDialog message handler function for CPage4.
    Use ClassWizard to create a class derived from CPropertySheet. Choose the name CFontSheet. Generate the code in the files Property.h and Property.cpp, the same files you used for the property page classes. Figure 13-6 shows these files with the added code in boldface.