Programming Windows with MFC - Chapter 12 Toolbars

 

Chapter 12 Toolbars, Status Bars, and Rebars

一、工具栏

    CToolBar::GetToolBarCtrl()

    1、Creating and Initializing a Toolbar

       (1) CToolBar::Create also accepts an optional third parameter specifying the toolbar ID. The default is AFX_IDW_TOOLBAR.There's no need to change the toolbar ID unless you write an application that contains two or more toolbars
           
m_wndToolBar.Create (this, WS_CHILD ¦ WS_VISIBLE ¦ CBRS_BOTTOM);

            m_wndToolBar.
Create (this);
            m_wndToolBar.
SetBarStyle ((m_wndToolBar.GetBarStyle () &
            ~CBRS_TOP) ¦ CBRS_BOTTOM);
        (2)You can change both the image size and the button size with CToolBar::SetSizes()

  (3)
    m_wndToolBar.Create (this);
    m_wndToolBar.LoadBitmap (IDR_TOOLBAR);
    m_wndToolBar.SetButtons (nButtonIDs, 10);

    m_wndToolBar.Create (this);
    m_wndToolBar.
LoadToolBar (IDR_TOOLBAR);

    (4)add text strings to the faces of the buttons with CToolBar::SetButtonText.
        SetSizes must be called after the button text is added, not before, or the button sizes won't stick。Also, the width of the button bitmaps must be expanded to make room for the button text.

    (5)
int nState =m_wndToolBar.GetToolBarCtrl ().GetState (ID_PARA_LEFT);
m_wndToolBar.GetToolBarCtrl ().SetState (ID_PARA_LEFT, nState ¦TBSTATE_CHECKED);

    (6)CCmdUI::SetCheck() SetButtonStyle() 强。

2、Docking and Floating

   (1) Floating and docking are enabled by calling the toolbar's EnableDocking function ( CControlBar::EnableDocking) with bit flags specifying which sides of the frame window the toolbar will allow itself to be docked to and by calling the frame window's EnableDocking function ( CFrameWnd::EnableDocking) with bit flags specifying which sides of the window are valid docking targets.

Bit FlagDescription
CBRS_ALIGN_LEFTPermit docking to the left side of the frame window
CBRS_ALIGN_RIGHTPermit docking to the right side of the frame window
CBRS_ALIGN_TOPPermit docking to the top of the frame window
CBRS_ALIGN_BOTTOMPermit docking to the bottom of the frame window
CBRS_ALIGN_ANYPermit docking to any side of the frame window

    (2)
    DockControlBar (&m_wndToolBar [, AFX_IDW_DOCKBAR_RIGHT] );
    FloatControlBar (&m_wndToolBar, CPoint (x, y)); //屏幕坐标

    (3) CControlBar::IsFloating(), SetWindowText()

    (4)TBBS_WRAPPED style to tell CToolBar where the line breaks are
        m_wndToolBar.SetButtonStyle (2,m_wndToolBar.GetButtonStyle (0) ¦ TBBS_WRAPPED);

    (5)
        CBRS_FLOAT_MULTI
        CBRS_SIZE_DYNAMIC
        CBRS_SIZE_FIXED

3.Controlling a Toolbar's Visibility

    (1)默认工具栏ID为: AFX_IDW_TOOLBAR , Framework提供默认处理程序,菜单为 ID_VIEW_TOOLBAR, ID_VIEW_STATUS_BAR

    (2)
        void CMainFrame::OnViewToolbar2 ()
        {
            ShowControlBar (&m_wndToolBar2, (m_wndToolBar2.GetStyle() & WS_VISIBLE) == 0, FALSE);
        }

        void CMainFrame::OnUpdateViewToolbar2UI (CCmdUI* pCmdUI)
        {
            pCmdUI->SetCheck ((m_wndToolBar2.GetStyle () & WS_VISIBLE) ? 1 : 0);
        }

