Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC

Create a Modeless Dialog Box as Sibling of the App's Main Window

Rate me:
Please Sign up or sign in to vote.
3.67/5 (6 votes)
9 Nov 2000 124.6K   1.9K   31   3
Simple step by step article explaining how to create a modeless dialog box as sibling of the app's main window.

This article is part of the drag and drop interface samples.

  1. Serializing ASCII Data
  2. Modeless child dialog
  3. Modeless sibling dialog
  4. The drag source
  5. The MFC drop target
  6. The TBTextTarget class

It's nearly as short as the child version of modeless dialogs.

Follow these steps:

  1. Create a new dialog resource and use the Class Wizard for making a new CDialog based class COtherDropDialog.
  2. In the class maintaining the modeless dialog (here it's CInterfaceView), add a member variable of type pointer to CWinThread and make sure you have a destructor:
    C++
    class CInterfaceView : public CView
    {
    ...
        CWinThread *m_pDlgThread
        ~CInterfaceView();
    ...
    }
  3. Use ClassWizard to create a new CWinThread-based class COtherDropDialogThread.
  4. Override COtherRopDialogThread::InitInstance():
    If you forget to change the m_pMainWnd variable, the dialog will stay modal!
    C++
    BOOL COtherDropDialogThread::InitInstance()
    {
        COtherDropDialog dlg;
    
        m_pMainWnd = &dlg;
        dlg.DoModal();  // returning false will make MFC doing the cleanup for us :)
              
        return FALSE;
    }
  5. In your appropriate message handler, do the following:
    C++
    void CInterfaceView::OnFileOthermodeless() 
    {
        // this block determines if we have our dialog already
        // displayed and not yet closed. You can't rely on 
        // m_pDlgThread to be NULL, because if you already have displayed 
        // and closed it, the pointer is not NULL but invalid	 
        if (AfxIsValidAddress(m_pDlgThread, sizeof(CWinThread)) &&
            AfxIsValidAddress(m_pDlgThread->m_pMainWnd, sizeof(CWnd)))
        {
            if (::IsWindow(m_pDlgThread->m_pMainWnd->GetSafeHwnd()))
            {    m_pDlgThread->GetMainWnd()->SetWindowPos(
    	                &wndTop, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
                 return;
            }
        }
        // OK, we need to create a new thread
        m_pDlgThread=AfxBeginThread( RUNTIME_CLASS(COtherDropDialogThread) );	
    }
  6. If you like your top-level window to have an icon displayed in the taskbar, add the following to COtherDropDialog:: OnInitDialog (omitted in the sample):
    C++
    BOOL COtherDropDialog::OnInitDialog() 
    {
        CDialog::OnInitDialog();
        m_hIcon = AfxGetApp()->LoadIcon(IDI_MYICON);
        SetIcon(m_hIcon, TRUE);
        SetIcon(m_hIcon, FALSE);
        // and all the other stuff
        //...
    }
  7. Don't forget to do the clean-up in the destructor:
    C++
    CInterfaceView::~CInterfaceView()
    {
        if (AfxIsValidAddress(m_pDlgThread, sizeof(CWinThread)) &&
    	AfxIsValidAddress(m_pDlgThread->m_pMainWnd, sizeof(CWnd)))
        {
    	// retrieve the threads main window
    	CDialog *pDlg = (CDialog *)m_pDlgThread->m_pMainWnd;
    
    	if (::IsWindow(pDlg->GetSafeHwnd()))
    	{
    	    // make sure we have a CDialog-derived main 
    	    // thread window then terminate it
    	    ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CDialog)));
    	    pDlg->EndDialog(IDCANCEL);
    
    	    // now give the thread some time to end, 
    	    // 2 second should be enough. You may 
    	    // able to use INFINITE as wait value, if you dare...
    	    WaitForSingleObject(m_pDlgThread->m_hThread, 2000);
    	    m_pDlgThread=NULL;
    	}
        }
    }

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Germany Germany
PhD Chemist,
programming with MSVC & MFC since 1996

Comments and Discussions

 
QuestionHow you can add a modeless dialog to a win32 project Pin
Member 100432902-Jul-14 23:10
Member 100432902-Jul-14 23:10 
GeneralSystem crashes Pin
Member 6264825-Apr-05 9:00
Member 6264825-Apr-05 9:00 
Generalprobelm with CPU usage Pin
Anonymous14-Apr-03 20:50
Anonymous14-Apr-03 20:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.