Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my MFC dialogue based application one situation is that first I open model dialog and click event of one of button of this dialog,it open main dialog which contains Initinstance and OnInitDialog method in it,so my question is whenever I close this first dialog onCancel event occur and in that event I called PostQuitMessage(),it closes application but slightly show next window and then close.I think this method is wrong!I any another method to do that?please suggest!
Thanks in advance!!
Posted

If you need a start dialog that is shown before your main dialog, just show this dialog from within InitInstance() and return upon Cancel:
BOOL CMyApp::InitInstance()
{
    // Common initialization

    // Show start dialog
    CStartDlg StartDlg;
    if (StartDlg.DoModal() == IDCANCEL)
        return FALSE;

    // Show the main dialog
    CMainDlg MainDlg;
    m_pMainWnd = &MainDlg;
    MainDlg.DoModal();
    return FALSE;
}

If you need to pass some data between the start dialog and your application, you can pass a pointer to your CMyApp class.

[EDIT: Added example to show how data can be passed]
BOOL CMyApp::InitInstance()
{
    // Common initialization

    // Create the main dialog instance and assign it to m_pMainWnd
    CMainDlg MainDlg;
    m_pMainWnd = &MainDlg;

    // Show start dialog
    CStartDlg StartDlg;
    if (StartDlg.DoModal() == IDCANCEL)
        return FALSE;

    // Show the main dialog
    MainDlg.DoModal();
    return FALSE;
}

void CStartDlg::OnOK()
{
    // Pass data to main dialog members here.
    // Note that the dialog window itself is not created yet.
    // So access only CMainDlg specific member variables 
    //  (no CWnd base class members).
    CMainDlg* pMainDlg = static_cast<CMainDlg*>(AfxGetApp()->m_pMainWnd);
//  pMainDlg->SomeVar = SomeValue;
    CDialog::OnOK();
}
 
Share this answer
 
v2
Comments
Venkat Raghvan 2-Aug-13 3:18am    
Thanks!!,but it happened same!nothing change in it!
Jochen Arndt 2-Aug-13 4:41am    
With my solution, CStartDlg is a normal (modal) dialog that must not call PostQuitMessage(). When the start dialog is closed using the Cancel button (or the close button on top right), the main dialog is not shown and the application quits. When using the OK button, the main dialog is created and shown.

If you need a different behaviour, you should try to implement it based on my solution. Using PostQuitMessage() should be generally avoided.

To get a better answer you may explain in detail what excatly should happen upon starting the app; e.g.:
- Show start dialog
- Enter some data
- Don't start app if start dialog closed by Cancel or close button
- Start app if start dialog closed by OK
- Get data from start dialog
Venkat Raghvan 2-Aug-13 5:10am    
Yes you are right! with that seq-
Show start dialog
- Enter some data
- Don't start app if start dialog closed by Cancel or close button
- Start app if start dialog closed by OK
- Get data from start dialog
Jochen Arndt 2-Aug-13 5:26am    
See my updated solution.
Your design sounds wrong to me; why do you need a dialog to open your main dialog? A dialog application should only need a single dialog which opens automatically when the application starts. It may use other child dialogs during its lifetime but that is all that would be necessary.
 
Share this answer
 
Comments
Venkat Raghvan 2-Aug-13 3:18am    
Sorry!but it is necessary for project purpose!
Richard MacCutchan 2-Aug-13 3:25am    
Then you need to understand and live with the consequences.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900