Adding a Button to CPropertySheet

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

In my app I use a CPropertySheet and CPropertyPages to display properties for
individual days. If the user wanted all the weeks days to be Setup with the same
properties, I wanted a way to copy an individual day’s properties to all the other days of
the week. The solution I came up with was to create a "Copy" button in the
CPropertySheet rather than in each CPropertyPage as show in the picture below…

newpropbutton.gif

  • First sub-class CPropertySheet with a class of your own using ClassWizard, say
    CMyPropertySheet.
  • Next in your MyPropertySheet.h add a member function CButton
    m_ButtonCopy. Also in your resource.h add a
    #define IDC_BUTTON_COPY 0x02000. where 0x02000 is
    a resource ID that has not been used yet.
  • Then over-ride the CPropertySheet::OnInitDialog function and insert the following
    code…

CRect rect, tabrect;
int width;

//Get button sizes and positions

GetDlgItem(IDOK)->GetWindowRect(rect);
GetTabControl()->GetWindowRect(tabrect);
ScreenToClient(rect); ScreenToClient(tabrect);

//New button -> width, height and Y-coordiate of IDOK
// -> X-coordinate of tab control
width = rect.Width();
rect.left = tabrect.left; rect.right = tabrect.left + width;

//Create new "Add" button and set standard font
m_ButtonCopy.Create("Copy",

BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect, this, IDC_BUTTON_COPY);
m_ButtonCopy.SetFont(GetFont());

If your were to compile at this point you would now see the new Copy
button in the CPropertySheet; however the button would not do anything. To handle a single
button click added the following line to your message map declaration…


BEGIN_MESSAGE_MAP(CMyPropertySheet, CPropertySheet)
//{{AFX_MSG_MAP(CMyPropertySheet)
// NOTE – the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_COPY, OnButtonCopy)
END_MESSAGE_MAP()

Lastly add a helper function called OnButtonCopy() and insert your
code for when the button is clicked.

Download demo project – 17 KB

Download source – 2 KB

Date Last Updated: March 5, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read