Solved

modeless & modal

Posted on 1998-09-10
2
381 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now