HOWTO: Change the Background Color of a Tab Control

Last reviewed: January 29, 1998
Article ID: Q179909
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) on the following platforms:

        - Microsoft Windows 95
        - Microsoft Windows NT 4.0
    
  • Microsoft Visual Studio versions 97, 97sp1, 97sp2

SUMMARY

This article demonstrates how to change the background color of each tab in a Tab control. It assumes that you have a dialog box and have selected and sized a Tab control into the dialog using the Resource Editor.

MORE INFORMATION

To change the background color of each tab you must make the Tab control owner draw and use the FillRect() method to fill the rectangle area of the tab itself with a brush that you create and call the SetBkColor() method before you make a call to the TextOut() method with the text you want to appear on the tab.

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.

The following #defines are used in the sample:

        #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)

If you are using the SDK:

The brushes in this sample excerpt were created in WM_INITDIALOG and are static handles.

Add the WM_DRAWITEM message to the dialog box's procedure.

   case WM_DRAWITEM:
      lpdis = (LPDRAWITEMSTRUCT) lParam; // item drawing information
      hTabCtrl = GetDlgItem(hDlg, IDC_TAB1);

      if (hTabCtrl == lpdis->hwndItem)   // is this the tab control?
      {
         // which tab? first, second...fifth
         switch (lpdis->itemID)
         {
         case 0:
            hbr = hbrRed;
            bkColor = RED;
            break;
         case 1:
            hbr = hbrYellow;
            bkColor = YELLOW;
            break;
         case 2:
            hbr = hbrMagenta;
            bkColor = MAGENTA;
            break;
         case 3:
            hbr = hbrWhite;
            bkColor = WHITE;
            break;
         case 4:
            hbr = hbrBlue;
            bkColor = BLUE;
            break;
         }

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

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

         TabCtrl_GetItem(hTabCtrl, lpdis->itemID, &tci);

         FillRect(lpdis->hDC, &lpdis->rcItem, hbr);
         SetBkColor(lpdis->hDC, bkColor);

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

If your are using MFC:

The brushes referred to are part of the dialog class and were created when the dialog constructor was called.

Override the OnDrawItem() method for your CDialog derived class using Class Wizard and add the following code, changing variable names as neccessary. It is important to note that a pointer to a CDC object from the handle of the DC passed in via the LPDRAWITEMSTRUCT is required, otherwise only the background of the text will be the desired color.

Sample Code

       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));
          }
       }
Version : WIN95:97; WINNT:4.0;WINNT:97;


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 29, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.