I have working through the Sketcher MFC MDI app in Chapt. 15, and all was going swimmingly until I added toolbar buttons for the functions (pp 924ff). On run, I then get a" Debug Assertion failed!" Following the reference in the popup message, the error was triggered from a function in afxtoolbar.cpp, which I paste here:
Code:
BOOL __stdcall CMFCToolBar::SetUserImages(CMFCToolBarImages* pUserImages)
{
ENSURE(pUserImages != NULL);
if (!pUserImages->IsValid())
{
ASSERT(FALSE);
return FALSE;
}
if (m_sizeImage != pUserImages->GetImageSize())
{
ASSERT(FALSE);
return FALSE;
}
m_pUserImages = pUserImages;
return TRUE;
}
My message comes from the second test, on GetImageSize(), so evidently the problem has to do with the size of the toolbar icons.
On doing some debugging, I find that the assertion failure come right after the following code in Mainframe.cpp (in CMainFrame::OnCreate) :
Code:
if (CMFCToolBar::GetUserImages() == NULL)
{
// load user-defined toolbar images
if (m_UserImages.Load(_T(".\\UserImages.bmp")))
{
CMFCToolBar::SetUserImages(&m_UserImages);
}
}
It turns out CMFCToolBar::GetUserImages() is returning NULL, and loading UserImages.bmp, rather then the icons stored in the app's .rc file. UserImages.bmp contains tiny MS icons (a little man, house, question mark, red X mark etc), NOT the icons I have created in the resource editor. The problem seems to boil down to figuring out why GetUserImages is returning NULL here.
I don't know if it's relevant, but at the top of the call stack (just above the breakpoint in CMainFrame::OnCreate) is:
mfc100ud.dll!5e2b292f()
[Frames below may be incorrect and/or missing, no symbols loaded for mfc100ud.dll]
With some help from the MSDN forum, I found I could get rid of the assert failure by simply removing the code, OR adding a line:
Code:
if (CMFCToolBar::GetUserImages() == NULL)
{
// load user-defined toolbar images
if (m_UserImages.Load(_T(".\\UserImages.bmp")))
{
m_UserImages.SetImageSize(CSize(16, 16), FALSE); //new line
CMFCToolBar::SetUserImages(&m_UserImages);
}
}
While both of these worked, I don't really understand why: what GetUserImages() is doing and why it returns NULL here, and looks for the UserImages.bmp file rather than recognising that the toolbar button icons are in the .rc file.
I'd be grateful for clarification and guidance on this!