手把手教你BCGControlBar MFC界面控件“起航”技巧(文章转载自:慧都控件网)

BCGControlBar是全球最大的MFC控件套包,由微软指定合作控件开发商BCGSoft公司开发,主要用于构建类似Microsoft® Office 2000/XP/2003/2007/2010、Microsoft Visual Studio(打印、用户定制工具栏、菜单等)和其他一些知名产品的高级用户界面,例如:日历、网格、编辑和甘特图等。

BCGControlBar下载

BCGControlBar安装好后,在开始使用前,你有一些初始化设置和使用技巧需要了解一下。

首先,请你按以下内容修改源码:

  • 确定在你的应用程序InitInstance()中调用了AfxOleInit()
  • 在你的应用程序ExistInstance()中添加一个调用BCGCBProCleanUp()
  • 在stdafx.h文件中添加如下内容: 
#include "BCGCBProInc.h"

有关库版本将自动链接到你的项目中。

  • 添加CBCGWorkspace-derivation到你的程序类中: 
class CMyApp : public CWinApp,
public CBCGPWorkspace
  • 首先,你需要确定你的自定义数据将被保存在注册表中哪个地方,哪些功能是需要自定义的(如鼠标,键盘,上下文菜单)。要做到这一点,你需要在CMyApp :: InitInstance中设置注册表项和初始化定制管理器: 
SetRegistryBase (_T("Settings"));

// 初始化定制管理器:
InitMouseManager();
InitContextMenuManager();
InitKeyboardManager();
  • 假如你决定使用鼠标或上下文菜单的自定义功能,你需要在鼠标自定义管理器中附加一个视图,或初始化上下文菜单。重载CBCGWorkspace::PreLoadState方法:
class CMyApp ....
{
...
virtual void PreLoadState();
...
};

void CMyApp::PreLoadState()
{
// 把鼠标事件连接到特定的视图 :
GetMouseManager()->AddView (iIdTestView, _T("Test view"), IDR_VIEW);

// 初始化上下文菜单:
GetContextMenuManager()->AddMenu (_T("Test menu"), idMenu); 
}
  • 同时在mainfrm.h和mainfrm.cpp文件中把CMDIFrameWnd变成CBCGMDIFrameWnd(在SDI程序的情况下,把CFrameWnd变成CBCGFrameWnd)
  • CMDIChildWnd变成CBCGMDIChildWnd
  • CToolbar变成CBCGToolBar并添加一个内嵌菜单栏对象到你的CMainFrame类中:
CBCGPMenuBar    m_wndMenuBar;     // 新菜单栏
CBCGPToolBar      m_wndToolBar;      // 应用程序工具栏
  • 启用菜单栏功能需要在CMainFrame::OnCreate()方法中添加如下的行:
// 创建菜单栏(替换标准菜单栏):
if (!m_wndMenuBar.Create (this))
{
TRACE0("Failed to create menubar\n");
return -1;      // 创建失败
}

m_wndMenuBar.SetBarStyle (m_wndMenuBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

  • 启用菜单栏停靠功能,只需添加如下代码:T
m_wndMenuBar.EnableDocking (CBRS_ALIGN_ANY);
DockControlBar (&m_wndMenuBar);


提示:
你可以在程序中使用任意数量的CBCGToolBar工具栏。所有的工具栏图像将被合并到一个位图文件中。无论如何,只有一个CBCGMenuBar对象被使用。

启用工具栏/菜单自定义功能,请做如下修改:

  • 添加工具栏工具栏/菜单自定义命令 (例如,View | Customize...)
  • OnViewCustomize方法的实现。代码看起来如下:
void CMainFrame::OnViewCustomize()
{
// 创建一个自定义工具栏对话框:
CBCGPToolbarCustomize* pDlgCust = new CBCGPToolbarCustomize (this,
TRUE /* Automatic menus scaning */);

 // 添加预定义工具栏:
pDlgCust->AddToolBar ("Main", IDR_MAINFRAME);
....

 // 添加用户自定义命令:
pDlgCust->AddButton ("User", CBCGPToolbarButton (ID_USER_TOOL1, 1, "User Tool 1", TRUE));
pDlgCust->AddButton ("User", CBCGPToolbarButton (ID_USER_TOOL2, 2, "User Tool 2", TRUE));
pDlgCust->AddButton ("User", CBCGPToolbarButton (ID_USER_TOOL3, 3, "User Tool 3", TRUE));
....

 pDlgCust->SetUserCategory ("User");

 // 启用用户自定义工具栏的 创建/删除:
pDlgCust->EnableUserDefinedToolbars ();

 pDlgCust->Create ();
}

启用Microsoft Office 2000菜单:

  • 定义你自己的基本命令集合(通常在CMainFrame::OnCreate()中):
CList<UINT, UINT> lstBasicCoomads;

lstBasicCoomads.AddTail (ID_FILE_NEW);
lstBasicCoomads.AddTail (ID_FILE_OPEN);
lstBasicCoomads.AddTail (ID_FILE_SAVE);

......
lstBasicCoomads.AddTail (ID_APP_ABOUT);

CBCGPToolBar::SetBasicCommands (lstBasicCoomads);

  • 这些命令将显示在下拉菜单中。

以下代码可以实现运行时改变菜单字体:

CBCGPMenuBar::SetMenuFont (LPLOGFONT lpLogFont, BOOL bHorz = TRUE);


只需调用即可实现“页面”(自定义)按钮:

m_wndToolBar.EnableCustomizeButton (TRUE, id_of_customize_command,
_T("Customize..."));


以下代码可以实现按钮图像覆盖文字:

m_wndToolBar.EnableTextLabels (BOOL bEnable = TRUE);


实现用户自定义工具栏:

  • 添加一个新的菜单列表:ID_TOOLS_ENTRY。这个列表将被实际存在的工具栏列表自动替换。
  • 添加如下字符串资源列表:
    1. ID_TOOL1    "Activates user-defined tool\nUser Tool"
    2. ID_TOOL2    "Activates user-defined tool\nUser Tool"
      ....
    3. ID_TOOLx    "Activates user-defined tool\nUser Tool"
  • 在应用程序中调用InitInstance():

    EnableUserTools (ID_TOOLS_ENTRY, ID_TOOL1,    ID_TOOLx);
  • 一个新的"Tools"页面将被添加到自定义对话框中  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值