Click here to Skip to main content
13,354,132 members (58,380 online)
Click here to Skip to main content
Add your own
alternative version

Stats

190K views
4.1K downloads
66 bookmarked
Posted 3 Dec 2001

A ToolBarCtrl Based Menubar Control

, 3 Dec 2001
Rate this:
Please Sign up or sign in to vote.
A ToolBarCtrl based menubar control using CMenuXP
<!-- Download Links --> <!-- Article image -->

Sample Image - MenuBarXP.jpg

<!-- Add the rest of your HTML here -->

Introduction

This menubar control is based on the CToolBarCtrl class rather than CToolBar, so it never assumes that it has a parent window derived from CFrameWnd. It lacks some features like docking or automatic tooltips, but it can be used with any window whose parent is derived from CWnd. You must of course be using MFC.

Implementation

Between CMenuBar and CToolBarCtrl, I wrote a class named CToolBarXP to process customdraw. If you don't need this feature, just modify the base class as CToolBarCtrl, and remove the following line from the messagemap:

ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, CToolBarXP::OnCustomDraw)

CToolBarXP includes a LoadToolBar member function to load a toolbar resource. It may be useful to you, even if you don't care about menubars or customdraw.

CMenuXP is used as the menu class to implement ownerdraw. CMenuXP is derived from CMenu so it has all the features of CMenu. If you don't need a ownerdraw menu, just use the CMenu members to construct the menu.

The included sample is simple and far from powerful, but it is a good start I hope.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Neil Yao
China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

You may also be interested in...

Pro

Comments and Discussions

 
Generalctoolbarxp didn't display text Pin
wanglinhai88821-Feb-10 21:16
memberwanglinhai88821-Feb-10 21:16 
GeneralSubMenu Bug Pin
benjamin2321-May-08 5:39
memberbenjamin2321-May-08 5:39 
GeneralApp suddenly exits Pin
Chris982121-Apr-08 9:59
memberChris982121-Apr-08 9:59 
GeneralRe: App suddenly exits Pin
hujian_19827-Sep-09 20:39
memberhujian_19827-Sep-09 20:39 
QuestionSEPARATOR bug Pin
lihe20057-Sep-06 21:16
memberlihe20057-Sep-06 21:16 
GeneralSome improvement (XP themes) Pin
AlexXF24-Nov-04 8:29
memberAlexXF24-Nov-04 8:29 
GeneralRe: Some improvement (XP themes) Pin
Frank Overcast22-Jan-06 16:40
memberFrank Overcast22-Jan-06 16:40 
GeneralCatch Click Events Pin
naumankhan15-Apr-04 22:44
membernaumankhan15-Apr-04 22:44 
GeneralRe: Catch Click Events Pin
Yao Zhifeng15-Apr-04 22:59
memberYao Zhifeng15-Apr-04 22:59 
GeneralRe: Catch Click Events Pin
jagan_gadela21-Apr-04 2:33
memberjagan_gadela21-Apr-04 2:33 
GeneralRe: Catch Click Events Pin
jagan_gadela21-Apr-04 2:58
memberjagan_gadela21-Apr-04 2:58 
I Will Try To Explain what I Did.

My Intention is to create the Tool Bar for a button.

So I Have taken CButtonST class for my requirements

In CButtonST Class declarations I added following code

//class declarations
CMenu *pMenuX ;
void AddButtonMenu(CMenu* pMenu);
// In Definations I did like this

void CButtonST::AddButtonMenu(CMenu *pMenu)
{

if(pMenuX!=NULL)
{
if(pMenuX->m_hMenu && pMenuX!=NULL)
{
pMenuX->DestroyMenu ();
}
}
pMenuX=NULL;
if(pMenuX==NULL)
{
pMenuX=pMenu;
}
}
And I have Modified OnClcked Method by Adding following code to it

if(pMenuX && pMenuX->m_hMenu )
{
CRect rWnd;
GetWindowRect(rWnd);
Invalidate();
CMenu* psub = (CMenu*)pMenuX->GetSubMenu(0);
if (m_csCallbacks.hWnd)
::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)psub, m_csCallbacks.lParam);
//::SendMessage(this->m_hWnd, m_csCallbacks.nMessage, (WPARAM)psub, m_csCallbacks.lParam);
DWORD dwRetValue = psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, this, NULL);
Invalidate();

return TRUE;
}


This is How I Have modified the code in CButtonST class


And In My Application I tried to work like this
I created a method called CreateToolsButtonMenu

CMenu* CXmlparseDlg::CreateToolsButtonMenu()
{


//You can unrecomment the next two line to load a normal menu
//pMenu->LoadMenu(IDR_MAINMENU);
//return pMenu;



CMenuXP *pPopup;
pPopup = new CMenuXP1;
pPopup->CreatePopupMenu();

CMenuXP *pButtons = new CMenuXP1;
pButtons->CreatePopupMenu();
pButtons->SetBackColor(RGB(0xf2, 0xf2, 0xf2));
pButtons->SetSelectedBarColor(RGB(0x00, 0xcc, 0xcc));
pButtons->SetMenuStyle(CMenuXP1::STYLE_XP);

pButtons->Break();
pButtons->AppendODMenu(0, new CMenuXP1Button(ID_TOOLS_INTERNETEXPLORER, AfxGetApp()->LoadIcon(IDI_EXPLORER)));
pButtons->Break();


pPopup->AppendODPopup(0, pButtons, new CMenuXPText(203, "Buttons"));

return pPopup;
}

and in OnInitDialog method I created button like this

m_btnTools.SetFlat(1);
m_btnTools.SetAlign(CButtonST::ST_ALIGN_VERT);
m_btnTools.SetIcon (IDI_TOOLS );
m_btnTools.EnableBalloonTooltip();
m_btnTools.SetTooltipText(_T("Executes the third party applications"));
m_btnTools.AddButtonMenu(CreateToolsButtonMenu());


After doing these modifications with yours and other expert Davide Calabro's code(Source : http://www.codeproject.com/buttonctrl/cbuttonst.asp) I was able to see your tool bar wth Davide's Button. But Only Problem I have faced is I am not able to catch the WM_COMMAND messages

Regards


hfg h hfgh hgfh
GeneralRe: Catch Click Events Pin
Yao Zhifeng21-Apr-04 5:10
memberYao Zhifeng21-Apr-04 5:10 
GeneralRe: Catch Click Events Pin
jagan_gadela22-Apr-04 3:05
memberjagan_gadela22-Apr-04 3:05 
GeneralRe: Catch Click Events Pin
Yao Zhifeng23-Apr-04 0:12
memberYao Zhifeng23-Apr-04 0:12 
GeneralRe: Catch Click Events Pin
Jim Tomasko24-Apr-04 23:38
memberJim Tomasko24-Apr-04 23:38 
Questionpossible to use this on SDI??? Pin
Hirosh16-Feb-03 21:57
memberHirosh16-Feb-03 21:57 
GeneralPretty cool, only one thing though Pin
Anonymous11-May-02 1:50
memberAnonymous11-May-02 1:50 
Questionhow to change the color of the toolbar? Pin
Anonymous1-May-02 20:40
memberAnonymous1-May-02 20:40 
GeneralI have a problem!!! Pin
CSZX3-Apr-02 1:48
memberCSZX3-Apr-02 1:48 
GeneralDoesn't work with dropdown arrow. Pin
Duc Truong14-Jan-02 15:23
memberDuc Truong14-Jan-02 15:23 
GeneralChanging background color of Menu Pin
Ritesh2321-Dec-01 23:36
memberRitesh2321-Dec-01 23:36 
GeneralGood job Pin
Cool_worm19-Dec-01 2:37
memberCool_worm19-Dec-01 2:37 
GeneralMenu icon lost itself when mousemove on the popup menu items Pin
Golden Lee10-Dec-01 16:46
memberGolden Lee10-Dec-01 16:46 
GeneralGetParent Pin
orcun colak6-Dec-01 18:44
memberorcun colak6-Dec-01 18:44 
GeneralRe: GetParent Pin
Yao Zhifeng7-Dec-01 5:13
memberYao Zhifeng7-Dec-01 5:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 4 Dec 2001
Article Copyright 2001 by Neil Yao
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid