I have Tabctrl in a dialog.I want to set its background colour for each tab .I tried ON_WM_DRAWITEM but the control never comes in OnDrawItem().Can you help?

#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 CVMListDialog::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_TAB_VM);

    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));
    }

}

Recommended Answers

All 7 Replies

You can find many examples on [url]www.codproject.com[/url] and [url]www.codeguru.com[/url]. Just search!!!

The above code only changed the tab colour and i donot want that.I want to colour the whole of the tab background to black.I derived a class from CTabCtrl and implemented ON_WM_ERASEBKGND but it did no change.I also searched codeproject.com and codeguru.com but in vain.

BOOL CMyTabCtrl::OnEraseBkgnd(CDC* cdc)
{
    CTabCtrl::OnEraseBkgnd(cdc);

    CRect tabRect;
    CBrush* m_Brush = cdc->GetCurrentBrush();
    GetClientRect(&tabRect);

    cdc->FillRect(tabRect,m_Brush);
    cdc->SetBkColor(RGB(0,0,0));

    return true;
}

Am i missing something in the code?

...
    CRect rcClient; 
    GetClientRect(&rcClient); 
    pDC->FillSolidRect(rcClient, m_bkgColor);
...
    CRect rcClient; 
    GetClientRect(&rcClient); 
    pDC->FillSolidRect(rcClient, m_bkgColor);

Yes i tried this too,but in vain

BOOL CMyTabCtrl::OnEraseBkgnd(CDC* cdc)
{
CTabCtrl::OnEraseBkgnd(cdc);

CRect Rect;
COLORREF color = RGB(0,108,175);
GetClientRect(&Rect);
cdc->FillSolidRect(Rect,color);

return true;
}

Do i need to set some properties for that tabctrl?

Read about OWNER DRAW Window's Style and Windows Subclassing and also Non Client Paint Message.

Following Link would help but pre-requisit are the above two concepts.

http://www.codeguru.com/cpp/controls/controls/tabcontrols/article.php/c2237

the Below is the link of VB Code just check How API's are being called.
http://www.vbaccelerator.com/home/VB/code/Controls/Tab_Controls/MDI_Tabs/article.asp


try this tutorial, if no luck then get back here again :). I will debug with you again..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.