c++mfcpropertysheetcpropertysheet

CPropertySheet only shows for a second


I am trying to add a CPropertySheet with three CPropertyPages to my MFC application. My problem is that the Property sheet only shows for less than a second then closes. When I open a different modal dialog after creating the CPropertySheet, the CPropertySheet stays open and I can use it with no problems. Here is my code:

BOOL CSLIMOptCplusplusApp::InitInstance()
{

CWinApp::InitInstance();
SQLHENV m_1;
EnvGetHandle(m_1);


Login lgn;
lgn.DoModal();




CImageSheet*      imagedlg            = new CImageSheet("Image Capture Dialog" );
CImageDisplay*    pageImageDisplay    = new CImageDisplay;
CImageDimensions* pageImageDimensions = new CImageDimensions;
ListOption*       pageListOption      = new ListOption;

ASSERT( imagedlg );
ASSERT( pageImageDisplay );
ASSERT( pageImageDimensions );  
ASSERT( pageListOption );

imagedlg->AddPage( pageListOption);
imagedlg->AddPage( pageImageDimensions );
imagedlg->AddPage( pageImageDisplay );

imagedlg->Create( NULL,
              -1,
              WS_EX_CONTROLPARENT | WS_EX_TOOLWINDOW ); 

imagedlg->ShowWindow( SW_SHOW );

I think my problem may be at imagedlg->Create( when I use NULL as the first parameter. The tutorial I was following used this in place of the NULL. However, that gives the error:

IntelliSense: argument of type "CSLIMOptCplusplusApp *" is incompatible with parameter of type "CWnd *"

I also tried imagedlg->Create(); and it also only flashes for a moment. I would like my CPropertySheet to stay open until it is closed.


Solution

  • Your problem lies in trying to construct a property sheet within a dialog based application. Actually, your choice of executing everything within InitInstance can be problematic at times.

    For starters, there's no need to create all of your objects on the heap (ie. using 'new'). But, if that's what you want, ok. As for your original problem of the sheet only displaying for a moment, InitInstance is designed to return immediately if not told otherwise. Thus, you see the sheet for an instance. This is due to MFC expecting a valid pointer to the CWinApp class derived member variable called 'm_pMainWnd' (actually, CWinThread::m_pMainWnd). If you want to start a property sheet, or, main dialog from within InitInstance, you need to set that variable to a valid window. Here's a quick sample I wrote:

    CPropertySheet* m_pdlgPropertySheet = new CPropertySheet(_T("Simple PropertySheet"));
        ASSERT(m_pdlgPropertySheet);
    
        // Add three pages to the CPropertySheet object.  Both m_pstylePage,  
        // m_pcolorPage, and m_pshapePage are data members of type  
        // CPropertyPage-derived classes in CView-derived class.
        Page1* m_pstylePage = new Page1;
        m_pstylePage->Construct(IDD_DIALOG1);
        Page2* m_pcolorPage = new Page2;
        m_pcolorPage->Construct(IDD_DIALOG2);
        m_pdlgPropertySheet->AddPage(m_pstylePage);
        m_pdlgPropertySheet->AddPage(m_pcolorPage);
    
        m_pMainWnd = m_pdlgPropertySheet;
        INT_PTR nResponse = m_pdlgPropertySheet->DoModal();
    

    Note the line above DoModal. If you need additional info, take a look at Creating a full application using the CPropertySheet. Lastly, you may want to read up on how MFC starts an application and what is expected.