where is my Modeless Dialog?

hi, I just begin to learn MFC with VC6.0. today when I read the <programming visual C++ >5th and try to learn the Modeless Dialog ,I come across a problem, when I get the exe and double click it, i run ,but i can`t find my modeless dialog , is there any thing different between Modal Dialog and Modeless Dialog when they are created or displayed,for example , the create() ,OnInitdialog()

any reply will be appreciated




Answer this question

where is my Modeless Dialog?

  • Mortsdeh

    That's not true. Who told you that Here is the counter-example:

    CMyDialog *pDlg=new CMyDialog;//created on heap

    pDlg->DoModal();

    and

    CMyDialog Dlg;//created on stack

    if(Dlg.Create(IDD_MYDIALOG))

    {

    Dlg.ShowWindo(SW_SHOW);
    }



  • Bill Henning

    you are right.

    But isn't it logical to create modelless dialog on heap

    Have you tried your code for modelless dilaog

    And about modal dialg, DoModal will return only if user take any action.

    So scope of that object should be local.

    I was talking about logical things. Any way MFC doesn't have this restriction.


  • Pete_M

    Modelless dialog is created using code,

    CMyDialog *pDlg=new CMyDialog;//created on heap

    pDlg->Create(IDD_MYDIALOG);

    pDlg->ShowWindo(SW_SHOW);

    Modal dialog is created on stack,

    CMyDialog Dlg;//created on stack

    pDlg.Domodal();


  • pkv

    Sorry, but you are making one more error: the term is not "modelless" but "modeless".

    But isn't it logical to create modelless dialog on heap

    Why Why does it have to be on the heap



  • sharonshauli

    The problem with the code is in InitInstance. A tipical InitInstance() for a dialog based app should look like this:

    BOOL CDlgTestApp::InitInstance()

    {

    // InitCommonControlsEx() is required on Windows XP if an application

    // manifest specifies use of ComCtl32.dll version 6 or later to enable

    // visual styles. Otherwise, any window creation will fail.

    INITCOMMONCONTROLSEX InitCtrls;

    InitCtrls.dwSize = sizeof(InitCtrls);

    // Set this to include all the common control classes you want to use

    // in your application.

    InitCtrls.dwICC = ICC_WIN95_CLASSES;

    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();

    // Standard initialization

    // If you are not using these features and wish to reduce the size

    // of your final executable, you should remove from the following

    // the specific initialization routines you do not need

    // Change the registry key under which our settings are stored

    // TODO: You should modify this string to be something appropriate

    // such as the name of your company or organization

    SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    CDlgTestDlg dlg;

    m_pMainWnd = &dlg;

    INT_PTR nResponse = dlg.DoModal();

    if (nResponse == IDOK)

    {

    // TODO: Place code here to handle when the dialog is

    // dismissed with OK

    }

    else if (nResponse == IDCANCEL)

    {

    // TODO: Place code here to handle when the dialog is

    // dismissed with Cancel

    }

    // Since the dialog has been closed, return FALSE so that we exit the

    // application, rather than start the application's message pump.

    return FALSE;

    }

    I would bet that yours looks like this:

    CDlgTestDlg dlg;

    m_pMainWnd = &dlg;

    dlg.Create(IDD_DLGTEST_DIALOG);

    // Since the dialog has been closed, return FALSE so that we exit the

    // application, rather than start the application's message pump.

    return FALSE;

    If you run step by step you may notice that the dialog is actually displayed, before the return statement. The problem is the you are creating a modeless dialog through a local variable. That means as soon as the return statement is reached the object is destroyed, and the app ends.

    If you make dlg a member of the class and return TRUE instead of FALSE, then your dialog will be shown.



  • dafan21

    Show us the code in InitInstance().

  • Anarchy

    I bet that Laozeng forget the show()

  • Jnox

    CMyDialog * m_pDlg;

    bool CMyDialog::Create ()
    {
    return CDialog::Create(CEx::IDD);
    }

    if(m_pDlg->GetSafeHwnd ()==0){
    m_pDlg->Create ();
    m_pDlg->ShowWindow(SW_SHOW);
    }



  • tomerlev

    the term is not "modelless" but "modeless".

    thanks for correcting.

    Why Why does it have to be on the heap

    To avoid problem, possibly faced by OP.

    You've given suggestion to OP to make it member variable, that could be one solution.

    But why you wanted an object in memory , when its only needed after its invokation and not needed when closed by user.

    Let me describe,

    CMyApp : CWinApp

    {

    // probalbly as this is dialog based application,could be Ok

    CMyDialogn m_dlg;

    public:

    };

    And in InitInstance

    // till this point m_dlg is unnecessarily in memory

    m_dlg.Create(...);//modeless

    m_dlg.ShowWindow(SW_SHOW);

    and after its destruction its still in memory.

    Isn't this more suitable

    CMyDialog *pDlg;

    pDlg->Create(..);

    pDlg->ShowWindow();

    and CMyDialog will delete itself is PostNcDestroy()

    CMyDialg::PostNcDestroy()

    {

    CDialog::PostNcSetroy();

    //tell parent I've been closed

    delete this;//thats all

    }

    Isn't it logical to create modeless dialog on heap  

     


  • Emadkb

    First, you should check the returned value of Create() to see whether the creation succeeded or failed. Second, the ShowWindow call is needed only if the window does not have the WS_VISIBLE style, which, I would bet, is not the case here (by default this flag is set, and I don't think it was manually unset in the properties window).

  • where is my Modeless Dialog?