Search code examples
propertysheet

Changing the IDCANCEL button to a IDCLOSE button on a propertysheet, taking localization into account


This might sound like a dumb question. I know that CMFCPropertyPage has a CancelToClose method but I can't find similar for the sheet object.

I basically want the Cancel button to be "Close" at all times and was hoping to do it in the sheet object.

Is the only way to do this by calling CancelToClose in each of the pages?

I read this and now realise it is not want I want anyway.

This is what I want on my sheet:

  1. A custom Preview button.
  2. A Close button.

The preview button will be to the left side of the close button. I have found a tutorial about adding a custom button.

For the Close button I am not sure what to do.

Update

So, at the moment I have:

Sheet

So it has the custom button (where the existing hidden IDOK button is located). And it has the IDCANCEL button. But I want the button to be "Close".

I know I can use SetWindowText but I am thinking about localization so I wondered what the best way is.


Solution

  • This is how I ended up resolving this issue. I now call this code from CMFCPropertySheet::OnInitDialog():

    void CVisitsRotaPropertySheet::SetupButtons()
    {
        CRect rctOK, rctCancel;
        CString strButtonText;
    
        // Get the position if the IDOK button
        GetDlgItem(IDOK)->GetWindowRect(rctOK);
        ScreenToClient(rctOK);
    
        // Get the position of the IDCANCEL button
        GetDlgItem(IDCANCEL)->GetWindowRect(rctCancel);
        ScreenToClient(rctCancel);
    
        // Hide the IDCANCEL button
        GetDlgItem(IDCANCEL)->ShowWindow(SW_HIDE);
    
        // Move the IDOK button to be in the same place as the IDCANCEL button
        GetDlgItem(IDOK)->MoveWindow(rctCancel);
    
        // Create the PREVIEW button in the original location of the IDOK button
        ENSURE(strButtonText.LoadString(IDS_STR_PREVIEW));
        m_btnPreview.Create(strButtonText,
            BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, rctOK, this, IDC_BUTTON_PREVIEW);
        m_btnPreview.SetFont(GetFont());
    }
    

    The above code adjusts the buttons as I desire. Then, in my CMFCPropertyPage::OnInitDialog() handlers I call CancelToClose().

    The result:

    With custom buttons