#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14

    [MFC] How do you derive from CToolBar?


    Please bear with me, there's several code components at work here, though they're all really basic.

    I'm learning MFC, and its come time to play with toolbars. It was smooth sailing, until I tried to make a custom toolbar class, to accomidate controls that aren't buttons. But lets not get ahead of ourselves here, I haven't gotten to the point of adding extra controls, I can't even seem to derive a class from CToolBar. Here's the relevant code from my CStyleBar class definition. There is literally no other code other than comments, includes, and misc directives that MSVC++ uses.

    Code:
    // StyleBar.h
    
    class CStyleBar : public CToolBar
    {
    // Implementation
    protected:
    
        // Generated message map functions
        //{{AFX_MSG(CStyleBar)
        afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
        //}}AFX_MSG
    
        DECLARE_MESSAGE_MAP()
    };
    
    // in StyleBar.cpp
    BEGIN_MESSAGE_MAP(CStyleBar, CCmdTarget)
        //{{AFX_MSG_MAP(CStyleBar)
        ON_WM_CREATE()
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    int CStyleBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
        if (CToolBar::OnCreate(lpCreateStruct) == -1)
            return -1;
        return 0;
    }
    That's the meat of the class definition. The OnCreate() is pretty empty, because I moved the LoadToolBar() into the code below for diagnostic purposes, and...there's no point in adding my custom controls if I can't even get the toolbar to load. Now here's how the CStyleBar is created.

    Code:
    // ...in MainFrame.h
        CStyleBar m_wndStyleBar;
    
    // ...in MainFrame.cpp
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
    
        // Tell the frame window to permit docking.
        EnableDocking(CBRS_ALIGN_ANY);
    
        // Create the toolbars and style bar.
        if (!CreateToolBar() // Another toolbar that works ok
            || !CreateStatusBar() // Statusbar, also works ok
            || !CreateStyleBar()) // Doesn't work unless a CToolBar object
            return -1;
    
        return 0;
    }
    
    BOOL CMainFrame::CreateStyleBar()
    {
        if (!m_wndStyleBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, IDW_STYLEBAR)
            || !m_wndStyleBar.LoadToolBar(IDR_STYLEBAR))
            return FALSE;
    
        m_wndStyleBar.SetWindowText(_T("Styles"));
        m_wndStyleBar.EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
        DockControlBar(&m_wndStyleBar);
        return TRUE;
    }
    I'm getting an assertion error:

    Code:
    // ...in CWnd::AssertValid() from Wincore.cpp
        ASSERT(::IsWindow(m_hWnd)); // Causes an Assertion error!
    So, it would seem that my m_wndStyleBar is not being properly created as a window, but I don't know why. I've tracked the problem down to the CStyleBar class not working properly, because switching the m_wndStyleBar definition from a CStyleBar to a CToolBar makes the toolbar work properly (albeit unable to hold custom controls).

    I must be missing something, maybe an overridable function? I swear I can't find any thing missing or out of place in my code compared to the book I'm learning from (save for I haven't added in any message handlers yet).

    I know its a vague question, but anyone have an idea of what's missing?

    -Mike
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    My bad....I found it:

    Code:
    BEGIN_MESSAGE_MAP(CStyleBar, CCmdTarget)
    Heh....I forgot to change the base class there when editing the autogenerated code.

    Damit, that took hours to figure out.

IMN logo majestic logo threadwatch logo seochat tools logo