Do-It-Yourself Control Views

[Previous] [Next]

CTreeView and CListView are examples of control views—views whose functionality comes from a Windows control. Both are derived from CCtrlView, which is also the base class for CEditView and CRichEditView. CCtrlView provides the basic functionality common to all control views. By using it as a base class, you can create control views of your own that wrap other types of Windows controls.

To demonstrate, the following CCtrlView-derived class defines a tabbed view, which is simply a view wrapped around a Win32 tab control. When displayed, it looks like a normal view except that it has property sheet_like tabs at the top:

class CTabView : public CCtrlView {     DECLARE_DYNCREATE (CTabView) public:     CTabView () :         CCtrlView (_T ("SysTabControl32"), AFX_WS_DEFAULT_VIEW) {}     CTabCtrl& GetTabCtrl () const { return *(CTabCtrl*) this; }     virtual BOOL PreCreateWindow (CREATESTRUCT& cs);     virtual void OnInitialUpdate (); }; IMPLEMENT_DYNCREATE (CTabView, CCtrlView) BOOL CTabView::PreCreateWindow (CREATESTRUCT& cs) {     ::InitCommonControls ();     if (!CCtrlView::PreCreateWindow (cs))         return FALSE;     cs.style ¦= TCS_FIXEDWIDTH; // Fixed-width tabs.     return TRUE; } void CTabView::OnInitialUpdate () {     static CString strLabel[] = {         _T ("Tab No. 1"),         _T ("Tab No. 2"),         _T ("Tab No. 3")     };     // Set the tab width to 96 pixels.     GetTabCtrl ().SetItemSize (CSize (96, 0));     // Add three tabs.     TC_ITEM item;     item.mask = TCIF_TEXT;     for (int i=0; i<3; i++) {         item.pszText = (LPTSTR) (LPCTSTR) strLabel[i];         item.cchTextMax = strLabel[i].GetLength ();         GetTabCtrl ().InsertItem (i, &item);     } } 

The key features of this class are the default constructor, which passes the base class's constructor the name of the tab control's WNDCLASS ("SysTabControl32"); the GetTabCtrl function, which returns a reference to the underlying tab control; and OnInitialUpdate, which adds three tabs to the control. PreCreateWindow also plays an important role by initializing the common controls library and applying default styles to the control.



Programming Windows with MFC
Programming Windows with MFC, Second Edition
ISBN: 1572316950
EAN: 2147483647
Year: 1999
Pages: 101
Authors: Jeff Prosise

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net