How to change CTabCtrl tab colors?

Hello and happy new year, (permissible to say this until Thursday)

I am trying to change the color of tabs in the CTabCtrl class. I am trying to create my own ReskinCTablCtrl so that I can just call it in separate classes and use it easily throughout my program.

Currently, I can change the background color of CTabCtrl, but I cannot change the tab myself.

I used ON_WM_ERASEBKGND()to paint the background and it worked without problems:

BOOL ReskinCTabCtrl::OnEraseBkgnd(CDC* pDC)
{
    CRect rect;
    GetClientRect(&rect);
    CBrush myBrush(RGB(51, 51, 51));    // dialog background color
    BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
    pDC->SetBkColor(RGB(51, 51, 51));
    pDC->FillRect(&rect, &myBrush);
    return bRes;
}

However, I was unsuccessful at changing the tab colors themselves. They are still standard MFC colors. I tried to implement ON_WM_PAINT()and ON_WM_DRAWITEM()without any success. I think I can navigate to a specific language using both OnDraw and DrawItem, similar to the second link example that I posted at the end of this question.

void ReskinCTabCtrl::OnPaint() {

    ...

    // paint the tabs first and then the borders
    int nTab = GetItemCount();
    int nSel = GetCurSel();

    if (!nTab) // no pages added
        return;

    while (nTab--)
    {
        if (nTab != nSel)
        {
            dis.itemID = nTab;
            dis.itemState = 0;

            VERIFY(GetItemRect(nTab, &dis.rcItem));

            dis.rcItem.bottom -= 2;
            DrawItem(&dis);
            DrawItemBorder(&dis);
        }
    }

    ...

}

I would really appreciate at least some direction to solve this problem, perhaps a few more examples or what methods I should focus on using. I do not need tabs for different colors, maybe there is an easy way to do this?

I tried to follow some examples, such as the following links, but I still could not figure out how to do this.

https://support.microsoft.com/en-us/help/179909/how-to-change-the-background-color-of-a-tab-control

https://www.codeproject.com/Articles/1786/Ownerdraw-Tab-Controls-Borders-and-All

+4
1

OwnerDraw , , TCS_OWNERDRAWFIXED OnInitDialog

CTabCtrl WM_DRAWITEM, WM_DRAWITEM/OnDrawItem . CTabCtrl::DrawItem(LPDRAWITEMSTRUCT).

, . DrawItem .

Visual Style , CTabCtrl::OnPaint . :

void CMyTabCtrl::OnPaint()
{
    CPaintDC dc(this);

    dc.SelectObject(GetFont());

    CPen pen, pen_active;
    COLORREF color_off = RGB(240, 240, 240);
    COLORREF color_active = RGB(200, 240, 240);
    CBrush brush_off, brush_active;
    brush_off.CreateSolidBrush(color_off);
    brush_active.CreateSolidBrush(color_active);
    pen.CreatePen(PS_SOLID, 1, RGB(200, 200, 200));
    pen_active.CreatePen(PS_SOLID, 1, color_active);

    CRect rcitem;
    GetItemRect(0, &rcitem);

    CRect rc;
    GetClientRect(&rc);
    rc.bottom = rcitem.bottom;
    dc.FillSolidRect(&rc, GetSysColor(COLOR_3DFACE));

    GetClientRect(&rc);
    rc.top = rcitem.bottom - 1;
    dc.SelectObject(&pen);
    dc.SelectObject(&brush_active);
    dc.Rectangle(&rc);

    for(int i = 0; i < GetItemCount(); i++)
    {
        dc.SelectObject(&pen);
        if(i == GetCurSel())
        {
            dc.SelectObject(&brush_active);
            dc.SetBkColor(color_active);
        }
        else
        {
            dc.SelectObject(&brush_off);
            dc.SetBkColor(color_off);
        }

        GetItemRect(i, &rcitem);
        rcitem.right++;
        dc.Rectangle(&rcitem);

        if(i == GetCurSel())
        {
            dc.SelectObject(pen_active);
            dc.MoveTo(rcitem.left+1, rcitem.bottom - 1);
            dc.LineTo(rcitem.right, rcitem.bottom - 1);
        }

        TCITEM item = { 0 };
        wchar_t buf[32];
        item.pszText = buf;
        item.cchTextMax = 32;
        item.mask = TCIF_TEXT;
        GetItem(i, &item);
        dc.DrawText(buf, &rcitem, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
}

BOOL CMyTabCtrl::OnEraseBkgnd(CDC*)
{
    return TRUE;
}

enter image description here

+5

Source: https://habr.com/ru/post/1691626/


All Articles