modeless dialog box - Programmer

This is a discussion on modeless dialog box - Programmer ; I have tried to implement a simple modeless dialog box. In the calling function, I want to go through a loop for as long as the modeless dialog box is still up. The modeless dialog box has a member variable ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: modeless dialog box

  1. modeless dialog box

    I have tried to implement a simple modeless dialog box. In the calling
    function, I want to go through a loop for as long as the modeless
    dialog box is still up. The modeless dialog box has a member variable
    m_Active that is set to false on creation, true on OnInit, and back to
    false on exit. The modeless dialog box is created just fine, but both
    it and the parent window are frozen and I have to kill the app.

    Here's the code that I call it:

    void CMyMainDialog::OnModeless()
    {
    CModelessDialog *dlg;
    dlg = new CModelessDialog;

    if(dlg != NULL)
    {

    BOOL ret = dlg->Create(IDD_MODELESS_DIALOG, this);
    if(!ret) //Create failed.
    AfxMessageBox("Error creating Dialog");
    dlg->ShowWindow(SW_SHOW);

    int cont = 1;

    while(cont)
    {
    if (dlg->m_Active == false)
    cont = 0;
    }
    delete dlg;
    }
    }


    and here's the only changes I made (from the class wizard code) to
    CModelessDialog:

    BOOL Create(UINT nID, CWnd * pWnd)
    {
    return CDialog::Create(nID,pWnd);
    }

    BOOL CModelessDialog::OnInitDialog()
    {
    m_Active = true;
    return true;
    }

    void CModelessDialog::OnAbort()
    {
    this->m_Active = false;
    this->OnCancel();
    }

  2. Re: modeless dialog box

    Bob Roberts wrote:

    > I have tried to implement a simple modeless dialog box. In the calling
    > function, I want to go through a loop for as long as the modeless
    > dialog box is still up. The modeless dialog box has a member variable
    > m_Active that is set to false on creation, true on OnInit, and back to
    > false on exit. The modeless dialog box is created just fine, but both
    > it and the parent window are frozen and I have to kill the app.
    >
    > Here's the code that I call it:
    >
    > void CMyMainDialog::OnModeless()
    > {
    > CModelessDialog *dlg;
    > dlg = new CModelessDialog;
    >
    > if(dlg != NULL)
    > {
    >
    > BOOL ret = dlg->Create(IDD_MODELESS_DIALOG, this);
    > if(!ret) //Create failed.
    > AfxMessageBox("Error creating Dialog");
    > dlg->ShowWindow(SW_SHOW);
    >
    > int cont = 1;
    >
    > while(cont)
    > {
    > if (dlg->m_Active == false)
    > cont = 0;
    > }
    > delete dlg;
    > }
    > }


    This is not a code problem but a design problem. A GUI program must
    respond to messages or it appears to be frozen. A single-threaded
    program does not respond to messages while it is looping. So you have
    to find another way to do whatever you are trying to do. What are you
    trying to do? I.e. what is the loop for?

    --
    Scott McPhillips [VC++ MVP]


  3. Re: modeless dialog box

    Why do I even want a modeless dialog box?

    Well, with the example that I showed, a modal dialog would work, but
    what I am really wanting to do is a little more complicated. I want
    to start the loop without any dialog. But if something goes wrong, I
    want to give the user the option of exiting out of the loop. But
    while the dialog giving the user that option, the code keeps going
    through the loop. If the user selects OK from the dialog, then the
    loop is exited prematurely. If the loop is exited normally, then it
    goes ahead and kills the dialog.


    "Scott McPhillips [MVP]" wrote in message news:...
    > Bob Roberts wrote:
    >
    > > I have tried to implement a simple modeless dialog box. In the calling
    > > function, I want to go through a loop for as long as the modeless
    > > dialog box is still up. The modeless dialog box has a member variable
    > > m_Active that is set to false on creation, true on OnInit, and back to
    > > false on exit. The modeless dialog box is created just fine, but both
    > > it and the parent window are frozen and I have to kill the app.
    > >
    > > Here's the code that I call it:
    > >
    > > void CMyMainDialog::OnModeless()
    > > {
    > > CModelessDialog *dlg;
    > > dlg = new CModelessDialog;
    > >
    > > if(dlg != NULL)
    > > {
    > >
    > > BOOL ret = dlg->Create(IDD_MODELESS_DIALOG, this);
    > > if(!ret) //Create failed.
    > > AfxMessageBox("Error creating Dialog");
    > > dlg->ShowWindow(SW_SHOW);
    > >
    > > int cont = 1;
    > >
    > > while(cont)
    > > {
    > > if (dlg->m_Active == false)
    > > cont = 0;
    > > }
    > > delete dlg;
    > > }
    > > }

    >
    > This is not a code problem but a design problem. A GUI program must
    > respond to messages or it appears to be frozen. A single-threaded
    > program does not respond to messages while it is looping. So you have
    > to find another way to do whatever you are trying to do. What are you
    > trying to do? I.e. what is the loop for?


  4. Re: modeless dialog box

    You can't do what you are doing there at the end of the function. Once you
    create the dialog then you have to let the system handle messages. But you
    are not allowing that by keeping the program in the function.
    You can do something to get around the problem. that while loop that you
    have can also double as a message loop.

    > while(cont)
    > {

    while (PeekMessage(&Msg))
    {
    if (!PreTranslateMessage(&Msg))
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }
    > if (dlg->m_Active == false)
    > cont = 0;
    > }


    But wouldn't a modal dialog box be better here.
    If not, I would do things a little different.
    Make CModelessDialog *dlg; a memeber of the dialog box that creates it. And
    let the modeless dialog box signal the creator dialog box that it is done,
    and needs to be deleted, or it can even delete itself.

    Ali

    "Bob Roberts" wrote in message
    news:c4e6b17d.0310062005.9bfeb53@posting.google.co m...
    > I have tried to implement a simple modeless dialog box. In the calling
    > function, I want to go through a loop for as long as the modeless
    > dialog box is still up. The modeless dialog box has a member variable
    > m_Active that is set to false on creation, true on OnInit, and back to
    > false on exit. The modeless dialog box is created just fine, but both
    > it and the parent window are frozen and I have to kill the app.
    >
    > Here's the code that I call it:
    >
    > void CMyMainDialog::OnModeless()
    > {
    > CModelessDialog *dlg;
    > dlg = new CModelessDialog;
    >
    > if(dlg != NULL)
    > {
    >
    > BOOL ret = dlg->Create(IDD_MODELESS_DIALOG, this);
    > if(!ret) //Create failed.
    > AfxMessageBox("Error creating Dialog");
    > dlg->ShowWindow(SW_SHOW);
    >
    > int cont = 1;
    >
    > while(cont)
    > {
    > if (dlg->m_Active == false)
    > cont = 0;
    > }
    > delete dlg;
    > }
    > }
    >
    >
    > and here's the only changes I made (from the class wizard code) to
    > CModelessDialog:
    >
    > BOOL Create(UINT nID, CWnd * pWnd)
    > {
    > return CDialog::Create(nID,pWnd);
    > }
    >
    > BOOL CModelessDialog::OnInitDialog()
    > {
    > m_Active = true;
    > return true;
    > }
    >
    > void CModelessDialog::OnAbort()
    > {
    > this->m_Active = false;
    > this->OnCancel();
    > }




+ Reply to Thread