我正在尝试使用基于 CFrameWnd 的代码编写一个简单的 MFC 应用程序,该应用程序在可滚动窗口中绘制。下面的代码改编自 Prosise“Programming Windows with MFC”,第 2 版,第 89ff 页。
当我在调试器中运行它时,我得到两个第一次机会异常。如果我忽略这些,窗口将按预期显示,并且我可以在其中绘图。如果我在“C++ 异常”上启用中断,我会得到一个仅是“内部”代码的堆栈,而我没有其源代码。通过单步执行代码,我发现异常发生在 CWnd::CreateEx 中对 CreateWindowEx 的调用中。
这段代码有什么问题? 64 位 Windows 7,Visual Studio 2013 更新 4。这是一个调试版本,x64,在静态库中使用 MFC,使用多字节字符集,具有多线程调试运行时 (/MTd)。
// Viewer.h
#include <afxwin.h>
// Adapted from Prosise "Programming Windows with MFC", 2nd ed, pp 89ff.
class CViewerApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnHScroll(UINT nCode, UINT nPos, CScrollBar *pScrollBar);
afx_msg void OnVScroll(UINT nCode, UINT nPos, CScrollBar *pScrollBar);
public:
DECLARE_MESSAGE_MAP()
};
// Viewer.cpp: Display
#include <afxwin.h>
#include "Viewer.h"
CViewerApp myApp;
BOOL CViewerApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
Create(NULL, "Viewer", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL | WS_MAXIMIZE);
}
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CMainWindow::OnPaint()
{
}
void CMainWindow::OnSize(UINT nType, int cx, int cy)
{
//TO DO
}
void CMainWindow::OnHScroll(UINT nCode, UINT nPos, CScrollBar *pScrollBar)
{
}
void CMainWindow::OnVScroll(UINT nCode, UINT nPos, CScrollBar *pScrollBar)
{
}