4.Adding ToolTips and Flyby Text

    (1)Simply add CBRS_TOOLTIPS to the toolbar style and create a string table resource containing ToolTip text.

    (2)If your application features a status bar as well as a toolbar, you can configure the toolbar to display "flyby" text in addition to (or in lieu of) ToolTips by setting the CBRS_FLYBY bit in the toolbar style.
           "xxxx/nxxx"

5.在工具栏中添加非按钮控件

    (1)在TOOLBAR资源中添加一个 separator空白按钮
    (2)调用 CToolBar::SetButtonInfo来增加预留位置的宽度
    (3)在该空白处创建一个组合框(或其他控件)

    CMyToolBar::OnCreate(){ ...
   LoadToolBar(IDR_MAINFRAME) ;

        SetButtonInfo( 11, IDC_MY_COMBO, TBBS_SEPARATOR, nWidth );

        CRect rect;
        GetItemRect( 11, &rect );
        rect.bottom = rect.top + nHeight;

        m_wndComboBox.Create( WS_CHILD | WS_VISIBLE | WS_VSCROLL |
        CBS_DROPDOWNLIST | CBS_SORT, rect, this, IDC_MY_COMBO ); }

6.更新非按钮控件

     CControlBar::OnUpdateCmdUI is a virtual function the framework calls as part of its idle-processing regimen。
    void CStyleBar::OnUpdateCmdUI (CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
    {
        CToolBar::OnUpdateCmdUI (pTarget, bDisableIfNoHndler);
        CString string = GetTypefaceAtCaret ();
        if (m_wndComboBox.SelectString (-1, string) == CB_ERR)
        m_wndComboBox.SetCurSel (-1);
    }
   
一定要在OnUpdateCmdUI中调用CToolBar::OnUpdateCmdUI

7.使工具栏永久化

    框架窗口的OnCreate()中调用CFrameWnd::SaveBarState()
    在OnClose()和OnEndSession中()调用CFrameWnd::LoadBarState()

    停靠式工具栏是框架窗口的子窗口,浮动窗工具栏是围绕着它的小型框架窗口的字窗口,小型框架窗口是框架窗口的弹出式窗口。
    因为弹出式窗口是在框架窗口之前被清除,而子窗口则在他们的父窗口之后被清除,所以,不能在OnDestroy中调用SaveBarState().

8.杂项

    给CToolBar::CreateEx()传递CBRS_GRIPPER样式用于在工具栏的左边缘画一细竖杠或“架子”

二、状态栏  

1、创建和初始化

    CStatusBar::SetIndicators() specifies the number of panes the status bar will contain and optionally assigns string resources to individual panes.

    简单的:
        (1)指定字符串资源ID
        (2)连接窗格和更新函数

    // In the RC file
        STRINGTABLE
        BEGIN
            ID_INDICATOR_INS "INS"
        END

    // In CMainFrame's message map
        ON_UPDATE_COMMAND_UI (ID_INDICATOR_INS, OnUpdateIndicator)   
//也可以在文档或视图中

    // In CMainFrame::OnCreate
        static UINT nIndicators[] = {
        ID_SEPARATOR,
        ID_INDICATOR_INS
        };

        m_wndStatusBar.Create (this);
        m_wndStatusBar.SetIndicators (nIndicators, 2);

 

    主框架在 CFrameWnd::OnUpdateKeyIndicator() 中保持这些指示符的同步:
    static UINT nIndicators[] = {
        ID_SEPARATOR,
        ID_INDICATOR_CAPS,
        ID_INDICATOR_NUM,
        ID_INDICATOR_SCRL
    };


2、为菜单项提供上下文相关帮助

    如果给第一个窗格赋予ID: ID_SEPARATOR,则启用了该功能

    AFX_IDS_IDLEMESSAGE "Ready" :在没有菜单拉下或菜单项选中时,主框架会在状态栏中显示"Ready"

3.创建自定义状态栏窗格

    在状态栏中添加窗格,并利用CStaturBar::SetPaneInfo() 调整窗格大小.

 StyleDescription
 SBPS_NOBORDERSDraws the pane flush with the surface of the status bar.
 SBPS_POPOUTDraws the pane so that it protrudes from the status bar.
 SBPS_NORMALDraws the pane so that it is indented into the status bar.
 SBPS_DISABLEDDisables the pane. Disabled panes don't display text.
 SBPS_STRETCH Stretches the pane to fill unused space when the status bar is resized. Only one pane per status bar can have this style.
 SBPS_OWNERDRAW Creates an owner-draw pane.
 static UINT nIndicators[] = {
ID_SEPARATOR,
ID_SEPARATOR,
ID_SEPARATOR
};

m_wndStatusBar.Create (this);
m_wndStatusBar.SetIndicators (nIndicators, 3);

m_wndStatusBar.SetPaneInfo (0, ID_SEPARATOR, SBPS_NOBORDERS, 64);
m_wndStatusBar.SetPaneInfo (1, ID_SEPARATOR, SBPS_POPOUT, 64);
m_wndStatusBar.SetPaneInfo (2, ID_SEPARATOR, SBPS_NORMAL ¦SBPS_STRETCH, 0);
  

4.更新窗格文本
    (1) CStatusBar::SetPaneText
    (2)给窗格映射一个更新函数,并由此函数调用CmdUI:SetText

    给字符串一个虚设值,则MFC用该字符串的宽度调整状态栏窗格的大小 _T("MMMMM").
    文本前加'/t'则文本会居中,两个"/t/t"则窗格文本会左右对齐。


   
三、组合栏

  • Create, which creates a rebar from a CReBar object
  • GetReBarCtrl, which returns a CReBarCtrl reference to the underlying rebar control
  • AddBar, which adds a band to the rebar

    m_bitmap.LoadBitmap (IDB_BKGND);
    m_wndToolBar.CreateEx (this, TBSTYLE_FLAT | TBSTYLE_TRANSPARENT);
    m_wndToolBar.LoadToolBar (IDR_TOOLBAR);
    m_wndReBar.Create (this);
    m_wndReBar.AddBar (&m_wndToolBar, _T ("Main"), &m_bitmap);

     When you use a background bitmap in this manner, it's important to create the toolbar with the styles TBSTYLE_FLAT and TBSTYLE_TRANSPARENT and to use light gray as the toolbar buttons' background color.
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容简介   《MFC Windows程序设计(第2版)》是对其极为经典的第1版的全面更新,本书不仅扩展了已被认为是权威的关于Microsoft用于Windows API的功能强大的C++类库的阐述,还新增了有关COM、OLE和ActiveX的内容。本书的作者,Jeff Prosise,用其无与伦比的技巧向读者讲述了MFC程序设计中的基本概念和主要技术——再次阐释了在32位Windows平台上进行了快速的面向对象开发的完美方法。   本书涵盖了以下专题:   事件驱动程序设计和MPC的基础知识   文档/视图体系结构   位图、调色板和区域   多线程和线程同步   MFC与组件对象模型(COM)   ActiveX控件 作者简介   Jeff Prosise是一位作者、教员和讲师,他以Windows编程和教授别人如何进行Windows为生。作为一位在Windows程序设计、MFC和COM领域世界知名的权威,他还是《PC Magazinge》和《Microsoft Systems Journal》杂志的组稿编辑。 目录   序言   第Ⅰ部分 WindowsMFC基础   第1章 Hello,MFC   第2章 在窗口中绘图   第3章 鼠标和键盘   第4章 菜单   第5章 MFC集合数   第6章 文件I/O和串行化   第7章 控件   第8章 对话框和属性表   第Ⅱ部分 文档/视图体系结构   第9章 文档、视图和单文档界面   第10章 滚动视图、HTML视图以及其他视图类型   第11章 多文档和多视图   第12工具栏、状态栏和组合栏   第13章 打印和打印预览   第Ⅲ部分 高级篇   第14章 计时器和空闲处理   第15章 位图、调色板以及区域   第16章 公用控件   第17章 线程和线程同步化   第Ⅳ部分 COM,OLE和ActiveX   第18章 MFC和组件对象模型   第19章 剪贴板和OLE拖放   第20章 Automation   第21章 ActiveX控件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值