Print Page | Close Window

How to create a fixed toolbar

Printed From: Codejock Forums
Category: General
Forum Name: Articles and Tutorials
Forum Description: Articles and Tutorials
Printed Date: 29 February 2024 at 12:12am
Software Version: Web Wiz Forums 12.04 -

Topic: How to create a fixed toolbar
Posted By: elmue
Subject: How to create a fixed toolbar
Date Posted: 05 June 2010 at 8:13pm

I wanted to create a toolbar that is neither movable, nor floatable nor closeable by the user.
I used the sample "GUI_VisualStudio".

What I wanted was a toolbar like this:

To achieve that remove all the code from CMainFrame::OnCreate() that contains pCommandBar.

Insert this code into the header file MainFrm.h:

    CXTPToolBar* mp_MainToolBar;

Insert this code into the file MainFrm.cpp:

    CXTPCommandBars* pCommandBars = GetCommandBars();

    mpMainToolBar = (CXTPToolBar*)pCommandBars->Add(_T("Standard"), xtpBarTop);
    if (!mp_MainToolBar || !mp_MainToolBar->LoadToolBar(IDR_MAINFRAME))
        return -1;

    // disallow floating
    mp_MainToolBar->SetFlags(0, xtpFlagFloating|xtpFlagAlignTop|xtpFlagAlignLeft|xtpFlagAlignRight|xtpFlagAlignBottom);

    // remove the menu that appears when right clicking the toolbar

    // avoid that the toolbar can be closed

    // remove gripper at the left side

    // remove the drop down menu at the right side

    // if you like the text below the icon instead of a tooltip (optional)
//    mp_MainToolBar->ShowTextBelowIcons(TRUE);

That's it !


If you want additionally that the toolbar has 100% width of the window, add this to the header file:

    virtual void RecalcLayout(BOOL bNotify);

And this to the CPP file:

    mp_MainToolBar = 0;

void CMainFrame::RecalcLayout(BOOL bNotify)

    if (mp_MainToolBar && mp_MainToolBar->GetSafeHwnd())
        CRect k_Wnd;
        CRect k_Bar;
        mp_MainToolBar->MoveWindow(0, 0, k_Wnd.Width()-2, k_Bar.Height());

Up to now I'm still searching without success how to remove ONLY the menu "Add or remove buttons"
WITHOUT removing the entire menu at the right side that appears when not all icons fit into the toolbar.

I solved it partially, but after an entire day I still don't have what I wanted.

I hope that in the future there will be a tutorial available that is like mine here and explains how to do things like these!
This tutorial must explain the main features !with screenshots! - !NOT ONLY TEXT! and explain how to use this huge toolkit.


Posted By: SuperMario
Date Posted: 07 June 2010 at 2:41pm
You need something like this for MFC version (untested code)

