Click here to Skip to main content
11,705,212 members (52,034 online)
Click here to Skip to main content

Hacking the CPropertySheet

, 21 Jun 2004 CPOL 365.3K 165
Rate this:
Please Sign up or sign in to vote.
Some tips and tricks to change the default look & feel and behavior of CPropertySheet class.

Introduction

In my applications, I usually need to change the default look and feel and the behavior of Property Sheets. Working with property sheets and pages is really a pain and you must do most of the work on yourself for non-standard operations (such as changing the coordinates of the buttons, adding new controls to the property sheet, ...etc). In this article, I tried to give as many tips and tricks as I can do.

Before going in to the details, you should first create a CPropertySheet derived class - say CMyPropSheet.

Lets begin with some simple things:

Hiding Standards Buttons

When our property sheet shows up, by default it has all the buttons visible and the Apply button is disabled. In general we don't need to use Apply button. To hide the apply button, you can simply use the following code:

propsheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;

However, this does not work for all buttons. A better approach is to get a handle to the specified button and then threat the button as a normal window. Here is a sample code that hides the cancel button:

CWnd *pWnd = GetDlgItem( IDCANCEL );
pWnd->ShowWindow( FALSE );

When we hide the controls using the ShowWindow function all the other controls remain whereever they were. This may give the property sheet and unappealing look. To avoid this, we should reposition the controls manually. Next subsections shows how to move the controls.

Moving the Standard Buttons

As I mentioned before, once we get the handle to the standard buttons, we can treat them like any other window. The code below first hides Apply and Help buttons, then moves OK and Cancel buttons right.

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

    int ids [] = {IDOK, IDCANCEL};//, ID_APPLY_NOW, IDHELP };
    
    // Hide Apply and Help buttons
    CWnd *pWnd = GetDlgItem (ID_APPLY_NOW);
    pWnd->ShowWindow (FALSE);
    pWnd = GetDlgItem (IDHELP);
    pWnd->ShowWindow (FALSE);
    
    CRect rectBtn;
    int nSpacing = 6;        // space between two buttons...

    for( int i =0; i < sizeof(ids)/sizeof(int); i++)
    {
        GetDlgItem (ids [i])->GetWindowRect (rectBtn);
        
        ScreenToClient (&rectBtn);
        int btnWidth = rectBtn.Width();
        rectBtn.left = rectBtn.left + (btnWidth + nSpacing)* 2;
        rectBtn.right = rectBtn.right + (btnWidth + nSpacing)* 2;

        GetDlgItem (ids [i])->MoveWindow(rectBtn);
    }

    
    return bResult;
}

The following code moves all standard buttons to the right and resizes the property sheet appropriately.

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

    
    int ids[] = { IDOK, IDCANCEL, ID_APPLY_NOW };
    
    CRect rectWnd;
    CRect rectBtn;
    
    GetWindowRect (rectWnd);
    GetDlgItem (IDOK)->GetWindowRect (rectBtn);
    
    int btnWidth = rectBtn.Width();
    int btnHeight = rectBtn.Height();
    int btnOffset = rectWnd.bottom - rectBtn.bottom;
    int btnLeft = rectWnd.right - rectWnd.left;

    rectWnd.bottom = rectBtn.top;
    rectWnd.right = rectWnd.right + btnWidth + btnOffset;
    MoveWindow(rectWnd);
    
    rectBtn.left = btnLeft;
    rectBtn.right = btnLeft + btnWidth;

    for (int i = 0; i < sizeof (ids) / sizeof (int); i++)
    {
        rectBtn.top = (i + 1) * btnOffset + btnHeight * i;
        rectBtn.bottom = rectBtn.top + btnHeight;
        GetDlgItem (ids [i])->MoveWindow (rectBtn);
    }
    
    return bResult;
}

Changing the Tab Label

To change the labels at runtime, we just need to get a pointer to the tab control and then use SetItem functions of the tab control. Here is an example:

TC_ITEM item;
item.mask = TCIF_TEXT;
item.pszText = "New Label";

//Change the label of the first tab (0 is the index of the first tab)...
GetTabControl ()->SetItem (0, &item);

Changing the Tab Label Font

This is also similar to changing the label of the tab. Here is an example code:

m_NewFont.CreateFont (14, 0, 0, 0, 800, TRUE,
  0, 0, 1, 0, 0, 0, 0, _T("Arial") );
GetTabControl()->SetFont (&m_NewFont);

Using Images with Tab Labels

