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






3.67/5 (6投票s)
2000 年 11 月 10 日

125726

1856
一个简单的分步文章,解释如何创建应用程序主窗口的同级无模式对话框。
本文是 拖放界面 示例的一部分。
- 序列化 ASCII 数据
- 无模式子对话框
- 无模式兄弟对话框
- 拖放源
- MFC 目标
- TBTextTarget 类
它几乎和无模式对话框的子版本一样简短。
请遵循以下步骤
- 创建一个新的对话框资源,并使用“类向导”创建一个基于
CDialog
的新类COtherDropDialog
。 - 在维护无模式对话框的类中(这里是
CInterfaceView
),添加一个指向CWinThread
类型的成员变量,并确保你有一个析构函数。class CInterfaceView : public CView { ... CWinThread *m_pDlgThread ~CInterfaceView(); ... }
- 使用“类向导”创建一个基于
CWinThread
的新类COtherDropDialogThread
。 - 重写
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; }
- 在适当的消息处理程序中,执行以下操作
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) ); }
- 如果你希望你的顶级窗口在任务栏中显示一个图标,请将以下内容添加到
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 //... }
- 不要忘记在析构函数中进行清理
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; } } }
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。