我是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。
我不是那个意思。有人可以解释一下吗?
答案 0 :(得分:1)
来自MSDN网站:
如果函数无法创建对话框,则返回值为-1;如果发生其他错误,则返回IDABORT,在这种情况下,“输出”窗口将包含来自GetLastError的错误信息。
可在此处找到错误引文:http://msdn.microsoft.com/en-us/library/619z63f5.aspx
系统无法创建和运行对话框。您可以阅读提供的MSDN链接以获取更多信息。
您可能有一个控件或DLL没有正确注册该对话框,因此无法找到。