In order to use images with the tab labels, first you have to create a CImageList class with the images you want to use in the tab control. The using SetItem method of CTabCtrl class, you should set the images of the items. Here is an example:

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

    m_imageList.Create (IDB_MYIMAGES, 13, 1, RGB(255,255,255));
    CTabCtrl *pTabCtrl = GetTabControl ();
    pTabCtrl->SetImageList (&m_imageList);
    
    TC_ITEM item;
    item.mask = TCIF_IMAGE;
    for (int i = 0; i < NUMBER_OF_TABS; i++)
    {
        item.iImage = i;
        pTabCtrl->SetItem (i, &item );
    }

    return bResult;
}

Placing a Bitmap in the Property Sheet

The following code places a bitmap in the left-bottom corner of the property sheet.

void CMyPropSheet::OnPaint () 
{
    CPaintDC dc(this); // device context for painting
    
    int nOffset = 6;
    // load IDB_BITMAP1 from our resources
    CBitmap bmp;
    if (bmp.LoadBitmap (IDB_BITMAP1))
    {
        // Get the size of the bitmap
        BITMAP bmpInfo;
        bmp.GetBitmap (&bmpInfo);
        
        // Create an in-memory DC compatible with the
        // display DC we're using to paint
        CDC dcMemory;
        dcMemory.CreateCompatibleDC (&dc);
        
        // Select the bitmap into the in-memory DC
        CBitmap* pOldBitmap = dcMemory.SelectObject (&bmp);
        
        // Find a bottom-left point for the bitmap in the client area
        CRect rect;
        GetClientRect (&rect);
        int nX = rect.left + nOffset;
        int nY = rect.top + (rect.Height () - bmpInfo.bmHeight) - nOffset;
        
        // Copy the bits from the in-memory DC into the on-
        // screen DC to actually do the painting. Use the centerpoint
        // we computed for the target offset.
        dc.BitBlt (nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
            0, 0, SRCCOPY);
        
        dcMemory.SelectObject (pOldBitmap);
    }

    // Do not call CPropertySheet::OnPaint() for painting messages
}

Adding a Control to the Property Sheet

To add your own control to the property sheet, first add a member variable to your header class. The following steps shows you adding an Edit conrol to your property sheet (to the bottom-left corner):

In MyPropSheet.h:

public:
    CEdit m_edit;

In MyPropSheet.cpp:

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

    
    CRect rect;
    
    int nHeight = 24;
    int nWidth = 120;
    int nOffset = 6;
    
    GetClientRect (&rect);

    // Find a bottom-left point for the edit control in the client area
    int nX = rect.left + nOffset;
    int nY = rect.top + (rect.Height() - nHeight) - nOffset;
    
    // finally create the edit control
    m_Edit.CreateEx (WS_EX_CLIENTEDGE, _T("EDIT"), NULL,
                     WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, 
        nX, nY, nWidth, nHeight, m_hWnd, 0, 0 );

    return bResult;
}

A More Complex Scenario

Now we are ready for a more complex operation on these stupid property sheets. Suppose you want to add a header on top of your property sheet. At first, this seems to be an easy task, but when you implement the first thing in your mind, you would mostly be disappointed.

