none
Failed to create empty document RRS feed

  • 问题

  • int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	BOOL bNameValid;
    
    	if (!m_wndMenuBar.Create(this))
    	{
    		TRACE0("Failed to create menubar\n");
    		return -1;      // fail to create
    	}
    
    	m_wndMenuBar.SetPaneStyle(m_wndMenuBar.GetPaneStyle() | CBRS_SIZE_DYNAMIC | CBRS_TOOLTIPS | CBRS_FLYBY);
    
    	// prevent the menu bar from taking the focus on activation
    	CMFCPopupMenu::SetForceMenuFocus(FALSE);
    
    	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    		!m_wndToolBar.LoadToolBar(theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME))
    	{
    		TRACE0("Failed to create toolbar\n");
    		return -1;      // fail to create
    	}
    
    	if(!m_newToolBar.LoadToolBar(IDR_TOOLBAR_MY))  //错误就是出在这里,IDR_TOOLBAR_MY是我自定义的一个TOOLBAR,共有4个图标
    	{
    		TRACE0("Failed to create toolbar\n");
    		return -1;
    	}
            // 剩下的代码就不浪费版面了
    }
    
    // 以下是跟踪调试的位置
    // 位于bartool.cpp
    // 跟踪1
    BOOL CToolBar::LoadToolBar(LPCTSTR lpszResourceName)
    {
    	ASSERT_VALID(this);
    	ASSERT(lpszResourceName != NULL);
    
    	// determine location of the bitmap in resource fork
    	HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_TOOLBAR);
    	HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR);
    	if (hRsrc == NULL)
    		return FALSE;
    
    	HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
    	if (hGlobal == NULL)
    		return FALSE;
    
    	CToolBarData* pData = (CToolBarData*)LockResource(hGlobal);
    	if (pData == NULL)
    		return FALSE;
    	ASSERT(pData->wVersion == 1);
    
    	UINT* pItems = new UINT[pData->wItemCount];
    	for (int i = 0; i < pData->wItemCount; i++)
    		pItems[i] = pData->items()[i];
    	BOOL bResult = SetButtons(pItems, pData->wItemCount); // 问题就出在这里,bResult值为0,下面继续跟踪SetButton,见跟踪2
    	delete[] pItems;
    
    	if (bResult)
    	{
    		// set new sizes of the buttons
    		CSize sizeImage(pData->wWidth, pData->wHeight);
    		CSize sizeButton(pData->wWidth + 7, pData->wHeight + 7);
    		SetSizes(sizeButton, sizeImage);
    
    		// load bitmap now that sizes are known by the toolbar control
    		bResult = LoadBitmap(lpszResourceName);
    	}
    
    	UnlockResource(hGlobal);
    	FreeResource(hGlobal);
    
    	return bResult;
    }
    
    // 跟踪2
    BOOL CToolBar::SetButtons(const UINT* lpIDArray, int nIDCount)
    {
    	ASSERT_VALID(this);
    	ASSERT(nIDCount >= 1);  // must be at least one of them
    	ASSERT(lpIDArray == NULL ||
    		AfxIsValidAddress(lpIDArray, sizeof(UINT) * nIDCount, FALSE));
    
    	// delete all existing buttons
    	int nCount = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
    	while (nCount--)
    		VERIFY(DefWindowProc(TB_DELETEBUTTON, 0, 0));
    
    	TBBUTTON button; memset(&button, 0, sizeof(TBBUTTON));
    	button.iString = -1;
    	if (lpIDArray != NULL)
    	{
    		// add new buttons to the common control
    		int iImage = 0;
    		for (int i = 0; i < nIDCount; i++)
    		{
    			button.fsState = TBSTATE_ENABLED;
    			if ((button.idCommand = *lpIDArray++) == 0)
    			{
    				// separator
    				button.fsStyle = TBSTYLE_SEP;
    				// width of separator includes 8 pixel overlap
    				ASSERT(_afxComCtlVersion != -1);
    				if ((GetStyle() & TBSTYLE_FLAT) || _afxComCtlVersion == VERSION_IE4)
    					button.iBitmap = 6;
    				else
    					button.iBitmap = 8;
    			}
    			else
    			{
    				// a command button with image
    				button.fsStyle = TBSTYLE_BUTTON;
    				button.iBitmap = iImage++;
    			}
    			if (!DefWindowProc(TB_ADDBUTTONS, 1, (LPARAM)&button))  // 问题似乎就出现在这里,当处理第一个button时就返回FALSE了,一共4个,见跟踪3
    				return FALSE;
    		}
    	}
    	else
    	{
    		// add 'blank' buttons
    		button.fsState = TBSTATE_ENABLED;
    		for (int i = 0; i < nIDCount; i++)
    		{
    			ASSERT(button.fsStyle == TBSTYLE_BUTTON);
    			if (!DefWindowProc(TB_ADDBUTTONS, 1, (LPARAM)&button))
    				return FALSE;
    		}
    	}
    	m_nCount = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
    	m_bDelayedButtonLayout = TRUE;
    
    	return TRUE;
    }
    // 跟踪3,位于wincore.cpp
    // DefWindowProc,跳到这里小弟就看不明白了
    LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
    {
    	if (m_pfnSuper != NULL)
    		return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);
    
    	WNDPROC pfnWndProc;
    	if ((pfnWndProc = *GetSuperWndProcAddr()) == NULL)
    		return ::DefWindowProc(m_hWnd, nMsg, wParam, lParam);
    	else
    		return ::CallWindowProc(pfnWndProc, m_hWnd, nMsg, wParam, lParam);
    }
    
    最后报错:
    "Failed to create empty document"
    网上有很多类似的问题,可是貌似和我的这个不一样,还请大侠帮忙看看哈
    这是学习《visual c++2010开发权威指南》第2章碰到的问题,我的环境是xp sp3,vc++2010 sp1
    2013年4月25日 12:01

答案

  • if(m_newToolBar.Create(this) && !m_newToolBar.LoadToolBar(IDR_TOOLBAR_MY) 
     {
      TRACE0("Failed to create toolbar\n");
      return -1;
     }

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.


    2013年4月26日 5:24
    版主

全部回复

  • if(m_newToolBar.Create(this) && !m_newToolBar.LoadToolBar(IDR_TOOLBAR_MY) 
     {
      TRACE0("Failed to create toolbar\n");
      return -1;
     }

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.


    2013年4月26日 5:24
    版主
  • 嗯,谢谢,问题解决了:)
    2013年4月27日 1:42
  • int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	BOOL bNameValid;
    
    	if (!m_wndMenuBar.Create(this))
    	{
    		TRACE0("Failed to create menubar\n");
    		return -1;      // fail to create
    	}
    
    	m_wndMenuBar.SetPaneStyle(m_wndMenuBar.GetPaneStyle() | CBRS_SIZE_DYNAMIC | CBRS_TOOLTIPS | CBRS_FLYBY);
    
    	// prevent the menu bar from taking the focus on activation
    	CMFCPopupMenu::SetForceMenuFocus(FALSE);
    
    	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    		!m_wndToolBar.LoadToolBar(theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME))
    	{
    		TRACE0("Failed to create toolbar\n");
    		return -1;      // fail to create
    	}
    
    	if(!m_newToolBar.LoadToolBar(IDR_TOOLBAR_MY))  //错误就是出在这里,IDR_TOOLBAR_MY是我自定义的一个TOOLBAR,共有4个图标
    	{
    		TRACE0("Failed to create toolbar\n");
    		return -1;
    	}
            // 剩下的代码就不浪费版面了
    }
    
    // 以下是跟踪调试的位置
    // 位于bartool.cpp
    // 跟踪1
    BOOL CToolBar::LoadToolBar(LPCTSTR lpszResourceName)
    {
    	ASSERT_VALID(this);
    	ASSERT(lpszResourceName != NULL);
    
    	// determine location of the bitmap in resource fork
    	HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_TOOLBAR);
    	HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR);
    	if (hRsrc == NULL)
    		return FALSE;
    
    	HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
    	if (hGlobal == NULL)
    		return FALSE;
    
    	CToolBarData* pData = (CToolBarData*)LockResource(hGlobal);
    	if (pData == NULL)
    		return FALSE;
    	ASSERT(pData->wVersion == 1);
    
    	UINT* pItems = new UINT[pData->wItemCount];
    	for (int i = 0; i < pData->wItemCount; i++)
    		pItems[i] = pData->items()[i];
    	BOOL bResult = SetButtons(pItems, pData->wItemCount); // 问题就出在这里,bResult值为0,下面继续跟踪SetButton,见跟踪2
    	delete[] pItems;
    
    	if (bResult)
    	{
    		// set new sizes of the buttons
    		CSize sizeImage(pData->wWidth, pData->wHeight);
    		CSize sizeButton(pData->wWidth + 7, pData->wHeight + 7);
    		SetSizes(sizeButton, sizeImage);
    
    		// load bitmap now that sizes are known by the toolbar control
    		bResult = LoadBitmap(lpszResourceName);
    	}
    
    	UnlockResource(hGlobal);
    	FreeResource(hGlobal);
    
    	return bResult;
    }
    
    // 跟踪2
    BOOL CToolBar::SetButtons(const UINT* lpIDArray, int nIDCount)
    {
    	ASSERT_VALID(this);
    	ASSERT(nIDCount >= 1);  // must be at least one of them
    	ASSERT(lpIDArray == NULL ||
    		AfxIsValidAddress(lpIDArray, sizeof(UINT) * nIDCount, FALSE));
    
    	// delete all existing buttons
    	int nCount = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
    	while (nCount--)
    		VERIFY(DefWindowProc(TB_DELETEBUTTON, 0, 0));
    
    	TBBUTTON button; memset(&button, 0, sizeof(TBBUTTON));
    	button.iString = -1;
    	if (lpIDArray != NULL)
    	{
    		// add new buttons to the common control
    		int iImage = 0;
    		for (int i = 0; i < nIDCount; i++)
    		{
    			button.fsState = TBSTATE_ENABLED;
    			if ((button.idCommand = *lpIDArray++) == 0)
    			{
    				// separator
    				button.fsStyle = TBSTYLE_SEP;
    				// width of separator includes 8 pixel overlap
    				ASSERT(_afxComCtlVersion != -1);
    				if ((GetStyle() & TBSTYLE_FLAT) || _afxComCtlVersion == VERSION_IE4)
    					button.iBitmap = 6;
    				else
    					button.iBitmap = 8;
    			}
    			else
    			{
    				// a command button with image
    				button.fsStyle = TBSTYLE_BUTTON;
    				button.iBitmap = iImage++;
    			}
    			if (!DefWindowProc(TB_ADDBUTTONS, 1, (LPARAM)&button))  // 问题似乎就出现在这里,当处理第一个button时就返回FALSE了,一共4个,见跟踪3
    				return FALSE;
    		}
    	}
    	else
    	{
    		// add 'blank' buttons
    		button.fsState = TBSTATE_ENABLED;
    		for (int i = 0; i < nIDCount; i++)
    		{
    			ASSERT(button.fsStyle == TBSTYLE_BUTTON);
    			if (!DefWindowProc(TB_ADDBUTTONS, 1, (LPARAM)&button))
    				return FALSE;
    		}
    	}
    	m_nCount = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
    	m_bDelayedButtonLayout = TRUE;
    
    	return TRUE;
    }
    // 跟踪3,位于wincore.cpp
    // DefWindowProc,跳到这里小弟就看不明白了
    LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
    {
    	if (m_pfnSuper != NULL)
    		return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);
    
    	WNDPROC pfnWndProc;
    	if ((pfnWndProc = *GetSuperWndProcAddr()) == NULL)
    		return ::DefWindowProc(m_hWnd, nMsg, wParam, lParam);
    	else
    		return ::CallWindowProc(pfnWndProc, m_hWnd, nMsg, wParam, lParam);
    }
    最后报错:
    "Failed to create empty document"
    网上有很多类似的问题,可是貌似和我的这个不一样,还请大侠帮忙看看哈
    这是学习《visual c++2010开发权威指南》第2章碰到的问题,我的环境是xp sp3,vc++2010 sp1

    2016年6月22日 12:59