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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds