CommandBars Articles and Tutorials

Create a custom theme for your application

Author: Kirk Stowell
Platform: Visual C++ MFC

You can create a custom theme for your application by deriving a class from any of the theme classes available in the toolkit.

Create a double gripper theme similar to Visual Studio 6.0.

  1. Create a new class derived from one of the predefined themes found in the toolkit. We are going to use CXTPDefaultTheme, however you can use any of the following theme classes:

    CXTPDefaultTheme to inherit Office 2000 theme
    CXTPOfficeTheme to inherit Office XP theme
    CXTPOffice2003Theme to inherit Office 2003 theme
    CXTPNativeXPTheme to inherit Native XP theme

     class CDoubleGripperTheme : public CXTPDefaultTheme
     {
    
     };
    
  2. Override the DrawCommandBarGripper of CXTPDefaultTheme base class (See XTPPaintManager.h). This will allow us to add our own custom look for drawing the command bar grippers
     class CDoubleGripperTheme : public CXTPDefaultTheme
     {
         virtual CSize DrawCommandBarGripper(
             CDC* pDC, CXTPCommandBar* pBar, BOOL bDraw);
     };
    
     [...]
    
     // DrawCommandBarGripper function. 
     // if bDraw if FALSE must return gripper size.
     // if bDraw is TRUE must draw gripper.  
     CSize CDoubleGripperTheme::DrawCommandBarGripper(CDC* pDC,
          CXTPCommandBar* pBar, BOOL bDraw)
     {
         // If Toolbar is vertical docked
         if (pBar->GetPosition() == xtpBarRight ||
             pBar->GetPosition() == xtpBarLeft)
         {
             if (bDraw)
             {
                 CXTPClientRect rc(pBar);
                 Draw3dRect(pDC, CRect(3, 3, rc.right - 3, 6),
                     COLOR_BTNHILIGHT, COLOR_3DSHADOW);
                 Draw3dRect(pDC, CRect(3, 7, rc.right - 3, 10),
                     COLOR_BTNHILIGHT, COLOR_3DSHADOW);
             }
             return CSize(0, 10);
         }
         // if Toolbar is horizontal  docked 
         else if (pBar->GetPosition() == xtpBarTop ||
                 pBar->GetPosition() == xtpBarBottom)
             {
                CXTPClientRect rc(pBar);
                if (bDraw)
                {
                    Draw3dRect(pDC, CRect(3, 3, 6, rc.bottom - 3),
                        COLOR_BTNHILIGHT, COLOR_3DSHADOW);
                    Draw3dRect(pDC, CRect(7, 3, 10, rc.bottom - 3),
                        COLOR_BTNHILIGHT, COLOR_3DSHADOW);
                }
                 return CSize(10, 0);
             }
        }
        return CXTPDefaultTheme::DrawCommandBarGripper(pDC, pBar, bDraw);
     }
    
  3. Call CXTPPaintManager::SetCustomTheme from CMainFrame's OnCreate method to use the theme we just created
     int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
     {
         ...
    
             // Use our own theme for drawing command bar grippers.
             CXTPPaintManager::SetCustomTheme(new
             CDoubleGripperTheme());
    
         return 0;
     }
    

    Custom Theme