How to change the background color of a tab control

zhaozj2021-02-16  63

How to change the background color of a tab control

Suitable

This Article Was Previously Published Under Q179909

SUMMARYThis 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 INFORMATIONTo 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 sdkthe brushes in this sample Excerpt WERE CREATED IN WM_INITDIALOG AND area Static Handles.

Add the WM_DRAWITEM Message to the Dialog Box's Procedure.

Sample Code

case WM_DRAWITEM: lParam lpdis = (LPDRAWITEMSTRUCT); // 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) {cas 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 you are using mfcthe 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

转载请注明原文地址:https://www.9cbs.com/read-16086.html

New Post(0)