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 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



Comments

  • How to add Combo Box To the property sheet ?

    Posted by babumohan on 10/10/2004 05:17am

    I tried to add a combo box to the property sheet like the button in your program. I can see the Combo Box . But I cannot be able to drop down the list of items of Combo Box.
    Lemme know how to fix the issue?
    With regards
    babu

    • Add Combobox

      Posted by Jeremy Davis on 10/12/2004 05:42am

      When creating the combobox have you specified the expanded size? When displayed, the combobox will appear closed, but you must create it as if it were expanded. Look at www.codeproject.com for more help as it's much better than codeguru!

      Reply
    Reply
  • Is it possible to locate the button at other place in Propertysheet dialog??

    Posted by Legacy on 04/09/2002 12:00am

    Originally posted by: Santosh

    In this sample, the button is added by taking tabcontrol's
    x and y co-ordinates.

    Is it possible to add it any where in the dialog??

    Reply
  • Can it work in a modalless propertysheet

    Posted by Legacy on 03/30/2002 12:00am

    Originally posted by: hjzhai

    Hi:
    I read your work,it's great.And I tried to add a button to a modalless propertysheet,but failed.Would you like to give me some advice?
    Thanks in advance!

    Reply
  • how to add menu to a property sheet

    Posted by Legacy on 03/10/2002 12:00am

    Originally posted by: Gunjan Goel

    hi,

    can u tell me how to add a menu to a property sheet/property page.
    it cld beadded to a dialog if the style was popup...
    as soon as i made it Child the menu addition option became disabled.

    pls. help
    gunjan

    Reply
  • How to add the button to the top of the sheet?

    Posted by Legacy on 02/08/2002 12:00am

    Originally posted by: sujith.s

    How can i add the button to the top of a modeless propertysheet?ie. above the property page.

    Reply
  • I have a solution to my previous Q.!!!!

    Posted by Legacy on 04/09/2001 12:00am

    Originally posted by: Ohad Redlish

    contact me if u r interested:
    ohadr@nice.com
    and i will show you how to make the unvisible-buttons visible. since in Modeless style, the buttons are invisible, you have to FORCE the system show them.
    Ohad

    Reply
  • propertySheet and buttons

    Posted by Legacy on 12/14/2000 12:00am

    Originally posted by: Ohad

    hi.
    i read your advices. i think it's very nice, but how do i make the buttons in a PropertySheet visible, when the sheet is in Modeless style???
    this is much much tougher!!!

    please advise if you know anything
    thanks,
    Ohad R
    SW engineer
    NICE Systems
    Israel

    Reply
  • Help me on button in propertysheet

    Posted by Legacy on 09/09/1999 12:00am

    Originally posted by: Hanying chen

    Hi, everyone

    I just used this code. But the button can't work at all. I examed the code carefully, still don't know why. The only difference is my propertysheet is a modeless dialog.
    Is it the reason?

    Thanks

    Hanying Chen
    99/9/9

    Reply
  • Here's a little routine to set tab order of buttons...

    Posted by Legacy on 03/16/1999 12:00am

    Originally posted by: Chris Conn

    BOOL SetTabOrder(BOOL bShowWindow, HWND hWndTop, ...)
    {
    ASSERT(hWndTop != NULL);
    BOOL
    bRtrn = FALSE;
    UINT
    uiFlags = SWP_NOMOVE | SWP_NOSIZE;

    // Validate that something was sent in
    if (hWndTop != NULL)
    {
    HWND
    curHwnd = hWndTop,
    prevHwnd = HWND_TOP;

    // Add the show windows if needed
    if (bShowWindow == TRUE)
    uiFlags |= SWP_SHOWWINDOW;

    va_list curArg;
    va_start(curArg, curHwnd );
    // Initialize variable arguments

    // Additional parameters can be optional
    while (curHwnd != NULL)
    {
    // Now add any additional windows into the tab order
    bRtrn = ::SetWindowPos(curHwnd, prevHwnd, 0, 0, 0, 0, uiFlags);

    // Keep track of the last windows handle
    prevHwnd = curHwnd;

    // Get the next argument
    curHwnd = va_arg(curArg, HWND);
    } // while (lpTmp != NULL)

    va_end(curArg); // Reset variable arguments
    } // End of check whether handle is valid

    return bRtrn;
    } // SetTabOrder()

    I've used this successfully for several property sheets that I am currently using (and also dynamically changing by adding additional buttons or other controls). Here's the mechanism that I use to call this function:

    SetTabOrder(TRUE, oNewButton1.GetSafeHwnd(),
    ::GetDlgItem(this->GetSafeHwnd(), IDOK),
    ::GetDlgItem(this->GetSafeHwnd(), IDCANCEL),
    ::GetDlgItem(this->GetSafeHwnd(), ID_APPLY_NOW),
    ::GetDlgItem(this->GetSafeHwnd(), IDHELP),
    oNewButton2.GetSafeHwnd(),
    oNewButton3.GetSafeHwnd(),
    oNewButton4.GetSafeHwnd(),
    oNewButton5.GetSafeHwnd(),
    NULL);

    Reply
  • What about Tab order?

    Posted by Legacy on 03/10/1999 12:00am

    Originally posted by: Bill Foust

    Thats a really nice bit of work there. It helped out a lot.

    The one problem I see now is the tab order. It would be nice to be able to tab through the fields of the property page, then to the new button, then to OK, Cancel etc. Right now, you get the page then OK, Cancel, Tabs, and then the new button.

    Also quick note, do the stuff in OnInitDialog as described AFTER calling the base class OnInitDialog. :) That was the first mistake I made. :)

    Bill

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds