Tip of the day–An easy & convenient way to create tabs in an MFC application

Hi Community,

Today I started to write an MFC application that comprises the Kinect sensor and its SDK, and the idea is to have a single UI with different views, this functionality is provided by the CTabCtrl class (Tab control) – I could’ve accomplish this by creating tabs and setting its properties individually but it’s repetitive code that might grow in the future when a new tab is required, so I came up with a convenient way to do this as shown in the snippet below

void MyKinectAppDlg::CreateTabControl() {
    auto nIndex = 0;
    auto tabs = std::map<std::wstring, TCITEM>();
    tabs.insert(std::pair<std::wstring, TCITEM>(L"Color View", TCITEM()));
    tabs.insert(std::pair<std::wstring, TCITEM>(L"Depth View", TCITEM()));
    tabs.insert(std::pair<std::wstring, TCITEM>(L"Skeletal View", TCITEM()));

    std::for_each(tabs.begin(), tabs.end(), [&] (std::pair<std::wstring, TCITEM> tab) {
        tab.second.mask = TCIF_TEXT;
        tab.second.pszText =  (LPWSTR) tab.first.c_str();
        m_streamTabs.InsertItem(nIndex++, &tab.second);
    });
}

and the tab control created is

image

Happy coding & happy weekend!

Angel

Leave a Reply

Your email address will not be published. Required fields are marked *