C++ 如何显示非模态 CDialog?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2271821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a non-modal CDialog?
提问by rufian de los 4 mares
Can someone tell me how I could create a Non Modal Dialog in MFC's Visual c++ 6.0 and show it? I wrote this code:
有人能告诉我如何在 MFC 的 Visual c++ 6.0 中创建一个非模态对话框并显示它吗?我写了这段代码:
CDialog dialog;
if (dialog.init(initialization values...))
dialog.DoModal();
But it blocks my application from showing the dialog. I dont know if there exists any method or other way to do it.
但它阻止我的应用程序显示对话框。我不知道是否有任何方法或其他方式来做到这一点。
Thanks
谢谢
回答by Ramakrishna
/* CChildDialog class is inherited from CDialog */
CChildDialog *m_pDialog = NULL;
// Invoking the Dialog
m_pDialog = new CChildDialog();
if (m_pDialog != NULL)
{
BOOL ret = m_pDialog->Create(IDD_CHILDDIALOG, this);
if (!ret) //Create failed.
{
AfxMessageBox(_T("Error creating Dialog"));
}
m_pDialog->ShowWindow(SW_SHOW);
}
// Delete the dialog once done
delete m_pDialog;
回答by Goz
Use CDialog::Create and then use CDialog::ShowWindow. You now have a modeless dialog box.
使用 CDialog::Create,然后使用 CDialog::ShowWindow。您现在有一个无模式对话框。
回答by phil
You can call CDialog::Create
and CWnd::ShowWindow
like the others have suggested.
你可以打电话CDialog::Create
,CWnd::ShowWindow
就像其他人建议的那样。
Also, keep in mind your dialog will be destroyed right after its creationif it is stored in a local variable.
另外,请记住,如果您的对话框存储在局部变量中,它将在创建后立即销毁。
回答by Jonas
In this case I find it most convenient to let it self-delete itself to handle the cleanup.
在这种情况下,我发现让它自行删除以处理清理最方便。
Often it's considered bad form to make "implicit" memory freeing from within a class, and not by what it created it, but I usually make exceptions for modeless dialog boxes.
通常认为从类内部释放“隐式”内存而不是它创建它的内容是不好的形式,但我通常对无模式对话框进行例外处理。
That is;
那是;
Calling code:
调用代码:
#include "MyDialog.h"
void CMyApp::OpenDialog()
{
CMyDialog* pDlg = new CMyDialog(this);
if (pDlg->Create(IDD_MYDIALOG, this))
pDlg->ShowWindow(SW_SHOWNORMAL);
else
delete pDlg;
}
Dialog code:
对话代码:
void CMapBasicDlg::OnDestroy()
{
CDialog::OnDestroy();
delete this; // Shown as non-modal, we'll clean up ourselves
}
回答by Nikola Smiljani?
DoModal is blocking. You have to create your dialog on the heap or make it a member of your class (this is important), call Create then call ShowWindow.
DoModal 正在阻塞。您必须在堆上创建对话框或使其成为类的成员(这很重要),调用 Create 然后调用 ShowWindow。
回答by Rob
You need to call CDialog::Create
instead. You will need to call DestroyWindow
when you are finished with the dialog. You might also need to pass dialog messages onto the object but I can't remember if MFC handles this for you or not.
你需要打电话CDialog::Create
。DestroyWindow
完成对话后,您需要调用。您可能还需要将对话框消息传递到对象上,但我不记得 MFC 是否为您处理了这个问题。