afx_msg void OnInitCommandsPopup(CXTPPopupBar* pComandBar);
void CMainFrame::OnInitCommandsPopup(CXTPPopupBar* pCommandBar)
    // get the list of commands for the popup.
    CXTPControls* pCommandList = pCommandBar->GetControls();

    // Remove "Add\Remove" menu item from the File menu.
    CXTPControl* pCommandNew = pCommandList->FindControl(
    if (pCommandNew)

FYI, for #2 you just need xtpFlagStretched or xtpFlagStretchedShared

Posted By: elmue
Date Posted: 07 June 2010 at 11:59pm
Hello Mario

Thanks for you help.

In your code pCommandList->FindControl(..) returns 0.

Instead of
it must be

This will successfully remove the menu entry "Add or remove buttons".

But the result is a little strange: An empty menu opens when all icons fit into the toolbar.

It would be better if nothing would open at all or even better:
if the little arrow button would not appear in the toolbar when all icons fit into the toolbar.


Your second suggestion doesn't work:

I tried to set the flag xtpFlagStretched with
and with
as described in the CommandReference.
Both have no effect.


Posted By: SuperMario
Date Posted: 08 June 2010 at 3:07pm
My solution assumed you did not want the expand button, so you would never see the blank popup.  So do you want this button or not?  Sounds like you need to hide the button for the toolbar and use my code, then it will have only not viewable buttons when popup appears.

So ShowExpandButton(FALSE) should be False, then use my code and you have what you were looking for. 

Add this line in Visual Studio GUI sample and you will see streched toolbar:

    CXTPToolBar* pThemeBar = (CXTPToolBar*)pCommandBars->Add(_T("Theme"), xtpBarTop);
    if (!pThemeBar ||
        TRACE0("Failed to create toolbar\n");
        return -1;

    pThemeBar->EnableDocking(xtpFlagStretchedShared); //<<<<<<<<<<<<<<<<<<<<<<<<< This line

Posted By: elmue
Date Posted: 08 June 2010 at 8:49pm
Hello Mario

Would it be possible that you try your code before posting here ?
Please *?*

>   pThemeBar->EnableDocking(xtpFlagStretchedShared);

This is EXACTLY what I did after your last posting.
And it defintiely does not change anything !

The toolbar stays the same size and does NOT fill 100% of the width of the window as my code does in RecalcLayout().


> So ShowExpandButton(FALSE) should be False, then use my code and you have what you were looking for.

This is one of the very first things that I tried and it does not do what you assume !

Look at this screenshot:

There are two symbols in the toolbar.
The upper one appears only if there are icons that don't fit into the toolbar, otherwise it disappears automatically.
The lower one is always there if ShowExpandButton(TRUE).

It would be perfect if I could turn them off separately and if I could define if I want to see "Add or remove buttons" in the menu.
But this is impossible.

ShowExpandButton(FALSE) turns them BOTH COMPLETELY off and FOR EVER!

This means even if there are icons that don't fit into the toolbar there will be no symbol visible at all that the user could click on.

So up to now I did not find how to do what I want.
I think this is a missing feature.

Why isn't there an option like this:

ShowExpandButton(xtpCommandButtonMoreIcons | xtpCommandButtonCustomizeMenu)

that would allow me to turn on and off individually what I want.


Would you please try it before posting an answer ?


Posted By: lfoster
Date Posted: 09 June 2010 at 9:04am
Thank you SuperMario and Elmu for this solution. I think there is some confusion still as to what we are trying to achieve. SuperMario's suggestion of setting ShowExpandButton(FALSE) just removes the whole button which is not what we want.

I have the drop down which correctly displays any buttons that do not fit on the toolbar. I still have the empty drop down though even when everything already fits on the toolbar.

Ideally the drop down would have nothing in it if everything could be squeezed onto the toolbar instead of the single blank entry but I can live with this.

Thanks again guys,


Posted By: SuperMario
Date Posted: 09 June 2010 at 4:25pm
Trust me, the code works, it will make toolbar take up 100% of the remaining space available


Posted By: elmue
Date Posted: 10 June 2010 at 10:18am
Hello Mario

Thanks for the screenshot.
It shows me that we are talking about two different things.

You made the Themebar wider.
I am surprised that this functions.
I can reproduce your screenshot.

The same code applied to the toolbar, that I am talking about, (the command bar) STILL does NOT work.

Modify your line

And you will see that the upper bar stays unchanged !

I also tried SetFlags() which doesn't work either.

Why does the code work for one toolbar but not for another one ?
Is this a bug ?


Posted By: SuperMario
Date Posted: 11 June 2010 at 10:32am
my code works for all toolbars as seen below, maybe you load some old layout after setting this flag?  try to comment out any load settings\layout?
        //CXTPPropExchangeSection pxNormalLayout(px.GetSection(_T("NormalLayout")));

Posted By: elmue
Date Posted: 12 June 2010 at 12:02pm
Hello Mario

Yes you are right.
I deleted the file GUI_VisualStudio_vc60.xml.
And now the flag works.

When changing the code you must always delete this XML file or disable the code the loads the configuration otherwise any change at the code is ignored and the old configuration is loaded from the XML file.

But it makes a big difference if you use either my code in RecalcLayout or alternately the flag Stretched.
This difference becomes visible when you resize the window:


Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 -
Copyright ©2001-2021 Web Wiz Ltd. -