You need a free space on top of the property sheet. Thus you need to increase the size of the property sheet, then move all the buttons and tab control. However this is not enough. The problem is that, when you move your Tab Control, your property pages in the Tab Control will not be moved appropriately. They will remain on the same coordinates (even though the tab control is moved). So, you also have to move the first property page on your tab control. You don't have to move the other property pages, because when you change the active tab, tab control will get the new coordinates automatically and place the property pages correctly. The problem occurs only when the property sheet is created and firstly shown. The code below creates an area on top of the property sheet and moves all the controls and property pages accordingly. You just need to add your header control.

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

    
    int _PropSheetButtons[] = {IDOK, IDCANCEL, ID_APPLY_NOW, IDHEL };
    int m_nHeaderHeight = 70;        //The height of the header control

    CRect rectWnd;
    GetWindowRect (rectWnd);
    ScreenToClient (rectWnd);
    SetWindowPos (NULL, 0, 0, rectWnd.Width(), 
        rectWnd.Height() + m_nHeaderHeight,
        SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
    
    // create your header control here
    //m_HeaderCtrl.CreateEx (NULL, NULL, NULL, 
    //    WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, 
    //    -1, -1, rectWnd.Width (), m_nHeaderHeight - 5, m_hWnd, 0, 0);
    //m_HeaderCtrl.SetTitle (m_sTitle);
    //m_HeaderCtrl.SetIcon (m_nIcon);
    //m_HeaderCtrl.SetDesc (m_sDesc);*/
    
    
    HWND hWnd = (HWND)GetTabControl ()->m_hWnd;
    ASSERT (hWnd != NULL);
    CRect rectOld;
    ::GetWindowRect (hWnd, &rectOld);
    ScreenToClient (&rectOld);
    ::SetWindowPos (hWnd, NULL, rectOld.left, 
                    rectOld.top + m_nHeaderHeight, rectOld.Width (), 
                    rectOld.Height (),
                    SWP_NOZORDER | SWP_NOACTIVATE);
    
    hWnd = (HWND)m_Page1.m_hWnd; // m_Page1 is assumed to be the first page
                 // in your property sheet. de corrections accordingly

    ASSERT (hWnd != NULL);
    ::GetWindowRect (hWnd, &rectOld);
    ScreenToClient (&rectOld);
    ::SetWindowPos (hWnd, NULL, rectOld.left, 
                    rectOld.top + m_nHeaderHeight, 
                    rectOld.Width (), rectOld.Height (),
                    SWP_NOZORDER | SWP_NOACTIVATE);
    
    // move buttons by similar amount
    for (int i = 0; i < sizeof (_PropSheetButtons) / sizeof (int); i++)
    {
        hWnd = ::GetDlgItem (m_hWnd, _PropSheetButtons [i]);
        if (hWnd != NULL)
        {
            ::GetWindowRect (hWnd, &rectOld);
            ScreenToClient (&rectOld);
            ::SetWindowPos (hWnd, NULL, rectOld.left, 
                            rectOld.top + m_nHeaderHeight, 
                            rectOld.Width (), rectOld.Height (),
                            SWP_NOZORDER | SWP_NOACTIVATE);
        }
    }
    
    // Enable Apply Now button
    hWnd = ::GetDlgItem(m_hWnd, ID_APPLY_NOW);
    if (hWnd != NULL)
    {
        ::ShowWindow (hWnd, SW_SHOW);
        ::EnableWindow (hWnd, TRUE);
    }
    
    CenterWindow ();
    
    return bResult;
}

So far so good. Now, I want to change the behavior for the OK, Cancel and Apply buttons. For example, when the user clicks on OK or Cancel button, you may want to do something different rather than closing the property sheet. Here is a general template that can be used to do this kind of stuff:

BOOL CMyPropSheet::OnCommand (WPARAM wParam, LPARAM lParam) 
{
    // allow message map override
    if (CWnd::OnCommand (wParam, lParam))
        return TRUE;
    
    // crack message parameters
    UINT nID = LOWORD(wParam);
    HWND hWndCtrl = (HWND)lParam;
    int nCode = HIWORD(wParam);
    
    // set m_nModalResult to ID of button, whenever button is clicked
    if (hWndCtrl != NULL && nCode == BN_CLICKED)
    {
        if (::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0) &
            (DLGC_BUTTON|DLGC_DEFPUSHBUTTON))
        {
            LONG lStyle = ::GetWindowLong(hWndCtrl, GWL_STYLE) & 0x0F;
            if (lStyle == BS_PUSHBUTTON || lStyle == BS_DEFPUSHBUTTON ||
                lStyle == BS_USERBUTTON || lStyle == BS_OWNERDRAW)
            {
                if (nID == IDOK)
                {
                    if (YOU_WANT_TO_CLOSE_THE_PROPERTY_SHEET)
                    {
                        // do whatever you want before closing the property page

                        // You dont have to assign nID to 
                        // m_nModalResult. If you want to 
                        // return IDOK or IDCANCEL instead of 
                        // the default return value, 
                        // you can do it by assigning IDOK or 
                        // IDCANCEL to m_nModalResult
                        m_nModalResult = nID;
                    }
                    else
                    {
                        // do whatever you want.
                        return TRUE;
                    }
                }
                else if (nID == ID_APPLY_NOW)
                {
                    if (YOU_WANT_TO_CLOSE_THE_PROPERTY_SHEET)
                    {
                        // do whatever you want before 
                        // closing the property page
                        
                        // You dont have to assign nID to 
                        // m_nModalResult. If you want to 
                        // return IDOK or IDCANCEL instead 
                        // of the default return value, 
                        // you can do it by assigning IDOK or 
                        // IDCANCEL to m_nModalResult
                        m_nModalResult = nID;
                    }
                    else
                    {
                        // do whatever you want.
                        return TRUE;
                    }
                }
                else if (nID == IDCANCEL)
                {
                    if (YOU_WANT_TO_CLOSE_THE_PROPERTY_SHEET)
                    {
                        // do whatever you want before 
                        // closing the property page

                        // You dont have to assign nID to 
                        // m_nModalResult. If you want to 
                        // return IDOK or IDCANCEL instead 
                        // of the default return value, 
                        // you can do it by assigning IDOK or 
                        // IDCANCEL to m_nModalResult
                        m_nModalResult = nID;
                    }
                    else
                    {
                        // do whatever you want.
                        return TRUE;
                    }
                }
            }
        }
    }
    return FALSE;
}

