Help:How to create multiple dialog boxes

  • 13 years ago

    Can anybody help me please. I am learning Visual C++ 6.0 and developing MFC AppWizard applications.


    I try to add a user deifined diaolog box window to my application.  I name it as CMsgDlg and its base class is set to CDialog. When I create the user defined window by calling CDialog::DoModal() method, I seem to have 2 choices to close the window.  That is, in a function of the user-defined window, I could call either OnOK() or EndDialog().  I am not quite sure what they do! Do I call OnOK() to make the window invisible and call EndDialog() to destroy the window? The Help manual is not clear about  it! It seems to suggest that, for a user-defined modal window created by CDialog::DoModal(), both OnOK() and EndDialog() make the window invisible or both of them destroy the window!!  I am confused. Could anyone tell me (1) how to make the user-defined modal window just invisible, & (2) how to destroy it!!  The manual seems to suggest that, for a window created by CDiaolog::DoModal(), DestroyWIndow() method is not used at all! (c.f. Can I insist on using CDialog::DoModal() please).



    I have added the following 5 lines in my C++ program:

       char *pt;

       const char *cpt;

       CString Msg = "hello";

       cpt = (const char *)Msg; //This is OK!

        pt = (char *)Msg; //The compiler does not take this!!

    How come a conversion from CString to (const char *) is fine but from CString to (char *) is not?


    I look forward to hearing from you guys.


    Chong Kim

  • 13 years ago


    'OnOK()' called when the user clicks the OK button (the button with an ID of IDOK). It validates the dialog_box data and updates the appropriate variables in your application ( like calling UpdateData(TRUE) ); then destroys the window (by calling EndDialog()).

    But 'EndDialog()' destroys(terminates) a modal dialog box(without saving data). When you call 'OnOK()' or 'EndDialog()' for a modeless dialog_box, it makes the dialog box invisible but doesn't destroy it. Then you can call 'DestroyWindow()' to destroy the dialog_box.

    If you want to hide(invisible) a window(or a control), just call 'ShowWindow(SW_HIDE)', and to show that again, call 'ShowWindow(SW_SHOW)'.



    You can't convert form 'CString' to 'char *', because the conversion is just for 'const char *'. When you convert from a CString to a 'const char*', it's safe until the CString is not changed, but when it's changed, the pointer maybe become invalid.

    I recommend to use the following way (that works in VC++6 but not in VC++2005), since it copies the content of CString to 'char *', and you can change 'char *', also it's valid till you delete the allocated memory (delete[] pt).

    CString Msg ("hello");
    int size = (Msg.GetLength() + 1);
    char *pt = new char[size];
    strcpy( pt, Msg );

  • 13 years ago

    Dear Mohammad Rastkar

    Many thanks for your reply to my post regarding Multiple Windows in Visual C++ 6.0.  I have got a question for you though.  You say that EndDialog() destroys a modal dialog box.  Are you sure of it?  I have done some experiment about it with DestroyWindow() as follows:


      if (DestroyWindow()){MessageBox("DestroyWindow() succeeded");};

    I have found that here DestroyWindow() returns TRUE!!  How could it return TRUE if the dialog box had already been destroyed?  If you add another DestroyWindow() after the first one, you will notice that this time the second one will return FALSE!!


    I look forward to hearing from you.


    Best regards

    Chong Kim

  • 13 years ago

    My reference is MSDN, and also from there : "EndDialog does not destroy the dialog box immediately. Instead, it sets a flag and allows the dialog box procedure to return control to the system", then I think when you call 'EndDialog', and since the dialog is not destroyed immediately, calling 'DestroyWindow' will do something, but I'm sure (as MSDN says) that 'EndDialog' will destroy the window, and I think calling 'DestroyWindow' after that, is not necessary and maybe harmful! (I also got a RunTime error, when just called 'DestroyWindow' to close the dialog).

    Also consider the following (which is another proof that 'EndDialog' is enough!) :

    // VS2005

    CMyDlg dlg;
    dlg.DoModal(); // closed by just calling 'EndDialog'
    int res = dlg.DestroyWindow();
    CString msg;
    msg.Format( L"%d", (int)res );
    MessageBox( msg ); // will show '0'

  • 13 years ago

    Dear Mohammad

    Thanks again for your reply to my question!!  Would you be kind enough to let me know where in MSDN you got your reference!!  By the way, is it allowed to call a method of a child window's class from a function in its parent window (i.e. dlg.DestroyWindow() in your example which caused a run time crash)? I thought that such a call  to such a method would cause an unpredictable result!! If you do not mind, I will do some experiment myself with your advice.  I hope that you do not mind me coming back to it later.  Many thanks again!


    Best regards



  • 13 years ago

    chong (my friend! Smiley Face ),

    =>   My references was from 'MSDN 2005', that I read in help of functions : 'DestroyWindow' , 'EndDialog' , 'OnOK' and the like.

    =>   I got the runtime error when I called 'DestroyWindow()' in a function of child dialog (like the click_func for a button in the child dialog) . For the code snippet in my previous post, I got no errors (and I don't think it has a problem).

    =>   I'm happy to have conversations with you! Smiley Face, and it's very nice that you dig into functions! I just think that 'EndDialog()' will do that all, but maybe not, and I want you, to share your experience with me and others Wink.

  • 13 years ago

    Dear MohammadSmiley Face

    It is nice to be a friendSmiley Face!!  Thanks!! I will share my work with you and others probaly in the next post!!  At the moment, I am a bit stuck and can not summerize what I have been doing for the last few weeks. In the meantime you could help me out by answering a few questions of mine!!


    => I have done some experiments and it looks quite clear that EndDialog() is enough to destroy a modal child dialog box.  It appears that, each time DoModal() is called to create a child window, OnInitDialog() of the child window gets executed.  I have some expeirience with Vsiaul Basic and VB suggests that it means that the child window is created from scratch.


    => There is something which puzzles me.  Is it allowed to call a method/function of a child dialog box window class from a user function (i.e. a command button function) of its parent window (i.e. m_dlgCustomWindow.DestroyWindow() where m_dlgCustomWindow is a member variable declared in the parent window)?  I thought that such a call could confuse the system!!  I defintely need an answer to this question, Mohammad. I am stuck!Sad  If you are not sure what I am asking here, I will rephrase this question in the next post.


    => You said that I could use the ShowWindow() method/function to show/hide a child window!!  Can I use it for a modal dialog box?  I have done some work on this but the result has been very confusing!! I put it in a command button function in a child window followed by return statement. It causes an endless loop! 


    I look forward to hearing from you.  Many thanks for your help.

    Best regards


  • 13 years ago

    => First, I should say I'm testing my codes in VC++2005 (in Vista), and I haven't installed VC++6.

    => Calling a function of a dialog, "is just calling a function of a dialog's class instance", like any other classes (if you have access to a class(for static funcs) or an instance of that (for call with that object), you can call methods of that without problem). Here, the difference is not in 'syntax' it's in 'Run_Time' behavior and what the functions do, that the question is : "can we call a method after another one ?", but where you are calling, itself, is not important.

    => When you create and show a modal dialog_box (instead of a modeless one), you want to force user to respond to the modal_box then he can work with the main window. I tested 'ShowWindow( SW_HIDE )' : when I call it in a modal_box (for EX, in a button's click_func), modal_box will disappear and just the main_window remains on the screen (you should minimize all other windows to see that). But I can't interact with the main_win, since my modal_box is there but hidden! you can show that again with ShowWindow( SW_SHOW ), for an example app :

    • Assume 'CMyDlg' is a class for a dialog box (except your main_window(which its class is 'CMainDlg')) :
    // click_event for 'Test' button

    void CMyDlg::OnTestClickedBtn() // click_func for a button in the modal_box (CMyDlg)


    ShowWindow( SW_HIDE );

    Sleep( 3000 ); // wait 3000 milliseconds (3 seconds)

    ShowWindow( SW_SHOW );



    • and you can show the modal_box from your main_win (as you know) :
    // click_event for 'ShowModal' button

    void CMainDlg::OnShowModalClickedBtn() // click_func for a button in the main_win (CMainDlg)


    CMyDlg dlg;



    When you run the app, and click the 'ShowModal' button, your modal_box will appear. Then when you click on the 'Test' button, the modal_box will disappear and after 3 seconds it will appear again. During those 3 seconds, you can't interact with main_win. It's the nature of a modal dialog_box, and it's not in a loop.

  • 12 years ago

    Hi Mohammad

    Many thanks for your help so far. I have just posted another question titled "Help:C++ streams to access a binary file", which might interest you.  If you can have a look and then can make suggesiotns as you have done in the past, I will be grateful!!

    All the best


Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow