Tabbed Views

This program is useful if you need multiple views of the same document but you only want one of them to be visible at once. It draws a number of tabs under the menu/toolbars that can be used to select the active view.

First we create a CWnd-derived class


class CTabWnd : public CWnd
{
 // Construction
 public:
  CTabWnd();
 
 // Generated message map functions
protected:
 //{{AFX_MSG(CTabWnd)
 afx_msg BOOL OnEraseBkgnd(CDC* pDC);
 afx_msg void OnPaint();
 //}}AFX_MSG
DECLARE_MESSAGE_MAP()
}
Then we add a Create function to initialize our control.

BOOL CTabWnd::Create(DWORD dwStyle, CWnd* pParentWnd, UINT nID)
{
 CRect rect, parent;
 ASSERT(pParentWnd);

 if (!CWnd::Create(NULL, "SealiteTabWnd", WS_CHILD|WS_VISIBLE, rect, pParentWnd, nID, NULL))
  return FALSE;

 m_dwStyle=dwStyle;
 return TRUE;
}
As you can see the window is created with an uninitialized rect. It will be resized as soon as it receives a WM_SIZEPARENT function. It will fill the entire client area and the views are creating using it as a parent. Because the control is created in the client area of the parent window it has to draw itself the client edge. For best results it is recommended that you remove the WS_EX_CLIENTEDGE style of the parent window.

Next, we add a linked list of views and a CreateView function to add views in it.

typedef struct
{
 CWnd *pWnd;
 char szLabel[32];
 int x_min, x_max;
}

CList <TABWND_MEMBER*,TABWND_MEMBER *> m_viewList;
BOOL CreateView(LPCTSTR lpszLabel, CRuntimeClass *pViewClass, CCreateContext *pContext);
In the CreateView function we instantiate the pViewClass using CRuntimeClass::CreateObject() and add the resulting CView to the list.

The created views have to be resized whenever the parent window resize, so we add a handler for WM_SIZEPARENT.


LRESULT CTabWnd::OnSizeParent(WPARAM, LPARAM lParam)
{
 PostMessage(WM_TABRESIZE);
 return 0;
}
To find out how much space is available for the client area after the control bars have allocated space for themselves we have to use the RepositionBars function with the CWnd::reposQuery parameter. Unfortunatly in a WM_SIZEPARENT handler RepositionBars does not return a valid rectangle because the message is used by the control bars to resize the client area. To walk around this we post a message and in its handler we can safely call this function.

The next thing to do is handle the WM_PAINT message to draw the selector and the WM_LBUTTONUP message to switch the views. The SetActiveView function is used to make sure that the window/command messages are routed to the active view. When the current tab is changed both the view thats going to be selected and the one thats going to be unselected are sent the WM_TABCHANGED message.

This is a rather complex subject and it is imposible to throughly cover it in a few pages. For more information you should go through the CTabWnd (you can download the project and source files by clicking the link at the beginning of this page) and CSplitterWnd.

Download demo project - 25 KB

Date Last Updated: April 3, 1999



