Solved

modeless & modal

Posted on 1998-09-10
2
384 Views
Last Modified: 2013-11-20
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!
0
Comment
Question by:sunraj
2 Comments
 
LVL 1

Expert Comment

by:Bridge
ID: 1322029
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
 
LVL 1

Accepted Solution

by:
plebel earned 50 total points
ID: 1322030
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 87
Tab names are off by one 5 54
fizzArray  challenge 1 96
fizzArray2 challenge 1 91
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question