Click here to Skip to main content
Click here to Skip to main content

XP Themes Tab Control in any orientation

, 6 Mar 2004
Rate this:
Please Sign up or sign in to vote.
How to make XP Themes Tab Control work properly in other than top orientation.

XPTabApp Image Using CXPTabCtrl

Using CXPTabCtrl, XP Themes Tab Control, left, bottom and right orientation

Introduction

The article shows how to solve a problem, which appears on the Windows Tab Control when using Windows XP operating system with XP Themes enabled. Tab control behaves as if it has only one orientation - top. In fact, it has all four orientations, but all four of them (for some reason) have the same appearance.

It is demonstrated how to use the proposed class CXPTabCtrl, Windows tab control extension as a solution to this problem. CXPTabCtrl detects the operating system and tab control orientation:

  • If it is not XP system or XP Themes are not enabled - it uses default Windows tab behavior (it does not do anything extra).
  • If it is XP system and XP Themes are enabled - it draws tab control properly in appropriate direction.

CXPTabCtrl control also works properly if the state of XP Themes enabling is changed during the running of the application. Tab control hot tracking is also supported.

Background

There are a lots of Windows applications developed for earlier Windows versions, which use bottom or other "not-top" oriented Windows tab control. The problem is: if the application is running on an XP system with XP Themes, Windows tab control is not shown properly as it used to be.

XPTabApp Image default SysTabControl32

Problems when using default SysTabControl32 (CTabCtrl) with XP Themes, bottom orientation

One possibility to solve this problem is to change all tab controls in these applications to be top oriented. This would involve change of the look of the applications and painful Windows resource changes and possible code changes.

Another possibility (used for CXPTabCtrl) is to use a thin wrapper around the tab control and to simulate missing orientations. CXPTabCtrl uses available XP Themes "top-tab" look as a template and adapts it for other tab orientations:

  • Bottom orientation:
    1. use top background bitmap,
    2. mirror it vertically,
    3. draw icon and text on mirrored background.

    Build CXPTabCtrl bottom orientation

  • Left orientation:
    1. use top background bitmap,
    2. draw icon and text on the background,
    3. rotate the bitmap for 90°.

    Build CXPTabCtrl left orientation

  • Right orientation:
    1. use top background bitmap,
    2. mirror it vertically,
    3. draw icon and text on the background,
    4. rotate the bitmap for 90°.

    Build CXPTabCtrl right orientation

  • Top orientation (used only for test purposes, could use default from XP Themes Windows XP):
    1. use top background bitmap,
    2. draw icon and text.

    Build CXPTabCtrl top orientation

Notice also that the body of the tab control has to be manipulated, mirrored or rotated, not only the "tab" parts of the tab control (because a "tab body" has texture and shadows).

How to use it

The CXPTabCtrl class is simple to use. To add it to your project, please follow the steps below:

  1. Put its source files (XPTabCtrl.cpp and XPTabCtrl.h) into the proper project folder and add their file names to your Visual Studio project.
  2. Include its header to the appropriate header file - usually dialog class header where class CXPTabCtrl is used. If you plan to use CXPTabCtrl in several places of your application, you can add it only to your StdAfx.h file:
    #include "XPTabCtrl.h"
  3. You should replace CTabCtrl with CXPTabCtrl everywhere in the project where you want new XP Themes tab behavior.
    CXPTabCtrl  m_tabCtrl
  4. If CXPTabCtrl has images, then appropriate bitmap IDB_TABIMAGES (or other with correct tab images) should be included as resource bitmap. It is also necessary to call InitImageList(IDB_TABIMAGES) function, from OnInitDialog or other appropriate initializing place:
    m_tabCtrl.InitImageList(IDB_TABIMAGES);
    // only necessary to call if tabs have images

Although tab control orientation is usually set in resource editor or in Tab's Create function, CXPTabCtrl orientation can be changed any time while application is running.

Points of Interest

