Click here to Skip to main content
15,886,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all:

I used the MS method to change the background color of tab control in MFC.
http://support.microsoft.com/default.aspx?scid=kb;en-us;179909[^]

C++
#define RED     RGB(255,0,0)
#define YELLOW  RGB(255,255,0)
#define MAGENTA RGB(255,0,255)
#define WHITE   RGB(255,255,255)
#define BLUE    RGB(0,0,255)

void CMFCTabCtrlDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpdis)
{
    CDialog::OnDrawItem(nIDCtl, lpdis);

    char        szTabText[100];
    RECT        rect;
    UINT        bkColor;
    CBrush      *cbr;
    TC_ITEM     tci;

    CTabCtrl    *pTabCtrl = (CTabCtrl *)GetDlgItem(IDC_TAB1);

    if (pTabCtrl->m_hWnd == lpdis->hwndItem)
    {
        // which tab?
        switch (lpdis->itemID)
        {
        case 0:
            cbr = &m_brRed;
            bkColor = RED;
            break;

        case 1:
            cbr = &m_brYellow;
            bkColor = YELLOW;
            break;

        case 2:
            cbr = &m_brMagenta;
            bkColor = MAGENTA;
            break;

        case 3:
            cbr = &m_brWhite;
            bkColor = WHITE;
            break;

        case 4:
            cbr = &m_brBlue;
            bkColor = BLUE;
            break;
        }

        memset(szTabText, '\0', sizeof(szTabText));

        tci.mask        = TCIF_TEXT;
        tci.pszText     = szTabText;
        tci.cchTextMax  = sizeof(szTabText)-1;

        pTabCtrl->GetItem(lpdis->itemID, &tci);

        CDC *dc = CDC::FromHandle(lpdis->hDC);

        dc->FillRect(&lpdis->rcItem, cbr);
        dc->SetBkColor(bkColor);

        TextOut(lpdis->hDC,
                lpdis->rcItem.left,
                lpdis->rcItem.top,
                tci.pszText,
                lstrlen(tci.pszText));
    }
}

I used the wizard to produce the OnDrawItem( ).
I guessed the m_brRed, m_brYellow and so on is the instance for each Tab.
in my code, I used
C++
CPage1 m_page1;
CPage2 m_page2; 

But I can't build it.....

I thought maybe I can't build it because I can't understand for this sentence in the article:

First bring up the properties for the tab control in the Resource Editor and select the Styles tab. Select the "Owner draw fixed" check box and save your work.
If you are dynamically creating the Tab control during the dialog box's initialization with CreateWindow() or CreateWindowEx() be sure to include the TCS_OWNERDRAWFIXED bit in the dwStyle parameter.

I have select the "Owner draw fixed" check box, But I'm confused for the second sentence.
How can I "include the TCS_OWNERDRAWFIXED bit in the dwStyle parameter" ?

Thank you, everyone,

best regard.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900