BCG例子BCGPExplorer:
1.BCG的菜单、工具栏、动画图标和地址栏
这是BCG的主要特色,也比较繁琐。
(1)如果要支持自定义工具。
首先在String表定义入口ID:ID_TOOLS_ENTRY,与某菜单项关联。然后定义连续的ID,如ID_USER_TOOL1、ID_USER_TOOL2......。
在App下增加 EnableUserTools (ID_TOOLS_ENTRY, ID_USER_TOOL1, ID_USER_TOOL10);
(2)主要是修改CMainFrame
头文件声明
CBCGPMenuBar m_wndMenuBar;
CBCGPStatusBar m_wndStatusBar;
CBCGPToolBar m_wndToolBar;
CBCGPReBar m_wndReBar; //菜单、工具栏、地址栏的容器
CBCGPAnimCtrl m_wndAnimate;//动画控件
CComboBoxEx m_wndAddress;//地址栏
增加响应自定义工具的消息函数OnViewCustomize
手动添加消息响应
afx_msg LRESULT OnToolbarReset(WPARAM,LPARAM);
afx_msg LRESULT OnToolbarContextMenu(WPARAM,LPARAM);
虚函数virtual BOOL OnShowPopupMenu (CBCGPPopupMenu* pMenuPopup);
Cpp文件
ON_REGISTERED_MESSAGE(BCGM_RESETTOOLBAR, OnToolbarReset)
ON_REGISTERED_MESSAGE(BCGM_TOOLBARMENU, OnToolbarContextMenu)
OnCreate()函数
CBCGPToolBar::EnableQuickCustomization ();
CBCGPToolBar::SetSizes (CSize (28, 28), CSize (22, 22));//工具栏大小
CBCGPToolBar::SetMenuSizes (CSize (22, 22), CSize (16, 16));//工具栏中下拉菜单项大小
//指定常用的工具,其它会自动收缩。每个下拉(pulldown)菜单条至少要有一项
CList<UINT, UINT> lstBasicCommands;
lstBasicCommands.AddTail (ID_VIEW_TOOLBARS);
lstBasicCommands.AddTail (ID_APP_EXIT);
lstBasicCommands.AddTail (ID_APP_ABOUT);
lstBasicCommands.AddTail (ID_VIEW_TOOLBAR);
lstBasicCommands.AddTail (ID_VIEW_CUSTOMIZE);
lstBasicCommands.AddTail (ID_COMMAND_HISTORY);
lstBasicCommands.AddTail (ID_VIEW_LARGEICON);
lstBasicCommands.AddTail (ID_VIEW_SMALLICON);
lstBasicCommands.AddTail (ID_VIEW_LIST);
lstBasicCommands.AddTail (ID_VIEW_DETAILS);
lstBasicCommands.AddTail (ID_EDIT_CUT);
lstBasicCommands.AddTail (ID_EDIT_COPY);
lstBasicCommands.AddTail (ID_EDIT_PASTE);
CBCGPToolBar::SetBasicCommands (lstBasicCommands);
if (!m_wndMenuBar.Create (this))//菜单的创建
{
TRACE0("Failed to create menubar\n");
return -1; // fail to create
}
m_wndMenuBar.SetBarStyle(m_wndMenuBar.GetBarStyle() | CBRS_SIZE_DYNAMIC);
// Remove menubar gripper and borders:
m_wndMenuBar.SetBarStyle (m_wndMenuBar.GetBarStyle() &
~(CBRS_GRIPPER | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
//动画控件的创建,AFX_IDW_TOOLBAR为标准ID,IDB_ANIMATION是连续位图
if (!m_wndAnimate.Create (_T(""), WS_CHILD | WS_VISIBLE, CRect(0, 0, 20, 20), this, AFX_IDW_TOOLBAR + 2) ||
!m_wndAnimate.SetBitmap (IDB_ANIMATION, 20))
{
TRACE0("Failed to create aimation\n");
return -1; // fail to create
}
m_wndAnimate.Play (500);
//检测颜色深度是256还是真彩色
CClientDC dc (this);
BOOL bIsHighColor = dc.GetDeviceCaps (BITSPIXEL) > 8;
UINT uiToolbarHotID = bIsHighColor ? IDB_TOOLBAR256 : 0; //IDB_TOOLBAR256真彩色图像(用作工具栏)
UINT uiToolbarColdID = bIsHighColor ? IDB_TOOLBARCOLD256 : 0;
UINT uiMenuID = bIsHighColor ? IDB_MENU256 : IDB_MENU16;
if (!m_wndToolBar.CreateEx(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME, uiToolbarColdID, uiMenuID, FALSE, 0, 0, uiToolbarHotID))//IDR_MAINFRAME是256色的预定义工具栏
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// Remove toolbar gripper and borders:
m_wndToolBar.SetBarStyle (m_wndToolBar.GetBarStyle() &
~(CBRS_GRIPPER | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
//创建地址栏
if (!m_wndAddress.Create (CBS_DROPDOWN | WS_CHILD, CRect(0, 0, 200, 120), this, AFX_IDW_TOOLBAR + 1))
{
TRACE0("Failed to create combobox\n");
return -1; // fail to create
}
//将各项加入rebar面板
DWORD dwStyle = RBBS_GRIPPERALWAYS | RBBS_FIXEDBMP | RBBS_BREAK;
if (!m_wndReBar.Create(this) ||
!m_wndReBar.AddBar (&m_wndMenuBar) ||
!m_wndReBar.AddBar (&m_wndToolBar, NULL, NULL, dwStyle) ||
!m_wndReBar.AddBar(&m_wndAnimate, NULL, NULL, RBBS_FIXEDSIZE | RBBS_FIXEDBMP) ||
!m_wndReBar.AddBar(&m_wndAddress, _T("Address"), NULL, dwStyle))
{
TRACE0("Failed to create rebar\n");
return -1; // fail to create
}
m_wndMenuBar.AdjustLayout ();
m_wndToolBar.AdjustLayout ();
//--------------------------------------------------------------
// Set up min/max sizes and ideal sizes for pieces of the rebar:
//--------------------------------------------------------------
REBARBANDINFO rbbi;
CRect rectToolBar;
m_wndToolBar.GetItemRect(0, &rectToolBar);
rbbi.cbSize = sizeof(rbbi);
rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE;
rbbi.cxMinChild = rectToolBar.Width();
rbbi.cyMinChild = rectToolBar.Height();
rbbi.cx = rbbi.cxIdeal = rectToolBar.Width() * m_wndToolBar.GetCount ();
m_wndReBar.GetReBarCtrl().SetBandInfo (1, &rbbi);
rbbi.cxMinChild = 0;
CRect rectAddress;
m_wndAddress.GetEditCtrl()->GetWindowRect(&rectAddress);
rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE;
rbbi.cyMinChild = rectAddress.Height() + 10;
rbbi.cxIdeal = 200;
m_wndReBar.GetReBarCtrl().SetBandInfo (3, &rbbi);//基于上面的AddBar所定的顺序
// 菜单和工具栏允许增加自定义按钮
m_wndMenuBar.EnableCustomizeButton (TRUE, (UINT)-1, _T(""));
m_wndToolBar.EnableCustomizeButton (TRUE, (UINT)-1, _T(""));
EnableDocking (CBRS_ALIGN_ANY);
m_wndReBar.EnableDocking (CBRS_TOP);//可以浮动和停靠
DockControlBar (&m_wndReBar);
CString strMainToolbarTitle;
strMainToolbarTitle.LoadString (IDS_MAIN_TOOLBAR);
m_wndToolBar.SetWindowText (strMainToolbarTitle);
// TODO: Remove this if you don't want tool tips
m_wndMenuBar.SetBarStyle(m_wndMenuBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
void CMainFrame::OnViewCustomize()
{
//------------------------------------
// Create a customize toolbars dialog:
//------------------------------------
CBCGPToolbarCustomize* pDlgCust = new CBCGPToolbarCustomize (this,
TRUE
);
pDlgCust->Create ();
}
LRESULT CMainFrame::OnToolbarContextMenu(WPARAM,LPARAM lp)
{
CPoint point (BCG_GET_X_LPARAM(lp), BCG_GET_Y_LPARAM(lp));
CMenu menu;
VERIFY(menu.LoadMenu (IDR_POPUP_TOOLBAR));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CBCGPPopupMenu* pPopupMenu = new CBCGPPopupMenu;
pPopupMenu->Create (this, point.x, point.y, pPopup->Detach ());
return 0;
}
//替换256色标准工具栏IDR_MAINFRAME为真彩色
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM)
{
UINT uiToolBarId = (UINT) wp;
if (uiToolBarId != IDR_MAINFRAME)
{
return 0;
}
// Replace "Back" and "Forward" buttons by the menu buttons
// with the history lists:
CMenu menuHistory;
menuHistory.LoadMenu (IDR_HISTORY_POPUP);
CBCGPToolbarMenuButton btnBack (ID_GO_BACK, menuHistory,
CImageHash::GetImageOfCommand (ID_GO_BACK), _T("Back"));//带菜单的按钮
btnBack.m_bText = TRUE;
m_wndToolBar.ReplaceButton (ID_GO_BACK, btnBack);
m_wndToolBar.ReplaceButton (ID_GO_FORWARD,
CBCGPToolbarMenuButton (ID_GO_FORWARD, menuHistory,
CImageHash::GetImageOfCommand (ID_GO_FORWARD), _T("Forward")));
// "Folders" button has a text label:
m_wndToolBar.SetToolBarBtnText (m_wndToolBar.CommandToIndex (ID_VIEW_FOLDERS),
_T("Folders"));
// Replace "Views" button by the menu button:
CMenu menuViews;
menuViews.LoadMenu (IDR_VIEWS_POPUP);
m_wndToolBar.ReplaceButton (ID_VIEW_VIEWS,
CBCGPToolbarMenuButton ((UINT)-1, menuViews,
CImageHash::GetImageOfCommand (ID_VIEW_VIEWS), _T("Views")));
return 0;
}
BOOL CMainFrame::OnShowPopupMenu (CBCGPPopupMenu* pMenuPopup)
{
//---------------------------------------------------------
// 将占位的ID_VIEW_TOOLBARS菜单项替换为IDR_POPUP_TOOLBAR:
//---------------------------------------------------------
CFrameWnd::OnShowPopupMenu (pMenuPopup);
if (pMenuPopup != NULL &&
pMenuPopup->GetMenuBar ()->CommandToIndex (ID_VIEW_TOOLBARS) >= 0)
{
if (CBCGPToolBar::IsCustomizeMode ())
{
//----------------------------------------------------
// Don't show toolbars list in the cuztomization mode!
//----------------------------------------------------
return FALSE;
}
pMenuPopup->RemoveAllItems ();
CMenu menu;
VERIFY(menu.LoadMenu (IDR_POPUP_TOOLBAR));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pMenuPopup->GetMenuBar ()->ImportFromMenu (*pPopup, TRUE);
}
return TRUE;
}