CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Toolbar in MFC

  1. #1
    Join Date
    Feb 2022
    Posts
    11

    Toolbar in MFC

    Hello
    i try to create a simple toolbar with a mfc but the compiler can't find the class CToolbar:

    Code:
    #include <afxwin.h>
    #include "resource.h"
    class CResApp : public CWinApp
    {
    public:
    	BOOL InitInstance();
    };
    class TB: public CMFCToolBar;
    class CResFrame : public CFrameWnd
    {
    public:
    	CResFrame()
    	{
    		Create(NULL, TEXT("Resources Fundamentals"));
    	}
    };
    BOOL CResApp::InitInstance()
    {
    	m_pMainWnd = new CResFrame;
    	TB a;
    	a.Create(&m_pMainWnd);
    	m_pMainWnd->SetMenu(&hMenu);
    	m_pMainWnd->ShowWindow(SW_SHOW);
    	m_pMainWnd->UpdateWindow();
    	return TRUE;
    }
    
    class TB: public CMFCToolBar{
    public:
    	TB(CResFrame *CRF){
    		Create(CRF,CBRS_TOP,IDR_TOOLBAR1);
    	}
    
    }
    CResApp theApp;
    What's wrong?

  2. #2
    Join Date
    Feb 2022
    Posts
    11

    Re: Toolbar in MFC

    Ok, found the include. The following code is compiled without errors but the toolbar is not showed:
    Code:
    #include <afxwin.h>
    #include<afxext.h>
    #include "resource.h"
    class CResApp : public CWinApp
    {
    public:
    	BOOL InitInstance();
    };
    
    //class TB : public CToolBar{};
    
    class CResFrame : public CFrameWnd
    {
    public:
    	CResFrame()
    	{
    		Create(NULL, TEXT("Resources Fundamentals"));
    	}
    };
    BOOL CResApp::InitInstance()
    {
    	m_pMainWnd = new CResFrame;
    	HICON hIcon = LoadIcon(IDI_ICON);
    	HICON hPrev = m_pMainWnd->SetIcon(hIcon, FALSE);
    	CMenu hMenu;
    	hMenu.LoadMenu(IDR_MENU);
    	
    	CToolBar a;
    	a.Create(m_pMainWnd, CBRS_TOP, IDR_TOOLBAR1);
    
    	m_pMainWnd->SetMenu(&hMenu);
    	m_pMainWnd->ShowWindow(SW_SHOW);
    	m_pMainWnd->UpdateWindow();
    	return TRUE;
    }
    
    CResApp theApp;

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Toolbar in MFC

    You don't see your toolbar because it doesn't belong to any frame window.
    Check it out: Toolbars
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2022
    Posts
    11

    Re: Toolbar in MFC

    Quote Originally Posted by VictorN View Post
    You don't see your toolbar because it doesn't belong to any frame window.
    Check it out: Toolbars
    Did it with the following code:

    Code:
    	a.Create(m_pMainWnd, CBRS_TOP, IDR_TOOLBAR1);

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Toolbar in MFC

    Quote Originally Posted by Quasar999 View Post
    Did it with the following code:

    Code:
    	a.Create(m_pMainWnd, CBRS_TOP, IDR_TOOLBAR1);
    With this "code" you only create some toolbar. Nothing else.
    Again: Check it out: Toolbars
    Victor Nijegorodov

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Toolbar in MFC

    1) When I created a toolbar on a dialog, I needed to call LoadToolBar() after Create()

    2)
    Code:
    CToolBar a;
    is a local variable, so it will go out of scope when the function exits. Maybe it needs to be a member variable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured