I'm attempting to write a simple MFC app that draws in a scrollable window, using code based on CFrameWnd. The code below is adapted from Prosise "Programming Windows with MFC", 2nd ed, pp 89ff.
When I run this in the debugger, I get two first-chance exceptions. If I ignore these, the window appears as expected, and I can draw in it. If I enable break on "C++ exceptions", I get a stack which is only "internal" code, for which I do not have the source. By stepping through the code, I find that the exceptions occur in CWnd::CreateEx, in the call to CreateWindowEx.
What is wrong with this code? 64-bit Windows 7, Visual Studio 2013 update 4. This is a debug build, x64, using MFC in a static library, using multi-byte character set, with the multi-threaded debug runtime (/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)
{
}