Solved

CTabCtrl

Posted on 2001-06-06
10
2,149 Views
Last Modified: 2013-11-20
Hi All,
i have a tab control, and i want to change the color of the selected item, either the text of the backgound of the text.

i did the following:
InsertItem(0,"Tab Name");

now everytime the user click on a tab, i want the text of the selected one to change its color and font size, i m changing the text like this:

TCITEM     tabItem;
tabItem.mask = TCIF_TEXT ;
abItem.pszText = "New Text";
SetItem(m_tabCurrent,&tabItem);

but how can i change the color ?

thanks \
shay
0
Comment
Question by:Shay050799
  • 6
  • 4
10 Comments
 
LVL 2

Accepted Solution

by:
GloriousRain earned 50 total points
ID: 6162911
<MSDN>
HOWTO: Change the Text Color of the Selected Property Sheet Tab

Q206626


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), included with:
Microsoft Visual C++, 32-bit Editions, versions 4.2b, 5.0, 6.0

--------------------------------------------------------------------------------


SUMMARY
To change the color of text on the selected tab of a property sheet, you need to subclass the tab control associated with the property sheet and make it owner drawn.

Override the DrawItem method of your class derived from CTabCtrl and change the color of text before displaying it.



MORE INFORMATION

Create a new class called CMyTabCtrl deriving from CTabCtrl. Override the DrawItem method for CTabCtrl. Because ClassWizard does not show this method, this method needs to be added manually. The sample code below shows how to change the color of the text of the selected tab:


void CMyTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
     TC_ITEM tci;
     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
     CRect rect(lpDIS->rcItem);
     char szTabText[256];
     memset(szTabText,'\0',sizeof(szTabText));
     
     tci.mask = TCIF_TEXT;
     tci.pszText = szTabText;
     tci.cchTextMax = sizeof(szTabText) -1;
     GetItem(lpDIS->itemID, &tci);
     
     if (lpDIS->itemAction & ODA_DRAWENTIRE)
     {
         pDC->TextOut(rect.left+5, rect.top+5, tci.pszText);
     }
     
     if ((lpDIS->itemState & ODS_SELECTED) &&
         (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
     {
         //Make the color of text of the selected tab to be BLUE.
         pDC->SetTextColor(RGB(0,0,255));
         pDC->TextOut(rect.left+5, rect.top+5, tci.pszText);
     }
}



Create a new class, CMyPropertySheet, derived from CPropertySheet and add a member variable m_myTabCtrl of type CMyTabCtrl.

Add a handler for WM_INITDIALOG message. After the call to the base class OnInitDialog, add the code as shown below to subclass the tab control and make it an owner-drawn control:


BOOL CMyPropertySheet::OnInitDialog()
{
   BOOL bResult = CPropertySheet::OnInitDialog();

   /* Subclass the tab control. */
   m_myTabCtrl.SubclassWindow(GetTabControl()->m_hWnd);

   /* Make it owner drawn. */
   m_myTabCtrl.ModifyStyle(0,TCS_OWNERDRAWFIXED);

   return bResult;
}
 



Now, use CMyPropertySheet instead of CPropertySheet in your application.



REFERENCES

? Microsoft Corporation 1999, All Rights Reserved.
Contributions by Sreedhar Pelluru, Microsoft Corporation


Additional query words: CTabCtrl CPropertySheet DrawItem owner draw

Keywords : kbMFC kbPropSheet kbVC420 kbVC500 kbVC600 kbGrpMFCATL
Issue type : kbhowto
Technology : kbvc


Last Reviewed: March 9, 2000
? 2001 Microsoft Corporation. All rights reserved. Terms of Use.
 



--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
<MSDN>
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6162957
<MSDN>
HOWTO: Change the Background Color of a Tab Control

Q179909


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Windows Software Development Kit (SDK)
Microsoft Visual Studio 97

--------------------------------------------------------------------------------


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.
Sample Code

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

Additional query words:

Keywords : kbCtrl kbNTOS kbWinOS2000 kbTabCtrl kbGrpUser kbVS97 kbWinOS
Issue type : kbhowto
Technology : kbvcSearch


Last Reviewed: July 7, 2000
? 2001 Microsoft Corporation. All rights reserved. Terms of Use.
 



--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
<MSDN>
0
 
LVL 3

Author Comment

by:Shay050799
ID: 6163764
my OnItemDraw function, never gets called ?????

shay
0
 
LVL 3

Author Comment

by:Shay050799
ID: 6163767
i m using CFormView with a CTabCtrl on it, not cpropertypages !!!

Shay
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6166209
Hi Shay,
Notice to HOWTO: Change the Text Color of the Selected Property Sheet Tab. Remember:

1. Go to Tab Control Properties of your tab control->Styles tab->Check "Owner draw fixed" check box to make your DrawItem function active
2. Just create CMyTabCtrl class as instruction, you needn't create CMyPropertySheet
3. Declare virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS) in CMyTabCtrl class

Now, use CMyTabCtrl instead of CTabCtrl in your application
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:Shay050799
ID: 6167480
i m not using property sheets at all !!!!
i m using CTabCtrl on CDialog !!

Shay
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6175607
Hi Shay,
Have you read my comment carefully?
You needn't create CMyPropertySheet.
You just need create CMyTabCtrl as in article. Check "Owner draw fixed" check box in Tab Control Properties to active DrawItem function. Then use CMyTabCtrl instead of CTabCtrl. Cheers!

0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6184372
Have you done it, Shay?
0
 
LVL 3

Author Comment

by:Shay050799
ID: 6185747
sorry i was a way !!!
it works great, aparently i need to call OnDrawItem on the dialog, its works great thanks buddy
Shay
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6189102
Cheers!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now