Overriding the default buttons on CPropertySheets

WEBINAR: On-demand webcast

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

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

  • want to change the color of default wizard butto

    Posted by Rani Manickam on 06/10/2017 07:54pm

    "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." Hi, I am using wizard button. i want to change the default wizard button background and text color. Please send any sample code if you have. Thank you

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

  • In this era of Big Data, enterprise applications create a large volume of data that may be structured, semi-structured, or unstructured in nature. Application development cycles are also shorter and application availability is a critical requirement. As such, enterprises are forced to look beyond traditional relational databases to NoSQL databases, such as MongoDB and Apache Cassandra, to onboard these next-generation applications. This guide discusses the backup and recovery challenges for distributed and …

  • Are you weighing the benefits of cloud storage versus on-premises storage? If so, the right answer might be to use both — a mix of on-premises and public cloud services with data mobility between the two platforms. Hybrid has piqued interest as more organizations look to the public cloud to augment their on-premises data management. Analyst firm IDC predicts that by 2018, 85% of enterprises will operate in a multi-cloud environment. Check out this easy-to-absorb infographic to explore five reasons to …

Most Popular Programming Stories

More for Developers

RSS Feeds

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