This sample could also be used as an example of how to solve some other problems in Windows programming. Other techniques shown in this sample include:

  1. How to mirror bitmap image vertically fast and simply by using only two Windows calls GetDIBits and SetDIBits. This works in any color resolution.
  2. How to rotate rectangular bitmap image fast and in any color resolution.
  3. How to use inline assembler for lengthy repetitive processing, in this case, rotating bitmap image pixels. If image is of large dimensions (large tab control body), a C style coding could take considerable more time than assembler version.
  4. How to use XP Themes functions in applications, which can run on earlier operating systems.
  5. How to enable XP Themes dialog texture background.
  6. How to make applications XP Themes aware by simply copying XP manifest, resource "24", ID "1" from one application resource to another.
  7. How to use tab ToolTips.

Please note

  • The sample is created and compiled on VC++ 6.0 service pack 5.
  • The sample has been tested on Windows XP (XP Themes enabled or disabled while sample was running).
  • The sample has also been tested on Windows 2000, Windows NT 4.0 (CXPTabCtr does not have effect).
  • Other Windows systems could have possible problems (?)
  • The sample has been tested on the following color resolutions: 8 bit, 16 bit, 24 bit and 32 bit colors.
  • The sample has not been tested on multi-line (stacked) tabs, could be problems (?)

Correspondence

Please post your questions, suggestions and bug reports only to the forum below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Adi DEDIC

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
Generalcontrol repaint very slowly PinmemberDzungcs24-Oct-08 23:00 
GeneralRe: control repaint very slowly PinmemberDzungcs24-Oct-08 23:04 
GeneralTab Control on Dialog Box using ATL PinmemberJPMPHOENIX10-Aug-07 16:20 
GeneralExremely difficult code to follow [modified] PinmemberAlïen6-Jul-07 2:34 
GeneralRe: Exremely difficult code to follow PinmemberRandor29-Aug-07 22:04 
GeneralIncorrect drawing at many tabs Pinmemberbasti27112-Jul-07 0:49 
GeneralVerticalTab Crash [modified] PinmemberMadAlbert19-Jun-07 21:38 
QuestionTmobile code? PinmemberMsftone8-Apr-07 17:42 
GeneralOwner drawn tab control problem in Vista PinmemberRamya_Indian6923-Mar-07 0:14 
GeneralPure win32 port. Pinmemberadel amro10-Feb-07 4:41 
QuestionWhy so many CRects to get the Tab Control dimensions ? Pinmembererajsri17-Jan-07 8:23 
QuestionNo highlight when pointing to other tab Pinmemberdavidlks16-Jul-06 4:14 
Generaleditable tab item PinmemberPaulaPaziani4-May-06 10:21 
GeneralRefresh Problem Pinmembersmo7k15-Jul-05 18:46 
Generalproblem with vertical tab PinmemberHamid Reza Mohammadi1-Jul-05 20:23 
GeneralRe: problem with vertical tab Pinmembererajsri12-Feb-07 0:00 
GeneralDefault status PinmemberPaper#15-Jun-05 8:06 
GeneralLooking for latest updated sources Pinmemberbfigg9722921-Apr-05 12:02 
GeneralXP Theme using Manifest ?! PinmemberDJ Mantra7-Feb-05 7:07 
GeneralRe: XP Theme using Manifest ?! PinmemberDavid Pritchard20-Apr-05 9:40 
GeneralVertical Bug Pinsussketernal26-Dec-04 22:11 
Generalmemory leak Pinmemberpaul.heil7-Dec-04 7:34 
GeneralUsing true color images on tabs PinmemberMMaker30-Nov-04 21:58 
Questionanyone fixed left orientation with themes disabled ? Pinmemberfredwobus2-Nov-04 0:52 
GeneralAdding pages PinmemberSteve Kelly28-May-04 6:37 
GeneralRe: Adding pages PinmemberSiHot26-Jul-07 4:44 
GeneralRough patch for owner-draw tabs PinmemberDavid Pritchard25-May-04 3:57 
OK, my problem with the XPTabCtrl + MadButch patch was that it doesn't allow for owner-drawn tabs. However, the fix for this is not too difficult. The problem is that the DrawTabItem function assumes the standard case (text/image list). So we need to test for the owner-draw flag, and if activated, call DrawItem with the correct info, thusly:
 
