Let's get familiar with the rebar by jumping into an example. This example creates an SDI application that has a rebar with two bands: a familiar toolbar band and a dialog bar band. Figure 14-6 shows the example in action.
Figure 14-6. EX14C rebar example.
Here are the steps required to create the EX14C example:
At this point, you can open the MainFrm.h header file and see the code below, which declares the CReBar data member m_ndReBar.
protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; CReBar m_wndReBar; CDialogBar m_wndDlgBar;
Figure 14-7. AppWizard Step 4 settings for the rebar control.
Figure 14-8. Initial windows for EX14C example with the default rebar controls.
In the MainFrm.cpp file, you can see the code that adds the toolbar and the dialog bar to the CReBar object:
if (!m_wndReBar.Create(this) || !m_wndReBar.AddBar(&m_wndToolBar) || !m_wndReBar.AddBar(&m_wndDlgBar)) { TRACE0("Failed to create rebar\n"); return -1; // fail to create }
Figure 14-9. Edited IDR_MAINFRAME dialog bar.
Choose Select An Existing Class. We choose this option because we want our dialog resource to be a band in the toolbar, not a separate dialog class. Click OK and you will see these choices.
Choose CMainFrame from the list and click Select.
ClassWizard will prompt you with one last dialog.
Click Yes and then exit ClassWizard. You have successfully associated the IDR_MAINFRAME dialog bar with the CMainFrame class.
void CMainFrame::OnButton1() { CProgressCtrl * pProgress = (CProgressCtrl*)m_wndDlgBar.GetDlgItem(IDC_PROGRESS1); pProgress->StepIt(); }
The OnButton1 handler first gets a pointer to the progress control and then calls StepIt to increment the progress control.
Now we need to add similar code to the decrement handler. Double-click on the Decrement button in the resource editor and ClassWizard will automatically create an OnButton2 handler. Add the following boldface code to the OnButton2 member function:
void CMainFrame::OnButton2() { CProgressCtrl * pProgress = (CProgressCtrl*)m_wndDlgBar.GetDlgItem(IDC_PROGRESS1); int nCurrentPos = pProgress->GetPos(); pProgress->SetPos(nCurrentPos-10); }
In this chapter, we learned how to use MFC's toolbar, status bar, and the new rebar control. In the next chapter, we'll look at how to extend MFC to implement a frame window that remembers its position.