转载自:http://czy.t.blog.163.com/blog/static/1169115482012215112312422/
1.创建
见vs2008 fp的例子
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img1.ph.126.net/ii2OU4W6CvpIaSgFZUq3Rw==/629096572965638281.jpg)
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img8.ph.126.net/sxrmOfazxvaRKu3nScc8Jg==/1334191389625828188.jpg)
创建过程\
BOOL CMainFrame
::CreateStatusBar()
{
if ( !m_wndStatusBar.Create( this))
{
TRACE0( "Failed to create status bar\n");
return FALSE;
}
//创建连接按钮
m_wndStatusBar.AddElement( new CMFCRibbonLinkCtrl(ID_STATUSBAR_LINK, _T( "www.microsoft.com"), _T( "http://www.microsoft.com")), _T( "Link to website"));
//分隔符
m_wndStatusBar.AddSeparator();
//面板--按钮---所有的按钮在StatusBar里都叫面板
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_PAGE, _T( "Page 1")), _T( "Formatted Page Number"));
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_LINE, _T( "Line 1")), _T( "Line Number"));
//分隔符
m_wndStatusBar.AddSeparator();
//创建一个拼写动画按钮
-> ![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img9.ph.126.net/7xh1UqelRn5IhizGyM0YFQ==/2691182252347897949.jpg)
HICON hIconSpell = (HICON) ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SPELL), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_SHARED);
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_SPELL, _T( ""), ( UINT) IDB_SPELL, 16, RGB( 212, 212, 212), hIconSpell), _T( "Spelling and Grammar Check"));
//分隔符
m_wndStatusBar.AddSeparator();
//刷新按钮的图标
HICON hIconRefresh = (HICON) ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_REFRESH), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_SHARED);
//刷新按钮
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_REFRESH, _T( "Refresh"), FALSE, hIconRefresh), _T( "Refresh"));
//stl的智能指针分配用来创建按钮群
std ::auto_ptr <CMFCRibbonButtonsGroup > apSBGroup( new CMFCRibbonButtonsGroup);
CMFCToolBarImages images1;
images1.SetImageSize(CSize( 14, 14));
images1.Load(IDB_LAYOUT);
apSBGroup - >SetImages( &images1, NULL, NULL);
apSBGroup - >SetID(ID_GROUP_SHORTCUTS);
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_PRINTLAYOUT, _T( ""), 0));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_FULL_SCREEN_READING, _T( ""), 1));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_WEBLAYOUT, _T( ""), 2));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_MASTER_DOCUMENT_TOOLS, _T( ""), 3));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_DRAFT, _T( ""), 4));
//按钮群属于ExtendedElement,既是右边的那一栏
m_wndStatusBar.AddExtendedElement(apSBGroup.release(), _T( "View Shortcuts"));
//缩放按钮
m_wndStatusBar.AddExtendedElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_ZOOM, _T( "100%"), FALSE, NULL, _T( "1000%")), _T( "Zoom"));
//Ribbon滑竿按钮
CMFCRibbonSlider * pSlider = new CMFCRibbonSlider(ID_STATUSBAR_ZOOM_SLIDER);
pSlider - >SetZoomButtons();
pSlider - >SetRange( 0, 200);
pSlider - >SetPos( 100);
m_wndStatusBar.AddExtendedElement(pSlider, _T( "Zoom Slider"));
return TRUE;
}
{
if ( !m_wndStatusBar.Create( this))
{
TRACE0( "Failed to create status bar\n");
return FALSE;
}
//创建连接按钮
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://czy.t.blog.163.com/blog/static/1169115482012215112312422/3754062b-8438-45e0-8f64-cbf69526f3e9_files/6100403.png)
m_wndStatusBar.AddElement( new CMFCRibbonLinkCtrl(ID_STATUSBAR_LINK, _T( "www.microsoft.com"), _T( "http://www.microsoft.com")), _T( "Link to website"));
//分隔符
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img5.ph.126.net/KeqFDCLYEkA_PACpGkYGew==/1030761364731743375.jpg)
m_wndStatusBar.AddSeparator();
//面板--按钮---所有的按钮在StatusBar里都叫面板
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img2.ph.126.net/iQy6AFYty7nbngytdQofGw==/1023161540360555592.jpg)
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_PAGE, _T( "Page 1")), _T( "Formatted Page Number"));
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_LINE, _T( "Line 1")), _T( "Line Number"));
//分隔符
m_wndStatusBar.AddSeparator();
//创建一个拼写动画按钮
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img9.ph.126.net/d4krQIyTUj2bDZ_FUPnAxw==/2761550996525569949.jpg)
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img9.ph.126.net/7xh1UqelRn5IhizGyM0YFQ==/2691182252347897949.jpg)
HICON hIconSpell = (HICON) ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SPELL), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_SHARED);
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_SPELL, _T( ""), ( UINT) IDB_SPELL, 16, RGB( 212, 212, 212), hIconSpell), _T( "Spelling and Grammar Check"));
//分隔符
m_wndStatusBar.AddSeparator();
//刷新按钮的图标
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img4.ph.126.net/0lkDRezTv2a51vK_eeD_uQ==/2691463727324608594.jpg)
HICON hIconRefresh = (HICON) ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_REFRESH), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_SHARED);
//刷新按钮
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img8.ph.126.net/768v7m52nFs7L4_LFHu-1Q==/1298725542560283358.jpg)
m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_REFRESH, _T( "Refresh"), FALSE, hIconRefresh), _T( "Refresh"));
//stl的智能指针分配用来创建按钮群
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img7.ph.126.net/NQAGpEEcx7m6e9wYuFov-A==/1283244418841200537.jpg)
std ::auto_ptr <CMFCRibbonButtonsGroup > apSBGroup( new CMFCRibbonButtonsGroup);
CMFCToolBarImages images1;
images1.SetImageSize(CSize( 14, 14));
images1.Load(IDB_LAYOUT);
apSBGroup - >SetImages( &images1, NULL, NULL);
apSBGroup - >SetID(ID_GROUP_SHORTCUTS);
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_PRINTLAYOUT, _T( ""), 0));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_FULL_SCREEN_READING, _T( ""), 1));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_WEBLAYOUT, _T( ""), 2));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_MASTER_DOCUMENT_TOOLS, _T( ""), 3));
apSBGroup - >AddButton( new CMFCRibbonButton(ID_STATUSBAR_DRAFT, _T( ""), 4));
//按钮群属于ExtendedElement,既是右边的那一栏
m_wndStatusBar.AddExtendedElement(apSBGroup.release(), _T( "View Shortcuts"));
//缩放按钮
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img0.ph.126.net/u1SybXZ5eyPKGphk_R_2Dw==/1103381908723066030.jpg)
m_wndStatusBar.AddExtendedElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_ZOOM, _T( "100%"), FALSE, NULL, _T( "1000%")), _T( "Zoom"));
//Ribbon滑竿按钮
![【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远](http://img6.ph.126.net/o29reWOFHkMx-EaHV0uIGg==/2655434930305646196.jpg)
CMFCRibbonSlider * pSlider = new CMFCRibbonSlider(ID_STATUSBAR_ZOOM_SLIDER);
pSlider - >SetZoomButtons();
pSlider - >SetRange( 0, 200);
pSlider - >SetPos( 100);
m_wndStatusBar.AddExtendedElement(pSlider, _T( "Zoom Slider"));
return TRUE;
}
\
Ribbon的很多东西new之后都不用delete,它在析构时自动释放
创建他们之后解决的就是消息响应的问题了
大部分都是ON_COMMAND响应
首先是链接按钮的响应ON_COMMAND
头文件:
afx_msg
void OnLink();
消息响应列表
ON_COMMAND(ID_STATUSBAR_LINK, OnLink)
响应函数
void CMainFrame
::OnLink()
{
CMFCRibbonLinkCtrl * pLink = (CMFCRibbonLinkCtrl *) m_wndStatusBar.FindByID(ID_STATUSBAR_LINK);
if (pLink != NULL)
{
pLink - >OpenLink();
}
}
{
CMFCRibbonLinkCtrl * pLink = (CMFCRibbonLinkCtrl *) m_wndStatusBar.FindByID(ID_STATUSBAR_LINK);
if (pLink != NULL)
{
pLink - >OpenLink();
}
}
拼写动画按钮的ON_COMMAND响应OnSpell()
void CMainFrame
::OnSpell()
{
CMFCRibbonStatusBarPane * pPane = DYNAMIC_DOWNCAST(CMFCRibbonStatusBarPane, m_wndStatusBar.FindElement(ID_STATUSBAR_SPELL));
if (pPane == NULL)
{
return;
}
if (pPane - >IsAnimation())
{
pPane - >StopAnimation();
}
else
{
pPane - >StartAnimation( 500 /* 1/2 sec, Frame delay */, 10000 /* 10 sec, animation duration */);
}
}
{
CMFCRibbonStatusBarPane * pPane = DYNAMIC_DOWNCAST(CMFCRibbonStatusBarPane, m_wndStatusBar.FindElement(ID_STATUSBAR_SPELL));
if (pPane == NULL)
{
return;
}
if (pPane - >IsAnimation())
{
pPane - >StopAnimation();
}
else
{
pPane - >StartAnimation( 500 /* 1/2 sec, Frame delay */, 10000 /* 10 sec, animation duration */);
}
}
刷新按钮有两个,一个是ON_COMMAND,一个是ON_UPDATE_COMMAND_UI
ON_COMMAND(ID_STATUSBAR_REFRESH, OnRefresh)
ON_UPDATE_COMMAND_UI(ID_STATUSBAR_REFRESH, OnUpdateRefresh)
ON_UPDATE_COMMAND_UI(ID_STATUSBAR_REFRESH, OnUpdateRefresh)
void CMainFrame
::OnRefresh()
{
m_bInfiniteProgressMode = FALSE;
ShowProgress();
}
void CMainFrame ::ShowProgress( BOOL bShow)
{
if (bShow)
{
int cxFree = m_wndStatusBar.GetSpace();
if (cxFree < 20)
{
// Not enough space for progress bar
return;
}
int cxProgress = min(cxFree, 150);
CMFCRibbonProgressBar * pProgressBar = new CMFCRibbonProgressBar(ID_STATUSBAR_PROGRESS, cxProgress);
pProgressBar - >SetInfiniteMode(m_bInfiniteProgressMode);
m_wndStatusBar.AddDynamicElement(pProgressBar); //动态添加内容
m_nProgressValue = 0;
SetTimer(IdShowProgressTimer, 10, NULL);
}
else
{
KillTimer(IdShowProgressTimer);
m_wndStatusBar.RemoveElement(ID_STATUSBAR_PROGRESS);
m_nProgressValue = - 1;
}
m_wndStatusBar.RecalcLayout();
m_wndStatusBar.RedrawWindow();
CMFCPopupMenu ::UpdateAllShadows();
}
void CMainFrame ::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == IdShowProgressTimer)
{
m_nProgressValue ++;
if (m_nProgressValue > 100)
{
ShowProgress(FALSE);
}
else
{
CMFCRibbonProgressBar * pProgress = DYNAMIC_DOWNCAST(CMFCRibbonProgressBar, m_wndStatusBar.FindElement(ID_STATUSBAR_PROGRESS));
ASSERT_VALID(pProgress);
pProgress - >SetPos(m_nProgressValue, TRUE);
}
}
}
{
m_bInfiniteProgressMode = FALSE;
ShowProgress();
}
void CMainFrame ::ShowProgress( BOOL bShow)
{
if (bShow)
{
int cxFree = m_wndStatusBar.GetSpace();
if (cxFree < 20)
{
// Not enough space for progress bar
return;
}
int cxProgress = min(cxFree, 150);
CMFCRibbonProgressBar * pProgressBar = new CMFCRibbonProgressBar(ID_STATUSBAR_PROGRESS, cxProgress);
pProgressBar - >SetInfiniteMode(m_bInfiniteProgressMode);
m_wndStatusBar.AddDynamicElement(pProgressBar); //动态添加内容
m_nProgressValue = 0;
SetTimer(IdShowProgressTimer, 10, NULL);
}
else
{
KillTimer(IdShowProgressTimer);
m_wndStatusBar.RemoveElement(ID_STATUSBAR_PROGRESS);
m_nProgressValue = - 1;
}
m_wndStatusBar.RecalcLayout();
m_wndStatusBar.RedrawWindow();
CMFCPopupMenu ::UpdateAllShadows();
}
void CMainFrame ::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == IdShowProgressTimer)
{
m_nProgressValue ++;
if (m_nProgressValue > 100)
{
ShowProgress(FALSE);
}
else
{
CMFCRibbonProgressBar * pProgress = DYNAMIC_DOWNCAST(CMFCRibbonProgressBar, m_wndStatusBar.FindElement(ID_STATUSBAR_PROGRESS));
ASSERT_VALID(pProgress);
pProgress - >SetPos(m_nProgressValue, TRUE);
}
}
}