void CXPTabCtrl::DrawTabItem(CDC* pDC, int ixItem, const CRect& rcItemC, UINT uiFlags)
{
// DP: 25/05/2004 Fix for owner-draw tabs
if (GetStyle() & TCS_OWNERDRAWFIXED)
{
DrawOwnerDrawTabItem(pDC, ixItem, rcItemC, uiFlags);
}
else
{

// Original code...
}
}
 
// DP: 25/05/2004 Fix for owner-draw tabs
void CXPTabCtrl::DrawOwnerDrawTabItem(CDC* pDC,int ixItem,const CRect& rcItemC,UINT uiFlags)
{
DRAWITEMSTRUCT dis;
TCITEM tcitem;
 
ASSERT(pDC);
if (pDC)
{
// Simply build DRAWITEMSTRUCT info and call DrawItem
dis.CtlID = GetDlgCtrlID();
dis.CtlType = ODT_TAB;
dis.hDC = pDC->m_hDC;
dis.hwndItem = GetSafeHwnd();
dis.itemAction = ODA_DRAWENTIRE;
 
// Get item data - itemdata in DRAWITEMSTRUCT is the lParam from itemdata (normally!)
VERIFY(TabCtrl_GetItem(GetSafeHwnd(), ixItem, &tcitem));
dis.itemData = tcitem.lParam;
 
dis.itemID = ixItem;
dis.itemState = ((uiFlags&2)? ODS_SELECTED:0);
dis.rcItem = rcItemC;
 
// Call drawitem!
DrawItem(&dis);
}
}

 
I hope I got the DRAWITEMSTRUCT stuff right. There may be some minor detail I missed.
 
This fixes the problem...almost. If your custom drawing in DrawItem is done via the DC passed via the DRAWITEMSTRUCT, you're fine. However, I have sinned because I don't do this. I create some custom controls on the tabs and they don't get passed the DC, they just repaint themselves. This means that they get overpainted by the code at the end of DrawThemesXpTabItem, which dumps the content of the MemDC into the window DC. So I have come up with an ugly patch for this. Improvements welcome.
 
What I do is simply flag the owner-draw case and delay the call to DrawTabItem to the end. I have to save the ixItem variable because it's changed afterwards and I didn't want to inquire too closely as to why.
 
