mfcmfc-feature-pack

How to put checkbox in each CMFCTabCtrl's tab header


I have a MFC application. I am using CMFCTabCtrl in a dialog.

My requirement is I have to add a checkbox in each tab page of CMFCTabCtrl as below.

Image

Is it possible to add checkbox in each tab page of CMFCTabCtrl ?


Solution

  • My question was answered in the below link.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/2b86a6df-12bb-4fe2-9f23-d9848de49f84/adding-checkbox-in-cmfctabctrls-tab-header?forum=vcgeneral#000673ca-c995-42d8-85c2-cb4781090653

    The code is

    BOOL CMyDlg::OnInitDialog()
    {
        CDialogEx::OnInitDialog();
    
        // TODO:  Add extra initialization here
        CRect rectDummy;
        rectDummy.SetRectEmpty();
        if(!m_tab.Create(CMFCTabCtrl::STYLE_3D_VS2005, rectDummy, this, 1))
        {
        TRACE0("Failed to create output tab window\n");
        return -1;      // fail to create
        }
    
        m_tab.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
        m_tab.SetLocation(CMFCTabCtrl::LOCATION_TOP);
    
        CRect rc;
        GetClientRect(rc);
        m_tab.MoveWindow(0, 200, rc.right, 200);
    
        const DWORD dwStyle = LBS_NOINTEGRALHEIGHT | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;
        if (!m_wndOutputBuild.Create(dwStyle, rectDummy, &m_tab, 2) ||
            !m_wndOutputDebug.Create(dwStyle, rectDummy, &m_tab, 3) ||
            !m_wndOutputFind.Create(dwStyle, rectDummy, &m_tab, 4))
        {
            TRACE0("Failed to create output windows\n");
            return -1;      // fail to create
        }
    
        CString strTabName;
        BOOL bNameValid;
    
        // Attach list windows to tab:
        m_tab.AddTab(&m_wndOutputBuild, L"     First", (UINT)0);
        m_tab.AddTab(&m_wndOutputDebug, L"     Second", (UINT)1);
        m_tab.AddTab(&m_wndOutputFind,  L"     Third", (UINT)2);
    
        m_tab.init();
    
        return TRUE;  // return TRUE unless you set the focus to a control
                      // EXCEPTION: OCX Property Pages should return FALSE
    } myTabCtrl.h
    #pragma once
    #include "afxtabctrl.h"
    class myTabCtrl : public CMFCTabCtrl
    {
    public:
        myTabCtrl();
        ~myTabCtrl();
        void init();
    
        DECLARE_MESSAGE_MAP()
    
        CButton m_Check1;
        CButton m_Check2;
        CButton m_Check3;
        virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
    }; myTabCtrl.cpp
    #include "stdafx.h"
    #include "myTabCtrl.h"
    
    
    myTabCtrl::myTabCtrl()
    {
    }
    
    
    myTabCtrl::~myTabCtrl()
    {
    }
    
    void myTabCtrl::init()
    {
        CRect rc1, rc2, rc3;
        GetTabRect(0, rc1);
        GetTabRect(1, rc2);
        GetTabRect(2, rc3);
    
        m_Check1.Create(_T("Chkbox1"), WS_CHILD | WS_VISIBLE | BS_CHECKBOX, CRect(0, 0, 13, 13), this, 1234);
        m_Check2.Create(_T("Chkbox2"), WS_CHILD | WS_VISIBLE | BS_CHECKBOX, CRect(0, 0, 13, 13), this, 1235);
        m_Check3.Create(_T("Chkbox3"), WS_CHILD | WS_VISIBLE | BS_CHECKBOX, CRect(0, 0, 13, 13), this, 1236);
    
    
        m_Check1.MoveWindow(rc1.left + 20, rc1.top + 3, 13, 13);
        m_Check2.MoveWindow(rc2.left + 20, rc2.top + 3, 13, 13);
        m_Check3.MoveWindow(rc3.left + 20, rc3.top + 3, 13, 13);
    }
    
    BEGIN_MESSAGE_MAP(myTabCtrl, CMFCTabCtrl)
    END_MESSAGE_MAP()
    
    LRESULT myTabCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
        // TODO: Add your specialized code here and/or call the base class
        switch (message)
        {
        case WM_COMMAND:
            if (wParam == 1234)
            {
                BOOL checked = m_Check1.GetCheck();
                m_Check1.SetCheck(!checked);
            }
            else if (wParam == 1235)
            {
                BOOL checked = m_Check2.GetCheck();
                m_Check2.SetCheck(!checked);
            }
            else if (wParam == 1236)
            {
                BOOL checked = m_Check3.GetCheck();
                m_Check3.SetCheck(!checked);
            }
            break;
        }
    
        return CMFCTabCtrl::WindowProc(message, wParam, lParam);
    }