Thanks...

...To Zafir Anjum for his very useful articles about property sheets on CodeGuru.com.

...To The Code Project, for providing a useable forum for all of us.

Some points that you should keep in mind

  • I am not an expert in GUI programming. These tips and tricks are all from my own experience. Thus, they may contain some bugs. But till now, they are working fine for me Smile | :)
  • If you need resizable property sheets, I strongly recommend you to have a look at ResizableLib.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mustafa Demirhan
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
JunfengGuo12-Jun-13 15:20
memberJunfengGuo12-Jun-13 15:20 
GeneralMy vote of 5 Pin
G Haranadh1-Jul-10 3:14
memberG Haranadh1-Jul-10 3:14 
GeneralRe: My vote of 5 Pin
defiantgti2-May-11 8:59
memberdefiantgti2-May-11 8:59 
GeneralThank you for this article Pin
Member 16456951-Apr-08 4:37
memberMember 16456951-Apr-08 4:37 
GeneralNice Article Pin
Mushq14-Aug-07 19:28
memberMushq14-Aug-07 19:28 
Generalnice and helpful article Pin
Abu Mami5-Jul-07 8:28
memberAbu Mami5-Jul-07 8:28 
GeneralMoving the Propert Sheet Tab controls Pin
sath_1712-Dec-06 20:51
membersath_1712-Dec-06 20:51 
GeneralAlt + Selection key for buttons in a property sheet Pin
DivyaRS27-Nov-06 22:02
memberDivyaRS27-Nov-06 22:02 
GeneralSharing data between property pages Pin
butchi peddi20-Nov-06 18:40
memberbutchi peddi20-Nov-06 18:40 
Generalmy vote is zero... Pin
__PPS__17-Nov-06 8:26
member__PPS__17-Nov-06 8:26 
GeneralRe: my vote is zero... Pin
John Simmons / outlaw programmer17-Nov-06 9:56
memberJohn Simmons / outlaw programmer17-Nov-06 9:56 
GeneralRe: my vote is zero... Pin
__PPS__18-Nov-06 5:59
member__PPS__18-Nov-06 5:59 
GeneralRe: my vote is zero... Pin
John Simmons / outlaw programmer1-Dec-06 8:31
memberJohn Simmons / outlaw programmer1-Dec-06 8:31 
JokeRe: my vote is zero... Pin
Bruno Scopinho31-Dec-06 0:19
memberBruno Scopinho31-Dec-06 0:19 
GeneralRe: my vote is zero... Pin
l_d_allan29-Oct-08 6:15
memberl_d_allan29-Oct-08 6:15 
QuestionHow to gain access to the property page from the property sheet? Pin
Aki Wang25-Jul-06 0:05
memberAki Wang25-Jul-06 0:05 
AnswerRe: How to gain access to the property page from the property sheet? Pin
Mustafa Demirhan25-Jul-06 7:26
memberMustafa Demirhan25-Jul-06 7:26 
GeneralRe: How to gain access to the property page from the property sheet? Pin
Aki Wang25-Jul-06 16:01
memberAki Wang25-Jul-06 16:01 
QuestionHow to change the words shown on the buttons of the property sheet? Pin
Aki Wang19-Jul-06 19:14
memberAki Wang19-Jul-06 19:14 
Hi,

These article is really a very useful article.
Thanks for your kidly sharing it on Code Project!

But now, I encounter a situation that I dont want to show "OK"
on the OK button.

Is there any solution which can change the words shown on the buttons of the property sheet?

Thank you very much!


