65.9K
CodeProject 正在变化。 阅读更多。
Home

创建模型对话框作为应用程序主窗口的同级

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (6投票s)

2000 年 11 月 10 日

viewsIcon

125726

downloadIcon

1856

一个简单的分步文章,解释如何创建应用程序主窗口的同级无模式对话框。

本文是 拖放界面 示例的一部分。

  1. 序列化 ASCII 数据
  2. 无模式子对话框
  3. 无模式兄弟对话框
  4. 拖放源
  5. MFC 目标
  6. TBTextTarget 类

它几乎和无模式对话框的子版本一样简短。

请遵循以下步骤

  1. 创建一个新的对话框资源,并使用“类向导”创建一个基于CDialog的新类COtherDropDialog
  2. 在维护无模式对话框的类中(这里是CInterfaceView),添加一个指向CWinThread类型的成员变量,并确保你有一个析构函数。
    class CInterfaceView : public CView
    {
    ...
        CWinThread *m_pDlgThread
        ~CInterfaceView();
    ...
    }
  3. 使用“类向导”创建一个基于CWinThread的新类COtherDropDialogThread
  4. 重写COtherRopDialogThread::InitInstance()
    如果你忘记更改m_pMainWnd变量,对话框将保持模态!
    BOOL COtherDropDialogThread::InitInstance()
    {
        COtherDropDialog dlg;
    
        m_pMainWnd = &dlg;
        dlg.DoModal();  // returning false will make MFC doing the cleanup for us :)
              
        return FALSE;
    }
  5. 在适当的消息处理程序中,执行以下操作
    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. 如果你希望你的顶级窗口在任务栏中显示一个图标,请将以下内容添加到COtherDropDialog:: OnInitDialog(示例中省略)
    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. 不要忘记在析构函数中进行清理
    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;
    	}
        }
    }

许可证

本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。

作者可能使用的许可证列表可以在此处找到。

© . All rights reserved.