1

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?

Himanshu
  • 4,327
  • 16
  • 31
  • 39
Nikita Rana
  • 41
  • 1
  • 6
  • **Assert message** should help you to understand what problem is. In general don't _destroy_ them, just hide. – Adriano Repetti Mar 30 '15 at 11:06
  • My first action was to destroy them. I realized that the DestroyWindow() would have caused some problem but then, I commented those calls and tried running my application using ShowWindow(SW_HIDE) still, I am met with the same Debug Assertion in Debug mode. – Nikita Rana Mar 30 '15 at 11:08
  • What's exact error message? Together with line numbers (in same message) it should highlight what's wrong. – Adriano Repetti Mar 30 '15 at 11:09
  • The Debug Assertion Failed error message box contains the following Program: (path of my exe) File: f:\dd\vctool\vc7libs\ship\atlmfc\include\afxcmn.inl Line: 268 – Nikita Rana Mar 30 '15 at 11:12
  • LOL yes, I have MFC source code but as you can see there are many ASSERT in OnInitDialog(). Which one fails? – Adriano Repetti Mar 30 '15 at 11:14
  • How do I find out which one failed? Does the line number 268 indicate the line number in the MFC Source code? – Nikita Rana Mar 30 '15 at 11:15
  • LRESULT CWnd::Default() { // call DefWindowProc with the last message _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData(); return DefWindowProc(pThreadState->m_lastSentMsg.message, pThreadState->m_lastSentMsg.wParam, pThreadState->m_lastSentMsg.lParam); } Here is where the assertion fails. – Nikita Rana Mar 30 '15 at 12:41
  • Isn't a form designed by you + CMFCTabCtrl a more adequate solution for your interface? – sergiol Jul 24 '15 at 00:46

2 Answers2

-1

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.

Santosh Dhanawade
  • 1,756
  • 14
  • 29
-2
  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.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • Any way to undo the damage caused by my call to DestroyWindow() because after commenting the call to DestroyWindow() and then hiding the window, I still get the same error message. Could DestroyWindow() have tampered with the default settings of a Property Sheet? – Nikita Rana Mar 30 '15 at 11:16
  • Run the application in Debug mode (F5) and you'll see exact line where it ASSERTs in Debugger. – Andrew Komiagin Mar 30 '15 at 11:19
  • You also need to DISABLE any hidden buttons. So in addition to calling `ShowWindow(SW_HIDE)`, you need to call `EnableWindow(false)` on the CWnd, otherwise buttons will still be "active" via tabbing and button text "&" accelerator key strokes. – franji1 Mar 30 '15 at 12:36
  • The built in flags only work for Apply and Help buttons. Will I have to recreate my project all over again? – Nikita Rana Mar 30 '15 at 12:39
  • No, you don't need to recreate your project. Have you been able to identify the line where it ASSERTs? – Andrew Komiagin Mar 30 '15 at 12:41
  • LRESULT CWnd::Default() { // call DefWindowProc with the last message _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData(); return DefWindowProc(pThreadState->m_lastSentMsg.message, pThreadState->m_lastSentMsg.wParam, pThreadState->m_lastSentMsg.lParam); } – Nikita Rana Mar 30 '15 at 12:44
  • Is your propSheet located in DLL? Are you using any custom controls? Are there any threads updating GUI? – Andrew Komiagin Mar 30 '15 at 12:53
  • I apologize for the late answer but after creating another identical project bit by bit i realized I hadn't called the CPropertyPage::OnInitDialog() function in one of my classes. It works perfectly fine now. Thank you so much for your time. – Nikita Rana Apr 15 '15 at 08:36