none
How to add tabs dynamically to CSplitterWnd window? RRS feed

  • Question

  • Hi

    I have created multiple windows (views) in my MFC application using CSplitterWnd class. In one of the window (view) i need to add tabs dynamically..

    Can any one help me on how to add tabs to my CSplitterWnd window.?

    Thanks in advance.



    • Edited by sampathj Wednesday, February 28, 2018 5:15 PM
    Tuesday, February 27, 2018 11:16 AM

All replies

  • Hi sampathj,

    thanks for posting here.

    >>Can any one help me on how to add tabs to my CSplitterWndEx window.?

    For this case, maybe you could implement an owner drawn tab control. If a tab control has the TCS_OWNERDRAWFIXED style, the parent window must paint tabs by processing the WM_DRAWITEM message. The tab control sends this message whenever a tab needs to be painted.

    For more information, you could refer to the document and sample below.

    https://msdn.microsoft.com/library/windows/desktop/bb760550#owner_drawn_tabs

    https://www.codeguru.com/cpp/controls/controls/tabcontrols/article.php/c2237/Implementing-an-owner-drawn-Tab-Control.htm

    Hope this could be help of you.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 28, 2018 8:46 AM
  • CTabCtrl + CFormView ??

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Wednesday, February 28, 2018 9:38 AM
  • Baron Thanks for your time. 

    My requirement is to create a tab on already existing CSplitter window. I am unable to add any control on this window explicitly. Can you provide any sample code for this requirement.?

    This is my class:

    CSplitterWnd *GetSplitter0() {return splitterObj;}

    GetSplitter0()->CreateView
    (
    1,
    1,
    RUNTIME_CLASS(MyClass),
    sizeView,
    pContext
    );

    This created a splitter window.

    Now this is MyClass.cpp implementation.

    class MyClass : public CScrollView

    {

    //need to add tabs here.

    }

    I am a beginner in MFC.. 

    Regards
    sampath


    Wednesday, February 28, 2018 1:21 PM
  • CSplitterWnd expect its panes to be CViews. But it does not use CView features most of the time, I think it does only when creating/destroying panes. you can create the required columns/rows first, then get the child control id of the view at the given column/row, get the view's HWND, change the child control id of the view to something else, then create a tab control on the splitter with the same location and id. Basically you can swapping out the pane view with a generic CWnd. 

    but yeah a tab control on CFormView is much easier, since you most likely need to store the data in your  CDocument anyway. 



    Visual C++ MVP

    • Proposed as answer by Baron Bi Friday, March 2, 2018 6:36 AM
    Thursday, March 1, 2018 6:17 PM
  • Hi

    I have created multiple windows (views) in my MFC application using CSplitterWnd class. In one of the window (view) i need to add tabs dynamically..

    Can any one help me on how to add tabs to my CSplitterWnd window.?

    Thanks in advance.



    You can add in one of your splitter an CTabView object, where you can add/remove dynamically any number of CViews. Something like that:

    BOOL CMyChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    
    	if(! m_wndSplitterMain.CreateStatic(this, 1, 2))
    		return FALSE;
    	if(! m_wndSplitterMain.CreateView(0, 0, RUNTIME_CLASS(CMyTree), CSize(0, 0), pContext))
    	{
    		m_wndSplitterMain.DestroyWindow();
    		return FALSE;
    	}
    	if(! m_wndSplitterTab.CreateStatic(&m_wndSplitterMain, 2, 1, WS_CHILD | WS_VISIBLE | WS_BORDER, m_wndSplitterMain.IdFromRowCol(0, 1)))
    	{
    		m_wndSplitterTab.DestroyWindow();
    		return FALSE;
    	}
    	if(! m_wndSplitterView.CreateStatic(&m_wndSplitterTab, 1, 2, WS_CHILD | WS_VISIBLE | WS_BORDER, m_wndSplitterTab.IdFromRowCol(0, 0)))
    	{
    		m_wndSplitterView.DestroyWindow();
    		return FALSE;
    	}
    	if(! m_wndSplitterTab.CreateView(1, 0, RUNTIME_CLASS(CMyTabbedView), CSize(0, 0), pContext))
    	{
    		m_wndSplitterTab.DestroyWindow();
    		return FALSE;
    	}
    ....

    where CMyTabbedView is derived from CTabView.


    • Edited by flaviu_ Friday, March 2, 2018 9:41 AM
    Friday, March 2, 2018 9:40 AM
  • But my requirement is to create on CSplitter window itself. Can you please provide me a piece of code to create a tab on splitter window? I tried many ways to create a simple button on splitter after creating a view, but it doesnot work. 

    This is how i created window and button.

    //the below call created a window success fully. 

    bStatus = GetSplitter0()->CreateView
    (
    1,
    1,
    RUNTIME_CLASS(CMyClass),
    sizeView,
    pContext
    );

    //The below code doesnot add button.

    CButton but;

    CRect rect;
    GetWindowRect(rect);

    but.Create(_T("Hi"), BS_PUSHBUTTON, rect, GetView(MYVIEW2)->GetSafeOwner(), IDC_BUTTON1);

    but.ShowWindow(SW_SHOW);

    Can you please help me in adding atleast a button on it. 

    Regards

    Sampath

    Friday, March 2, 2018 12:26 PM
  • Thanks

    I created the same way as you mentioned above. 

    bStatus = GetSplitter0()->CreateView
    (
    1,
    1,
    RUNTIME_CLASS(MyTabClass),
    sizeView,
    pContext
    );

    class MyTabClass : public CTabView

    {

    //sample code

    void Init()

    {

    CTabView view;

    view.AddView(....);

    }

    };

    But it dint help. Am i doing correct?


    • Edited by sampathj Friday, March 2, 2018 2:08 PM
    Friday, March 2, 2018 12:33 PM
  • Here is a method to dynamically create a tab and put a panel in it:

    void OnDataTabCreateEvent(std::string tabName)

    {

        DataForm^ DataView = gcnew DataForm(m_modelView, m_modelContext);

        TabPage^ dataPage = gcnew TabPage();

        dataPage->Name = tabName;

        dataPage->Text = tabName;

        dataPage->CreateControl();

        dataPage->Controls->Add(DataView);

        m_PageView->tabControl->TabPages->Add(dataPage);

    }


    Saturday, June 15, 2019 3:45 AM