如何显示非模态CDialog?

发布于 2024-08-21 16:00:25 字数 230 浏览 5 评论 0原文

有人可以告诉我如何在 MFC 的 Visual c++ 6.0 中创建非模态对话框并显示它吗? 我写了这段代码:

CDialog dialog;
if (dialog.init(initialization values...))
   dialog.DoModal();

但它阻止我的应用程序显示对话框。我不知道是否有任何方法或其他方式可以做到这一点。

谢谢

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花辞树 2024-08-28 16:00:25
/* 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;
/* 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;
吾性傲以野 2024-08-28 16:00:25

使用CDialog::Create,然后使用CDialog::ShowWindow。您现在有一个非模式对话框。

Use CDialog::Create and then use CDialog::ShowWindow. You now have a modeless dialog box.

寻梦旅人 2024-08-28 16:00:25

您可以调用 CDialog::Create< /code>CWnd::ShowWindow 就像其他人建议的那样。

另外,请记住,如果对话框存储在局部变量中,则对话框将在创建后立即被销毁

You can call CDialog::Create and CWnd::ShowWindow like the others have suggested.

Also, keep in mind your dialog will be destroyed right after its creation if it is stored in a local variable.

愛放△進行李 2024-08-28 16:00:25

在这种情况下,我发现让它自我删除来处理清理是最方便的。

通常,从类内部释放“隐式”内存,而不是通过创建它的内容来释放“隐式”内存,通常被认为是不好的形式,但我通常对非模式对话框进行例外处理。

那是;

调用代码:

#include "MyDialog.h"

void CMyApp::OpenDialog()
{
    CMyDialog* pDlg = new CMyDialog(this);
    if (pDlg->Create(IDD_MYDIALOG, this))
        pDlg->ShowWindow(SW_SHOWNORMAL);
    else
        delete pDlg;
}

对话代码:

void CMapBasicDlg::OnDestroy()
{
    CDialog::OnDestroy();
    delete this; // Shown as non-modal, we'll clean up ourselves
}

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
}
趁年轻赶紧闹 2024-08-28 16:00:25

您需要改为调用CDialog::Create。完成对话框后,您需要调用 DestroyWindow。您可能还需要将对话框消息传递到对象上,但我不记得 MFC 是否为您处理这个问题。

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.

聚集的泪 2024-08-28 16:00:25

DoModal 正在阻塞。您必须在堆上创建对话框或使其成为您的类的成员(这很重要),调用 Create 然后调用 ShowWindow。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文