AnswerRe: How to change the words shown on the buttons of the property sheet? Pin
Mustafa Demirhan19-Jul-06 20:47
memberMustafa Demirhan19-Jul-06 20:47 
AnswerRe: How to change the words shown on the buttons of the property sheet? Pin
Aki Wang19-Jul-06 20:49
memberAki Wang19-Jul-06 20:49 
Generalupside down Pin
Sohail_CSC4-Jul-06 20:56
memberSohail_CSC4-Jul-06 20:56 
GeneralFeedback, and HOWTO: change the font used for the sheet and contained pages Pin
-Dy20-Apr-06 2:23
member-Dy20-Apr-06 2:23 
GeneralRe: Feedback, and HOWTO: change the font used for the sheet and contained pages Pin
Mustafa Demirhan19-Jul-06 20:49
memberMustafa Demirhan19-Jul-06 20:49 
GeneralCentering tab label Pin
Toshiya22-Mar-06 21:29
memberToshiya22-Mar-06 21:29 
Generalhide property page at run time Pin
sundee14-Dec-05 23:58
membersundee14-Dec-05 23:58 
GeneralRe: hide property page at run time Pin
Mustafa Demirhan15-Dec-05 20:00
memberMustafa Demirhan15-Dec-05 20:00 
GeneralRe: hide property page at run time Pin
sundee16-Dec-05 4:51
membersundee16-Dec-05 4:51 
GeneralTab label with image Pin
shinyhui19-Sep-05 20:36
membershinyhui19-Sep-05 20:36 
QuestionRe: Tab label with image Pin
WhiteSky10-Jul-07 21:24
memberWhiteSky10-Jul-07 21:24 
GeneralProblem in Placing a Bitmap in the Property Sheet Pin
PoornimaS23-Sep-04 20:58
memberPoornimaS23-Sep-04 20:58 
GeneralRe: Problem in Placing a Bitmap in the Property Sheet Pin
Mustafa Demirhan23-Sep-04 23:48
memberMustafa Demirhan23-Sep-04 23:48 
GeneralRe: Problem in Placing a Bitmap in the Property Sheet Pin
PoornimaS24-Sep-04 1:00
memberPoornimaS24-Sep-04 1:00 
GeneralRe: Problem in Placing a Bitmap in the Property Sheet Pin
raghavr27-Feb-05 2:57
sussraghavr27-Feb-05 2:57 
QuestionRe: Problem in Placing a Bitmap in the Property Sheet Pin
WhiteSky10-Jul-07 21:24
memberWhiteSky10-Jul-07 21:24 
GeneralLots of property pages Pin
DanYELL3-Sep-04 4:34
memberDanYELL3-Sep-04 4:34 
GeneralRe: Lots of property pages Pin
Mustafa Demirhan3-Sep-04 7:07
memberMustafa Demirhan3-Sep-04 7:07 
GeneralRe: Lots of property pages Pin
DanYELL4-Sep-04 7:05
memberDanYELL4-Sep-04 7:05 
GeneralThank you !! Pin
VoidIndigo12-Aug-04 11:55
memberVoidIndigo12-Aug-04 11:55 
GeneralRe: Thank you !! Pin
Mustafa Demirhan12-Aug-04 22:05
memberMustafa Demirhan12-Aug-04 22:05 
GeneralRe: Thank you !! Pin
KarstenK12-Nov-04 2:45
memberKarstenK12-Nov-04 2:45 
GeneralChanging background color of property page Pin
rems8126-Jul-04 17:07
memberrems8126-Jul-04 17:07 
QuestionHow can I show CProeprtySheet A in CPropertySheet B as if pages of A are seamless integral parts of B? Pin
snowshoe16-Jul-04 8:41
susssnowshoe16-Jul-04 8:41 
QuestionHow can I Update a PropertyPage before calling DoModal()?? Pin
biu-jee5-Jul-04 2:41
memberbiu-jee5-Jul-04 2:41 
AnswerRe: How can I Update a PropertyPage before calling DoModal()?? Pin
aouadn13-Jul-04 5:46
memberaouadn13-Jul-04 5:46 
AnswerRe: How can I Update a PropertyPage before calling DoModal()?? Pin
aouadn13-Jul-04 5:47
sussaouadn13-Jul-04 5:47 
AnswerRe: How can I Update a PropertyPage before calling DoModal()?? Pin
aouadn13-Jul-04 5:49
sussaouadn13-Jul-04 5:49 
GeneralAdd two PropertySheets in a dialog Pin
asef16-Jun-04 0:36
memberasef16-Jun-04 0:36 
GeneralRe: Add two PropertySheets in a dialog Pin
Mustafa Demirhan16-Jun-04 6:57
memberMustafa Demirhan16-Jun-04 6:57 
GeneralRe: Add two PropertySheets in a dialog Pin
Paolo Messina22-Jun-04 5:50
memberPaolo Messina22-Jun-04 5:50 

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
Web01 | 2.8.150819.1 | Last Updated 22 Jun 2004
Article Copyright 2002 by Mustafa Demirhan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid