modeless & modal

hai,

How to implement modeless dialog box ?
why it is neccessary to create a pointer to the dialog and why should the memmory be allocated for a modeless dialog box.
please explain in detail.
I'll double the points for suitable answers
adv thanks!
sunrajAsked:
Who is Participating?
 
plebelCommented:
A modeless dialog is not blocking, so it can not be a local static variable.  A modeles dialog need a DYNAMIC implementation so it will be free to run in paralell of the window that created it, and must be able to destroy itself and automaticaly free its memory space.  

When you use the DoModal function, the window or the function which call the DoModal function is blocked until the dialog is closed.  

when you use the Create function, it create an autonom dialog box and return to the calling window.  The calling window can then destroy the dialog in any time or the dialog can destroy itself latter but both (diallog an caller windows) are free to run in the same time.  So a modeless dialog can be use as a floating tool box.

But you must pay attention, if both (caller window and dialog) have the possibility to destroy the dialog, you must implement a mecanism to let the caller know if the dialog is alive.  In the same way, the dialog that destroy itself must delete itself.

here is an example

I use "BOOL OpenDlg(UINT Idd)" to open the dialog as medeless

I use "void CloseDlg()" to close the dialog from outside

I use void "CAbout::OnOK()" to close the dialog from INSIDE

As you can see, i use the pAbout to know if the object is valid and i set it to null when the object is deleted.

static CAbout *pAbout = NULL;  

// i define the pointer as static because it is implemented on // the dialog box file and not on the caller window file.

BOOL OpenDlg(UINT Idd)
{
      if (!pAbout)
      {
            pAbout = new CAbout(Idd,TRUE);
            pAbout->Create(Idd);
            pAbout->ShowWindow(SW_SHOW);
            pAbout->UpdateWindow();
      }
      return TRUE;
}

void CloseDlg()
{
      if (pAbout)
      {
            pAbout->DestroyWindow();
            delete pAbout;

            /////////////////
            // be sure you set the pointer as NULL

            pAbout = NULL;
      }
}


/////////////////////////////////////////////////////////////////////////////
// CAbout dialog


CAbout::CAbout(UINT Idd,CWnd* pParent /*=NULL*/)
      : CDialog(Idd, pParent), CVersionInfo()
{
      //{{AFX_DATA_INIT(CAbout)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT


}


void CAbout::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CAbout)
            // NOTE: the ClassWizard will add DDX and DDV calls here
      //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAbout, CDialog)
      //{{AFX_MSG_MAP(CAbout)
      ON_WM_TIMER()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//////////////////////////////
// CAbout message handlers

void CAbout::OnOK()
{
      if (pAbout)
      {
            pAbout->DestroyWindow();
            delete pAbout;

            /////////////////
            // be sure you set the pointer as NULL

            pAbout = NULL;
      }
}

Enjoy it!

Pierre
0
 
BridgeCommented:
ask yourself, when and where is the dialog destroyed ?
If its a MODAL it can be destroyed after the DoModal call. If its a MODELESS it could be anywhere.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.