Comments

  • add/del & scroll function?

    Posted by ahgu on 05/10/2006 11:22pm

    Can you add delete/new control to add/delete tabs? Also cursor to move if too many?

    Reply
  • Shortcut

    Posted by Legacy on 11/16/2002 12:00am

    Originally posted by: Jag

    How add shortcut key for the tab?
    And how to set tab order?

    TIA
    Jag

    Reply
  • Cannot get client area size in OnInitUpdate()

    Posted by Legacy on 07/29/2002 12:00am

    Originally posted by: YI Ren

    I have a SDI application, after adding the tabs, in the view's OnInitUpdate(), GetClientRect() returns 0 size of the view. I really need to know the size of the cient area, so I can resize it to 640x480. Any ideas why the ckient area has a size of zero ?

    Reply
  • Yes it works on my SDI application

    Posted by Legacy on 05/29/2002 12:00am

    Originally posted by: Guillermo Simanavicius

    Yes, it works on my SDI application, please take a look on my sample at http://sinopsis.net/cpp/TabViewPPSample.zip
    Guillermo

    Reply
  • No one above could solved the print preview problem

    Posted by Legacy on 05/22/2002 12:00am

    Originally posted by: Kivanc Oner & Ali Yazgan

    The techniques that both Marc Allaire and Guillermo Simanavicius are not capable of solving the print preview problem. Or we could not use the new codes efficiently. If anyone knows how to print preview from a CListView based SDI application please contact us...

    Reply
  • Sheetal's exclusive page

    Posted by Legacy on 04/10/2002 12:00am

    Originally posted by: Sheetal

    This is a very nice site but of no use.............
    

    Reply
  • Print Preview bug fix

    Posted by Legacy on 03/22/2002 12:00am

    Originally posted by: Marc Allaire

    I investigated a little in the print preview bug.

    It gives an assertion in the file winfrm.cpp, line 1842

    Looking at that i see it tries to get the handle of
    the control/window that has AFX_IDW_PANE_FIRST as its ID.
    This should be the main pane, that print preview hides
    before displaying the preview.

    All there is to do to fix the problem is create the
    CTabWnd window with AFX_IDW_PANE_FIRST for its ID
    and print preview works.

    Reply
  • Print Preview Crashes

    Posted by Legacy on 04/22/1999 12:00am

    Originally posted by: Jose Luis Flores


    When I try to make a print preview the program crashes but when I try to print I have no problem, so ???? I have spent time looking for the error but ... without luck !

    Reply
  • Problem with CRichEditView

    Posted by Legacy on 02/19/1999 12:00am

    Originally posted by: Emil Dimitrov

    When sitching from one tab to another CTabWnd sends notification messages WM_TABCHANGED to both child views.
    
    This is a problem when using CRichEditView as a view because the message WM_TABCHANGED has the same value as EM_SETTARGETDEVICE. After sending this message to the CRichEditView it stops responding.

    To avoid this problem do not use WM_USER but WM_APP because MFC uses user messages for some controls :

    #define WM_TABRESIZE (WM_APP+0x47)
    #define WM_TABCHANGED (WM_APP+0x48)

    Reply
  • Using it with splitter window

    Posted by Legacy on 11/06/1998 12:00am

    Originally posted by: Rochmad Setyadi

    I modified the code ...CTabWnd class no longer derived from CWnd ...
    
    but CSplitterWnd ..to satisfy my GUI ... and simply it's work! There
    are some modifications on OnTabRize ..handler.

    Here my modification ...

    LRESULT CTabWnd::OnTabResize(WPARAM, LPARAM)
    {
    CWnd *pParent=(CWnd *)GetParent();
    CWnd *coClient = ((CSplitterWnd*)pParent)->GetPane(0,0);
    ASSERT(pParent);
    ASSERT(coClient);

    CRect coRect;
    coClient->GetWindowRect(coRect);
    CRect parRect;
    pParent->GetClientRect(parRect);

    int pw = parRect.Width();
    int ph = parRect.Height();
    int wco = coRect.Width();

    CRect rect(wco,0,pw,ph);

    m_bLock= TRUE;
    CRect rct(0,0,32000,32000);
    pParent->RepositionBars(0, 0xffff,AFX_IDW_PANE_FIRST,
    CWnd::reposQuery,&rct, NULL);

    MoveWindow(rect.left+8,rect.top,rect.Width(),rect.Height());

    CWnd *pWnd;
    for (POSITION pos=m_viewList.GetHeadPosition(); pos;
    m_viewList.GetNext(pos))
    {
    pWnd=m_viewList.GetAt(pos)->pWnd;
    pWnd->MoveWindow(2, TABWND_HEIGHT+2, rect.Width()-10,
    rect.Height()-TABWND_HEIGHT-4,TRUE);
    }

    m_bLock=FALSE;
    return 0;
    }

    CTabWnd object was created using CreateStatic ..from CSplitterWnd ..instead of CTabWnd's Create

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Businesses are moving more and more of their customer transactions to the web. Security is understandably a top concern as online transactions increase, so it is important to make sure your electronic signature provider meets the highest security standards. That means more than simply passing a security audit or obtaining a certification. This white paper provides recommendations for taking a broader view of e-signature security, and answers key questions that help identify the security requirements against …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds