Effect of destroying OK, CANCEL and HELP windows of a porperty sheet

189 Views Asked by At

I wanted to use a CPropertySheet based application for a project and I did not want those default OK, Cancel, Help and Apply buttons that come with a CPropertySheet class. Therefore, I destroyed those windows on OnInitDialog. Here is the code for reference:

BOOLCProductUI::OnInitDialog()
{
    CPropertySheet::OnInitDialog();

    CRect rect;
    CButton *pTempBtn;
    CButton SaveChanges;

    pTempBtn = reinterpret_cast<CButton *>(GetDlgItem(IDHELP));
    if (NULL != pTempBtn)
    {
        pTempBtn->GetWindowRect(&rect);
        pTempBtn->DestroyWindow();
    }

    pTempBtn = reinterpret_cast<CButton *>(GetDlgItem(IDOK));
    if (NULL != pTempBtn)
    {
        pTempBtn->DestroyWindow();
    }

    pTempBtn = reinterpret_cast<CButton *>(GetDlgItem(IDCANCEL));
    if (NULL != pTempBtn)
    {
        pTempBtn->DestroyWindow();
    }

    pTempBtn = reinterpret_cast<CButton *>(GetDlgItem(ID_APPLY_NOW));
    if (NULL != pTempBtn)
    {
        ScreenToClient(&rect);
        pTempBtn->MoveWindow(rect);
        pTempBtn->SetWindowText(_T("Save Changes"));
    }

    UpdateData(FALSE);
    return TRUE;
}

CProductUI is a class of CPropertySheet.
However, when I compile the program using VC++2008 in Debug mode, I get a Debug Assertion Failed error message at the line
"CPropertySheet::OnInitDialog();"
Can anyone please shed some light on why this is happening?

2

There are 2 best solutions below

8
On
  1. You should call ShowWindow (SW_HIDE); instead of DestroyWindow();
  2. Also there is no need to cast CWnd* returned by GetDlgItem() to CButton*.
  3. Please also comment out your CButton SaveChanges; declaration. You don't need it.

You can also use built-in flags to do that:

CMyPropertyPage myPage;
myPage.m_psp.dwFlags &= ~PSP_HASHELP;
myPropertySheet.AddPage(&myPage);

myPropertySheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
myPropertySheet.m_psh.dwFlags &= ~PSH_HASHELP;

IMPORTANT: In general please run your application in Debug mode to see where it ASSERTs.

0
On

Per How to Hide the Apply Button in CPropertySheet. Destroying window is not a proper solution to hide default buttons of property sheet. I would suggest you to use "ShowWindow()". But as you already mentioned your showwindow() also creating problem which is not possible if your calls are correct. Let it be, if your ShowWindow() is not working in "OnInitDialog()" function then better move this function to "OnCreate()". And also if it is not working then please share your whole .H and .CPP file.