【问题标题】:Why does MFC DoModal returns -1 ? What does -1 mean?为什么 MFC DoModal 返回 -1 ? -1 是什么意思?
【发布时间】:2014-12-10 00:19:19
【问题描述】:

我对 MFC 非常陌生,并被要求在 Visual Studio 2008 中创建 MFC 应用程序。我正在尝试创建两个对话框。第一个在启动时打开,如果在第一个上按 OK,则打开第二个对话框。但是,当我调用 DoModal() 时,我的第一个对话框正确打开,但第二个对话框返回 -1。谁能让我知道我做错了什么?根据 MSDN 文档,-1 是“出了点问题”。我无法弄清楚我做错了什么。

// The main file from where the dialogs are launched - Encrypt.cpp
#include "stdafx.h"
#include "Encrypt.h"
#include "MainDialog.h"
#include "AddlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

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

CEncryptApp::CEncryptApp()
{

}

CEncryptApp theApp;

BOOL CEncryptApp::InitInstance()
{
    CWinApp::InitInstance();
    AfxEnableControlContainer();
    AfxInitRichEdit();

    CMainDialog dlg;
    INT_PTR nResponse = dlg.DoModal();

    if (nResponse == IDOK)
    {
        CAddlDlg ldg;
        INT_PTR nResponse = ldg.DoModal();

        switch (nResponse)
        {
            case -1: 
                AfxMessageBox(_T("-1"));
                break;
            case IDABORT:
                AfxMessageBox(_T("1!")); 
                break;
            case IDOK:
                AfxMessageBox(_T("2!")); 
                break;
             case IDCANCEL:
                AfxMessageBox(_T("3!"));  
                break;
             default:
                AfxMessageBox(nResponse);  
             break;
        };


    }
    else if (nResponse == IDCANCEL)
    {

    }

return FALSE;
}

这是主对话框

// Main Dialog

#include "stdafx.h"
#include "Encrypt.h"
#include "MainDialog.h"

#ifdef _DEBUG
 #define new DEBUG_NEW
#endif


IMPLEMENT_DYNAMIC(CMainDialog, CDialog)

CMainDialog::CMainDialog(CWnd* pParent /*=NULL*/)
     : CDialog(CMainDialog::IDD, pParent)
 {
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  }

 CMainDialog::~CMainDialog()
 {
 }

void CMainDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
 }

BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
   ON_WM_PAINT()
   ON_WM_QUERYDRAGICON()
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

  BOOL CMainDialog::OnInitDialog()
  {
     CDialog::OnInitDialog();

   SetIcon(m_hIcon, TRUE);          // Set big icon
   SetIcon(m_hIcon, FALSE);     // Set small icon
    return TRUE;  // return TRUE  unless you set the focus to a control
 }

 void CMainDialog::OnPaint()
 {
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
       int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;
        dc.DrawIcon(x, y, m_hIcon);
   }
   else
   {
       CDialog::OnPaint();
    }
}

 HCURSOR CMainDialog::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
 }

//第二个对话框

#include "stdafx.h"
#include "Encrypt.h"
#include "AddlDlg.h"

IMPLEMENT_DYNAMIC(CAddlDlg, CDialog)
CAddlDlg::CAddlDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CAddlDlg::IDD, pParent)
{      // Reaches until here
}

CAddlDlg::~CAddlDlg()
{
}

void CAddlDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
}

BOOL CAddlDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();
    return TRUE;
} 

BEGIN_MESSAGE_MAP(CAddlDlg, CDialog)
END_MESSAGE_MAP()

当我尝试打印调试语句时,我进入了第二个对话框的构造函数,但 OnInitDialog 从未被调用。有人可以帮帮我吗?

**更新 :: **

我在进一步调试时看到的错误表明它位于函数 ::CreateDialogIndirect 中 dlgcore.cpp 的第 311 行,实际错误是

::CreateDialogIndirect() 没有创建窗口(即由于模板中的错误)并返回 NULL。

我不明白那是什么意思。谁能给我解释一下?

【问题讨论】:

  • 您应该使用调试器并逐步调用 DoModal。
  • 为什么这被否决了?这是一个包含所有细节的真实问题。
  • 我不是投反对票的人。

标签: c++ visual-studio winapi mfc


【解决方案1】:

来自 MSDN 网站:

如果函数无法创建对话框,则返回值为 –1,如果发生其他错误,则返回值为 IDABORT,在这种情况下,输出窗口将包含来自 GetLastError 的错误信息。

可以在这里找到错误引用:http://msdn.microsoft.com/en-us/library/619z63f5.aspx

系统无法创建和运行您的对话框。您可以阅读提供的 MSDN 链接了解更多信息。

很可能您有一个未正确注册的控件或 DLL,对话框需要,因此无法找到。

【讨论】:

  • 是的。而且,我的猜测是使用 RichEdit 2.0 控件,因为她确实有 AfxInitRichEdit() 已经在那里。如果是这种情况,请尝试使用 AfxInitRichEdit2()。如果她正在使用 OLE 控件,那么她还应该在 InitInstance() 中调用 AfxEnableControlContainer()。
  • @StarPilot 我对此进行了进一步调试,发现错误位于 dlgcore.cpp 的第 311 行。在下面的进一步阅读中,它说“::CreateDialogIndirect() 没有创建窗口(即由于模板中的错误)*并返回 NULL。”
  • @Surya 在这里幽默我们。我要做的是从对话框中删除所有控件,然后查看是否可以创建对话框。如果它仍然失败,那么它不是因为我们怀疑它是由于对对话框的控制。如果它成功,那么它是由于对话框上的控制。此时,一次添加一个控件(或一次添加一种类型,例如 STATIC、EDIT 等),直到错误再次出现,然后您将找到有问题的控件。
  • 简单测试:首先创建第二个对话框。如果失败,则与第一个对话框无关。 ActiveX 控件也可能是罪魁祸首。
猜你喜欢
  • 2012-09-06
  • 2021-01-24
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
相关资源
最近更新 更多