How to change toolbar runtime

I  have  two toolbars  in my application.I  want  to change  toolbar  runtime. Inplace  of  first  toolbar  I  want  to  replace  second toolbar.
and vice versa  based  on  some  condition.How  to  do  it? Will  anyone  tell me  as  early as possible.
I  am  using  Visual C++ 2010 . And  my  toolbar  is CMFCToolbar.
ADITYA RAOSoftware DeveloperAsked:
Who is Participating?
 
sarabandeCommented:
if you have an sdi or mdi mfc project you find the code for creating and loading the toolbar in mainfrm.h and mainfrm.cpp.

to be able to exchange the toolbar i would suggest to turn the member 'CMfcToolbar m_wndToolBar' to pointer member 'CMfcToolbar * m_pWndToolBar'.

in mainfrm.h do

class CMainFrame ...
{
    ...
    //CMfcToolbar m_wndToolBar;
    CMfcToolbar * m_pWndToolBar;
    CMfcToolbar * m_pWndToolBar1;
    CMfcToolbar * m_pWndToolBar2;


in mainfrm.cpp do:

CMainFrame::CMainCMfcToolbar m_wndToolBarFrame()
    : m_pWndToolBar(NULL), m_pWndToolBar1(NULL), m_pWndToolBar2(NULL)
{
     ...


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    ...
        m_pWndToolBar = m_pWndToolBar1 = new CMfcToolBar();
	if (!m_pWndToolBar->CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_pWndToolBar->LoadToolBar(theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME))
	{
             ...
 
	m_pWndToolBar->SetWindowText(strToolBarName);
        ...
        m_pWndToolBar->EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize);        
        ...
        m_pWndToolBar->EnableDocking(CBRS_ALIGN_ANY);
        ... 
        DockPane(m_pWndToolBar);

Open in new window



if done so far, the mainframe class now has a toolbar which could be exchanged by a second toolbar.

you would add a new function to CMainFrame which would hide the current toolbar 1 and create a second toolbar 2 by using the same functions as it was done for the first toolbar. store the pointer in m_pWndToolBar2 and visualize it by call ShowWindow. after that you should have two valid toolbars which you simply could switch by hiding the one and showing the other (you may need to add some additional code for docking and undocking the toolbar correctly).

Sara
0
 
Randy DownsOWNERCommented:
Maybe this will help.

new MFC program with the wizard in VS-2010 you can choose this feature on the page "User Interface Features". Select "Use a menu bar and toolbar. There are two further settings "User-defined toolbars and images" and "Personal menu behavior". You have to select the first.

Look into the created code...

HTH

PS: Also the new BCG classes are completely compatible with MFC 2010
0
 
ADITYA RAOSoftware DeveloperAuthor Commented:
Yes   Mr/Mrs  sarabande  it  finally  worked  out.Toolbar is changing
Thank  you  very  very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.