Adding a Button to CPropertySheet

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • Mr

    Posted by psingh on 02/17/2016 06:19am

    create your own button in a modeless property sheet:- GetWindowRect(&rect); SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height() + 33, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); Btn_OK.Create(_T("Ok"), BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, CRect(6, rect.Height() - 30, 80, rect.Height() - 10), this, IDC_OTHER_OUT_OK); CWnd* Btn = GetDlgItem(IDC_OTHER_OUT_OK); ASSERT(Btn); Btn-ShowWindow(SW_SHOW); Btn-EnableWindow(TRUE);

    Reply
  • 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
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • The software-defined data center (SDDC) and new trends in cloud and virtualization bring increased agility, automation, and intelligent services and management to all areas of the data center. Businesses can now more easily manage the entire lifecycle of their applications and services via the SDDC. This Aberdeen analyst report examines how a strong foundation in both the cloud and internal data centers is empowering organizations to fully leverage their IT infrastructure and is also preparing them to be able …

  • Many enterprises are working with an IT architecture that's evolved over time. As business needs evolve, IT must decide whether to modernize incrementally, or all at once. Each approach has its benefits and drawbacks. Identity Management is key to modernizing IT; it plays a crucial role in migrating to cloud apps like Office 365 or HR information systems, building web and mobile apps, and opening developer access to business systems. Read how Okta's modern approach to identity management helps business lower …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date