The CExtMenuControlBar
class is derived from CExtToolControlBar,
i.e. the menu bar is an improved version of the toolbar. All buttons both in the menu bar and in the toolbar are instances of the CExtBarButton
class or derived from it. So, you need just to append the X button to the end of menu bar’s button list and shift its position to the right side of the menu bar.
First of all, use a CExtMenuControlBar
-derived class and implement the _UpdateMenuBar()
virtual method in it. This method builds the content of the menu bar. Your method should invoke the parent method and append the X button using the following code:
#define ID_X_BUTTON 10000 // some unique ID
. . .
BOOL CYourMenuBar::_UpdateMenuBar( // update after menu changed
BOOL bDoRecalcLayout
)
{
if( ! CExtMenuControlBar::_UpdateMenuBar( bDoRecalcLayout ) )
return FALSE;
// Ensure the ID_X_BUTTON command is registered in the command manager
LPCTSTR strCmdProfileName = g_CmdManager->ProfileNameFromWnd( m_hWnd );
CExtCmdItem * pCmdItem = g_CmdManager->CmdGetPtr( strCmdProfileName, ID_X_BUTTON );
if( pCmdItem == NULL )
{
pCmdItem = g_CmdManager->CmdAllocPtr( strCmdProfileName, ID_X_BUTTON );
ASSERT( pCmdItem != NULL );
pCmdItem;
HICON hIconX = (HICON)::LoadImage( MAKEINTRESOURCE(IDR_ICON_X_16x16), IMAGE_ICON, 16,16, 0 );
ASSERT( hIconX != NULL );
g_CmdManager->CmdSetIcon( strCmdProfileName, ID_X_BUTTON, hIconX, false );
}
// insert the "X" button to the end of buttons list
CExtToolControlBar::InsertButton( -1, ID_X_BUTTON 10000, bDoRecalcLayout );
return TRUE;
}
Now we have the
X button appended to the button list and you can add a command handler and/or a command updating method for the
ID_X_BUTTON
command. To make the
X button align right, just override the
CExtMenuControlBar::_RecalcPositionsImpl()
virtual method in this way:
void CYourMenuBar::_RecalcPositionsImpl()
{
CExtMenuControlBar::_RecalcPositionsImpl();
INT nPositionOfX = CExtToolControlBar::CommandToIndex( ID_X_BUTTON );
if( nPositionOfX < 0 )
return;
CExtBarButton * pTBBX = GetButton( nPositionOfX );
CRect rcClient, rcX = *pTBBX;
GetClientRect( &rcClient );
rcX.OffsetRect( rcClient.right - rcX.right + 3, 0 )
pTBBX->SetRect( rcX );
}