void CXPTabCtrl::DrawThemesXpTabItem(CDC* pDC, int ixItem, const CRect& rcItem, UINT uiFlag)
{ // uiFlag(1/0):1=Type(body/tab);2=Sel(y/n);4=Hot(y/n);8=bBottom(y/n);16=rotate(y/n)
 
BOOL bBody =(uiFlag& 1)?TRUE:FALSE;
BOOL bSel =(uiFlag& 2)?TRUE:FALSE;
BOOL bHot =(uiFlag& 4)?TRUE:FALSE;
BOOL bBottom=(uiFlag& 8)?TRUE:FALSE; // mirror
BOOL bVertic=(uiFlag&16)?TRUE:FALSE; // rotate
BOOL bLeftTab=!bBottom && bVertic && !bBody;
 
// DP: 25/05/2004 Fix for owner-draw tabs Part 1
// Must delay DrawItem call for owner-draw tabs to avoid overpainting (see below)
bool bDelayDrawItem = ((GetStyle() & TCS_OWNERDRAWFIXED) != 0);
int ixDelayItem = -1;

 
CSize szBmp=rcItem.Size();
if(bVertic) SwapVars(szBmp.cx,szBmp.cy);
 
...
 
if(bVertic)
{ if(bBody || !bBottom) bihOut.biHeight=-szBmp.cy;
if(!bBottom && !bBody && ixItem>=0) //
{ if(bSel) rcMem.bottom--;
 
// DP: 25/05/2004 Fix for owner-draw tabs Part 2
// Delay DrawItem call for owner-draw tabs to avoid overpainting (see below)
if (!bDelayDrawItem)
{
DrawTabItem(&dcMem, ixItem, rcMem, uiFlag);
}
else
{
ixDelayItem = ixItem;
}
ixItem=-1;

} } // rotate or mirror
...
 
if(!bBody && ixItem>=0) //
{ if(bSel) rcMem.bottom--;
 
// DP: 25/05/2004 Fix for owner-draw tabs Part 3
// Delay DrawItem call for owner-draw tabs to avoid overpainting (see below)
if (!bDelayDrawItem)
{
DrawTabItem(&dcMem, ixItem, rcMem, uiFlag);
}
else
{
ixDelayItem = ixItem;
}

 
if(bVertic) // if it is right tab, do GetDIBits again
{ bihOut.biHeight=-szBmp.cy;
GetDIBits(*pDC, bmpMem.operator HBITMAP(),nStart,szBmp.cy-nLenSub,pcImg,&biOut,DIB_RGB_COLORS);
} }
 
...
 
// 6th blit mirrored/rotated image to the screen
pDC->BitBlt(rcItem.left,rcItem.top,szBmp.cx,szBmp.cy,&dcMem,0,0,SRCCOPY); //
dcMem.SelectObject(pBmpOld);
 
// DP: 25/05/2004 Fix for owner-draw tabs. Part 4
// Must draw last in case user does painting outside DC, because otherwise BitBlt will overpaint it
if (bDelayDrawItem && ixDelayItem != -1)
{
DrawTabItem(&dcMem, ixDelayItem, rcMem, uiFlag);
}

}

 
Well, it's not pretty but it's working for me at the moment. I'll keep you posted.
GeneralCrash problem PinmemberDavid Pritchard22-Sep-04 0:41 
GeneralPatch for the patched patch PinmemberDavid Pritchard22-Sep-04 2:33 
GeneralFix for resize ghosting PinmemberMadButch18-May-04 23:06 
GeneralRe: Fix for resize ghosting PinmemberDavid Pritchard24-May-04 12:05 
GeneralRe: Fix for resize ghosting PinsussAnonymous24-May-04 12:10 
GeneralRe: Fix for resize ghosting PinmemberDavid Pritchard24-May-04 13:55 
GeneralRe: Fix for resize ghosting PinmemberMadButch24-May-04 23:13 
GeneralRe: Fix for resize ghosting PinmemberDavid Pritchard24-May-04 23:36 
GeneralTCN_SELCHANGE no longer handled by parent class PinmemberEKL18-Mar-04 8:14 
GeneralRe: TCN_SELCHANGE no longer handled by parent class Pinmemberisglass24-Apr-04 0:46 
GeneralRe: TCN_SELCHANGE no longer handled by parent class PinmemberMadButch29-Apr-04 1:09 
GeneralRe: TCN_SELCHANGE no longer handled by parent class Pinmemberisglass29-Apr-04 11:51 
GeneralRe: TCN_SELCHANGE no longer handled by parent class PinmemberMadButch16-May-04 22:42 
GeneralLeft tab being chopped PinmemberJoel Lucsy9-Mar-04 13:10 
Generala bug Pinmemberbenben8-Mar-04 17:54 
GeneralNice work Pinmembersnakeware7-Mar-04 23:05 
GeneralRe: Nice work PinmemberAdi DEDIC8-Mar-04 0:43 
GeneralGood work, but not finished! Pinmembermortal7-Mar-04 22:14 
GeneralProgramming is never finished PinmemberAdi DEDIC8-Mar-04 1:52 
GeneralUse of your control Pinmemberng_pearce7-Mar-04 18:05 
GeneralRe: Use of your control PinmemberAdi DEDIC8-Mar-04 0:12 
GeneralBugs PinmemberRazr3337-Mar-04 8:02 
GeneralRe: Bugs PinmemberAdi DEDIC8-Mar-04 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 7 Mar 2004
Article Copyright 2004 by Adi DEDIC
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid