【总结】CMFCRibbonStatusBar的用法-创建及响应

本文转自:http://czy.t.blog.163.com/blog/static/1169115482012215112312422/

2012-03-15 11:23:12|  分类: vc记录 |  标签:mfc  vc  vs  vs2010  |字号 订阅

1.创建
见vs2008 fp的例子
 
【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远
 
创建过程\
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"));
//分隔符【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远
    m_wndStatusBar.AddSeparator();
//面板--按钮---所有的按钮在StatusBar里都叫面板【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远
    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的用法-创建及响应 - 尘中远 - 尘中远 -> 
    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( 212212212), hIconSpell), _T( "Spelling and Grammar Check"));
//分隔符
    m_wndStatusBar.AddSeparator();
//刷新按钮的图标【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远
    HICON hIconRefresh  = (HICON)  ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_REFRESH), IMAGE_ICON,  ::GetSystemMetrics(SM_CXSMICON),  ::GetSystemMetrics(SM_CYSMICON), LR_SHARED);
//刷新按钮【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远
    m_wndStatusBar.AddElement( new CMFCRibbonStatusBarPane(ID_STATUSBAR_REFRESH, _T( "Refresh"), FALSE, hIconRefresh), _T( "Refresh"));
//stl的智能指针分配用来创建按钮群【总结】CMFCRibbonStatusBar的用法-创建及响应 - 尘中远 - 尘中远
    std ::auto_ptr <CMFCRibbonButtonsGroup > apSBGroup( new CMFCRibbonButtonsGroup);

    CMFCToolBarImages images1;
    images1.SetImageSize(CSize( 1414));
    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( 0200);
    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();
    }
}
 
拼写动画按钮的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 */);
    }
}
刷新按钮有两个,一个是ON_COMMAND,一个是ON_UPDATE_COMMAND_UI
ON_COMMAND(ID_STATUSBAR_REFRESH, OnRefresh)     
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);
        }
    }
}
 
 
其余的不再详述。

你可能感兴趣的:(【总结】CMFCRibbonStatusBar的用法-创建及响应)