【问题标题】:MFC application DialogBased using propertyPage, DoModal() for a CDialog don't open any DialogMFC应用程序DialogBased使用propertyPage,CDialog的DoModal()不打开任何对话框
【发布时间】:2013-11-25 02:53:19
【问题描述】:

在 VS2013 中,我创建了一个基于对话的 MFC 应用程序。 我修改了项目,以便在应用程序开始时使用 PropertyPage 和 Propertysheet,因此它不会启动 CDialog,而是启动我的 propertypage。

之后,我创建了一个对话框,并且类关联(from::CdialogEx)。 我想在单击按钮后打开此对话框。

点击按钮后,我会这样做:

CMyDialog myDialog;
myDialog.DoModal();

我没有任何错误消息,但是我没有在屏幕上显示我的对话框。

也许是因为这个对话框没有子没有?

有人可以帮帮我吗?

非常感谢,

最好的问候,

尼克斯

编辑:

这是我的入口点:

#include "stdafx.h"
#include "KenoApp.h"
#include "KenoDlg.h"

#include "GenerationDlg.h"
#include "KenoSheet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CKenoApp

BEGIN_MESSAGE_MAP(CKenoApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// construction CKenoApp

CKenoApp::CKenoApp()
{

}


// Seul et unique objet CKenoApp

CKenoApp theApp;


// initialisation de CKenoApp

BOOL CKenoApp::InitInstance()
{
    AfxEnableControlContainer();

    // Standard initialization

#ifdef _AFXDLL
        // Call this when using MFC in a shared DLL
#else
    Enable3dControlsStatic();   // Call this when linking to MFC statically
#endif

    CKenoSheet KenoSheet;
    KenoSheet.SetTitle(L"Keno Helper v1.1");

    CGenerationDlg Generation;
    CKenoDlg KenoDlg;

    KenoSheet.AddPage(&KenoDlg);
    KenoSheet.AddPage(&Generation);

    //m_pMainWnd = &KenoSheet;

    int nResponse = KenoSheet.DoModal();

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
}

然后,在我的属性页上:

CAboutDlg myDialog;
theApp.m_pMainWnd = &myDialog;
myDialog.DoModal();

我现在的问题是,DoModal() 关闭了我的应用程序。

【问题讨论】:

  • 您可以尝试将对话框显示为无模式的吗?
  • 不,这是怎么做到的?我尝试使用 ShowWindow() 方法,但我有一个断言错误!
  • 尝试和错误(见顶部);)
  • 您是否查看过确保代码正在查找对话框资源?找不到资源时,对话框创建通常会失败。

标签: c++ visual-studio-2010 winapi mfc dialog


【解决方案1】:

你能把myDialog.DoModal();的调用结果贴出来吗?

你可以试试下面这段代码:

无模式:

CMyDialog myDialog = new CMyDialog();

if(myDialog != NULL)
{
    BOOL ret = myDialog->Create(10000, this);
    if(!ret)
        AfxMessageBox(_T("Error creating Dialog"));

    myDialog->ShowWindow(SW_SHOW);
}
else
{
    AfxMessageBox(_T("Error Creating Dialog Object"));
}

【讨论】:

  • 你好,我特雷德,但是,在 Creat() 上,我有一个断言错误。我在第一个初始线程上附加了错误消息。
  • 10000 中的Create(10000, this) 更改为对话框模板资源的ID 号。
  • 告诉我们这个位置的 ASSERT 类型。
  • WindowShow 中的断言类型 ?ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
  • AfxWin2.inl 中的 CWnd::GetSafeHwnd() 似乎返回 false。
【解决方案2】:

快速修复:在您的应用程序的InitInstance() 中:

CMyPropSheet pps(_T("My Property Sheet"), NULL, 0);
//m_pMainWnd = &pps;        // *** remark away this line if you have it
int nResponse = pps.DoModal();
// do response ...

CTestDlg dlg;
m_pMainWnd = &dlg;          // this line is a must have
nResponse = dlg.DoModal();
// do response ...

以上代码假设 PropertySheet 和 Dialog 在应用程序的 InitInstance() 内依次启动。在您提供更多信息后,似乎不是您想要的方式,因此上述代码不适用于您的问题。在使用我的建议之前,请将您的代码恢复为原始代码。

【讨论】:

  • 我尝试了您的解决方案。删除行“m_pMainWnd = &pps”并在 DoModal() 之前添加行“theApp.m_pMainWnd = &myDialog”,我的应用程序在 doModal() 上关闭!没有错误信息,但关闭应用程序!
  • @WalterFabioSimoni - 属性表和对话框启动的顺序是什么,是否与我建议的代码一样,一个接一个?或者您是否尝试在 Propertysheet 中启动对话框? m_pMainWnd 应该是应用程序的唯一且唯一的主窗口。将其设置到应用程序类后,一旦此 m_pMainWnd 句柄变为无效/销毁,应用程序将自行终止。
  • @WalterFabioSimoni - 上面的代码假设 PropertySheet 和 Dialog 在应用程序的 InitInstance() 内依次启动。在您提供更多信息后,似乎不是您想要的方式,因此上述代码不适用于您的问题。请在使用我的建议之前将您的代码恢复为原始代码。
  • @WalterFabioSimoni - 我实际上用我自己的虚拟 PropertySheet/Dialog 类作为您实现的方式测试了您的代码,并且在按下 PropertySheet 按钮时激活对话框没有遇到任何问题。我认为您的对话框类可能有一些错误会阻止它被创建。您应该单独测试对话框以隔离问题并确认它可以/无法创建,并且问题是/不是由 PropertySheet 引起的。这可以通过将 PropertySheet 替换为对话框并在调试模式下运行以找出问题来完成。
  • 任何重要的事情,我已经解决了!如何 ?我将对话框样式设置为 DS_NOFAILCREATE。没关系!但是......为什么创建失败?我的对话框中只有一个控件,它是一个按钮!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
相关资源
最近更新 更多