How To Change the Background Color of a Tab Control

原创 2004年07月15日 13:55:00

How To Change the Background Color of a Tab Control

适用于

This article was previously published under Q179909

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

FreeCodeCamp(一)学习笔记

FrontEndDevelopmentCertificationHTML5andCSSSay Hello to HTML Elementshello world Hello worldHeadline...
  • BATManer
  • BATManer
  • 2016年07月05日 23:21
  • 4276

sklearn 源码分析系列:neighbors(3)

sklearn 源码分析系列:neighbors(3)by DemonSonggithub源码链接(https://github.com/demonSong/DML) by\space DemonSo...
  • u014688145
  • u014688145
  • 2017年03月21日 16:12
  • 1751

appstore_被拒记录

发件人 Apple 1.0 二进制文件被拒绝 2016年3月24日 2.16 Details Your app declares support for audio in the UIBackgro...
  • u012576807
  • u012576807
  • 2016年03月24日 12:45
  • 13878

Shows how to change the background color of an RTF box.

  • 2006年02月23日 09:05
  • 24KB
  • 下载

How to change tooltip background color in Unity?

Here is a screenshot of Eclipse which displays some source code in a tool tip with black text on bla...
  • snakewarhead
  • snakewarhead
  • 2012年05月01日 20:51
  • 876

How to change the text color of the selected property sheet tab in Visual C++

转自:http://support.microsoft.com/kb/206626/en-us Article ID: 206626 - View products that this arti...
  • tomisaboy
  • tomisaboy
  • 2014年11月25日 20:29
  • 579

How to Use MFC Tab Control in Developing VC++ Applications

How to Use MFC Tab Control in Developing VC++ ApplicationsIn this article, I will show you how to us...
  • buaazt
  • buaazt
  • 2015年03月02日 22:10
  • 793

How to change the Text color of Menu item in Android?

How to change the Text color of Menu item in Android? up vote2down votefavori...
  • Aaronzzq
  • Aaronzzq
  • 2012年06月15日 10:35
  • 1627

set transparent button image and background color to button

I have a transparent button image with white border and without label. I want to create a button whi...
  • zery
  • zery
  • 2012年08月13日 13:14
  • 967

Android4.4使用 webview控件报错nativeOnDrawfailed;clearing to background color

最近在整合 apk调用 flash的时候,里面用到 webview控件,然而还是每次打开应用都会提示如下错误:  W/AwContents(2825):  nativeOnDraw failed; ...
  • young471479588
  • young471479588
  • 2014年09月27日 12:25
  • 7382
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How To Change the Background Color of a Tab Control
举报原因:
原因补充:

(最多只允许输入30个字)