Adding a Button to CPropertySheet | CodeGuru

Adding a Button to CPropertySheet

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 5, 1999
1 minute read
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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.