原創|其它|編輯:郝浩|2012-09-20 14:44:29.000|閱讀 3368 次
概述:本文主要介紹如何使用BCG的菜單、工具欄、動畫圖標和地址欄。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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; }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園