Overriding the default buttons on CPropertySheets

How to override the default buttons on a properysheet:

What you need to do is override the OnSetDefID in CPropertySheet. So you would add the following to your message map in your derived class:

ON_MESSAGE(DM_SETDEFID, OnSetDefID)
And declare OnSetDefID which would look something like this:
//CMG: Override the CPropertySheet default button and replace with own.
LRESULT CTSIDlg::OnSetDefID(WPARAM wParam, LPARAM lParam)
{
LRESULT lReturn;
//The current default ID, in case you want to access it:
int nIDOldDef = LOWORD(DefWindowProc(DM_GETDEFID, wParam, lParam));

  switch (GetActiveIndex())
  {
   case 1:
    lReturn = DefWindowProc(DM_SETDEFID, YOUR_ID_HERE, lParam);
    return lReturn;
    break;
  }
  return Default(); 
}

Note in the DefWindowProc you can set the default ID for your own control.

One thing I didn't figure out is how to erase the dark highlight from the OK button. But the ID you set will also have the dark highlight.

If you were using the Wizard you might do this differently. If you want a really nice piece of code I could send it later as in Wizard mode the FINISH button and NEXT button would need to be default sometimes. But if you just have a regular PropertySheet dialog and want to override the default button this will do the job.

Cheers, Chris

Last updated: 22 July 1998



Comments

  • Adding to Christopher's solution

    Posted by Legacy on 06/25/2003 12:00am

    Originally posted by: Sreekanth Kollu

    void CSheetDerived::OnOK()
    {
    if (IDYES == MessageBox(_T("Do you really want to make this update?"),
    _T("Caption"),
    MB_YESNO))
    {
    PressButton(PSBTN_OK);
    // Functionality that can be added..
    EndDialog(IDOK);

    }
    }

    EndDialog(IDOK) must be there if we are handling OK button..Because the property sheet DoModal() method wont get IDOK return value. Thanks Christopher, ur soln helped me..

    Reply
  • your code doesn't work well

    Posted by Legacy on 01/28/2000 12:00am

    Originally posted by: Nguyen Cong Thuan

    follow your guide i've been build an app using wizard as following:
    LRESULT StsSheet::OnSetDefID(WPARAM wParam, LPARAM lParam)
    {
    LRESULT lReturn;
    int nIDOldDef = LOWORD(DefWindowProc(DM_GETDEFID, wParam, lParam));
    if(nIDOldDef==ID_WIZNEXT)
    return DefWindowProc(DM_SETDEFID, ID_PG_NEXT, lParam);
    return Default();
    }

    //on pages.h
    virtual LRESULT OnButtonNext();

    //on pages.cpp

    BEGIN_MESSAGE_MAP(Page1, CPropertyPage)
    //{{AFX_MSG_MAP(Page1)
    ON_MESSAGE(ID_PG_NEXT, OnButtonNext)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    LRESULT Page1::OnButtonNext()
    {
    //my code here
    }

    so i don't want to change to next page when user press next button but do another job. In fact function OnButtonNext() has never been called and still change to next page. so that code doesn't work.
    please help me to fix this problem. Thank you very much!


    Reply
  • Controlling the exit of a CPropertySheet

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

    Originally posted by: Christopher Duncan

    "All I wanted to do" was catch the OK button for the sheet as a whole and throw up a yes/no message box asking the user if they really wanted to make this update, but there is no OnOK() for property sheets, only the pages.
    
    

    Clicking OK on the sheet first invokes OnApply() for the current page. If that returns TRUE, then OnOK() for the current page is called, and the sheet exits (OnApply is *always* called before OnOK). OnOK() for the page returns void and gives you no control - by the time you reach it, you're outta there.

    Using Kirk Stowell's method of overriding IDOK for the derived property sheet, (http://codeguru.developer.com/mfc/comments/4863.shtml) the following snippet provides this functionality.

    Thanks, Kirk.

    // The sheet will only do the normal OK button stuff if the PressButton() call
    // is made. Otherwise the message box just returns to the sheet.
    void CSheetDerived::OnOK()
    {
    if (IDYES == MessageBox(_T("Do you really want to make this update?"),
    _T("Caption"),
    MB_YESNO))
    {
    PressButton(PSBTN_OK);
    }
    }

    Reply
  • Overriding the default state border

    Posted by Legacy on 02/15/1999 12:00am

    Originally posted by: Philippe Lhoste

    From the DM_SETDEFID help topic:

    Using the DM_SETDEFID message can result in more than one button appearing to have the default push button state. When the system brings up a dialog, it draws the first push button in the dialog template with the default state border. Sending a DM_SETDEFID message to change the default button will not always remove the default state border from the first push button. In these cases, the application should send a BM_SETSTYLE message to change the first push button border style.

    I didn't tried it yet...

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

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds