We help IT Professionals succeed at work.

Why my second frame always crashes when I select it?

xtran888
xtran888 asked
on
Medium Priority
237 Views
Last Modified: 2013-11-20
My first frame window is based on this instance:
       CMultiDocTemplate* pDocTemplate;
      pDocTemplate = new CChartDocTemplate(
            IDR_CHARTTYPE,
            RUNTIME_CLASS(CChartDoc),
            RUNTIME_CLASS(CChildFrame),      // custom MDI child frame
            RUNTIME_CLASS(CChartView));
      AddDocTemplate(pDocTemplate);
      //
All the client view from this frame has the same ToolBar.
Since I don't want my client window has the same ToolBar, I create another frame like:

      CMultiDocTemplate* pVolumePriceTemplate;
      pVolumePriceTemplate = new CChartDocTemplate(
            IDR_VOLUMEPRICE,
 //       ID_CHART_NEW_VOLUMEPRICE,
            RUNTIME_CLASS(CVolumePriceDoc),
            RUNTIME_CLASS(CVolumePriceFrame), // custom MDI child frame
            RUNTIME_CLASS(CVolumePriceView));
      AddDocTemplate(pVolumePriceTemplate);

My question is that:

1- Do I need to create another frame so that my client window has different ToolBar? How Do I it?
2- Why my second frame always crashes when activated?
Comment
Watch Question

Commented:
How do you create the second frame? Can you post the code?

Have you add a menu and icon resources with ID = IDR_VOLUMEPRICE?

If you add the toolbar to the child window in the way I explained at http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_23087034.html you must chenge the ID of the toolbar resources when you call LoadToolBar();

Author

Commented:
This is the header file for CVolumePriceFrame:

class CStyleBar : public CToolBar
{
public:
      CComboBox   m_comboBox;
      CFont       m_font;
};

class CVolumePriceFrame : public CMDIChildWnd
{
      DECLARE_DYNCREATE(CVolumePriceFrame)
public:
      CVolumePriceFrame();

// Attributes
public:
//      CToolBar   m_wndToolBar;
//      CComboBox   m_comboBox;
      CStyleBar   m_wndStyleBar;

// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CVolumePriceFrame)
      public:
      virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//      virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW, const RECT& rect = rectDefault, CMDIFrameWnd* pParentWnd = NULL, CCreateContext* pContext = NULL);
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CVolumePriceFrame();
#ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext& dc) const;
#endif

// Generated message map functions
protected:
      //{{AFX_MSG(CVolumePriceFrame)
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      afx_msg void OnCombo();
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};
And its code:
#include "stdafx.h"
#include "ChartApp.h"

#include "VolumePriceFrm.h"
#include "resource.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CVolumePriceFrame

IMPLEMENT_DYNCREATE(CVolumePriceFrame, CMDIChildWnd)

BEGIN_MESSAGE_MAP(CVolumePriceFrame, CMDIChildWnd)
      //{{AFX_MSG_MAP(CVolumePriceFrame)
      ON_WM_CREATE()
      ON_COMMAND(IDW_COMBO, OnCombo)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVolumePriceFrame construction/destruction

CVolumePriceFrame::CVolumePriceFrame()
{
}

CVolumePriceFrame::~CVolumePriceFrame()
{
}

BOOL CVolumePriceFrame::PreCreateWindow(CREATESTRUCT& cs)
{

      return CMDIChildWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CVolumePriceFrame diagnostics

#ifdef _DEBUG
void CVolumePriceFrame::AssertValid() const
{
      CMDIChildWnd::AssertValid();
}

void CVolumePriceFrame::Dump(CDumpContext& dc) const
{
      CMDIChildWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CVolumePriceFrame message handlers


int CVolumePriceFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
            return -1;

            

      if (!m_wndStyleBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndStyleBar.LoadToolBar(IDR_VOLUMEPRICE))
      {
            TRACE0("Impossibile creare la barra degli strumenti.\n");
            return -1;
      }

        int i=0;
        while (m_wndStyleBar.GetItemID (i++) == IDW_COMBO)
              break;

      
      m_wndStyleBar.SetButtonInfo(0,IDW_COMBO,TBBS_GROUP,180);
      const int nDropHeight = 100;
      CRect rect;
      m_wndStyleBar.GetItemRect (0, &rect);
      rect.top = 3;
      rect.bottom = rect.top + nDropHeight;
    //
      //
      if (!m_wndStyleBar.m_comboBox.Create(
                  CBS_DROPDOWN|WS_VISIBLE|WS_TABSTOP,
                  rect, &m_wndStyleBar, IDW_COMBO))
      {
            TRACE0("Failed to create combo-box\n");
            return FALSE;
      }
      m_wndStyleBar.m_comboBox .AddString ("list1 ");
      m_wndStyleBar.m_comboBox .AddString ("list2 ");
      m_wndStyleBar.m_comboBox .AddString ("lsit3 ");
        // TODO: Add your specialized creation code here
      
      return 0;
}

void CVolumePriceFrame::OnCombo()
{
      // TODO: Add your command handler code here
      CString myString;
      m_wndStyleBar.m_comboBox .GetDlgItemText (0, myString);
      
}
I did change the ID of the toolbar resourcem but the program still crashes.

Author

Commented:
Is there anybody can help me to solve this question that was posted since Friday?

Commented:
Give a look at http://www.experts-exchange.com/Programming/System/Windows__Programming/MFC/Q_23075242.html
It can help to create a new window from a different template.

Author

Commented:
Thanks,
I found out that my frame window (CVolumePriceFrame) is derived from CMDIChildWnd instead of
CFrameWnd. Now it is working now.
PAQed with points refunded (500)

Computer101
EE Admin

Explore More ContentExplore courses, solutions, and other research materials related to this topic.