基于对话框程序,自定义工具栏(支持真彩色图标,可添加文字)

来源:互联网 发布:知聊破解版无限聊币 编辑:程序博客网 时间:2024/05/02 02:24

对话框中添加工具栏

 

基于对话框程序,自定义工具栏(支持真彩色图标,可添加文字)

动机:传统的VC工具栏只支持16色的图标,且不能添加文字。

要点:CToolBarCtrl类的使用。先引用MSDN上的话(翻译水平比较菜,见谅!)

            使用CToolBarCtrl类,一般遵从以下几个步骤:
           1.构造一个CToolBarCtrl对象。
            2.调用Create函数创建Windows工具条通用控件并与CToolBarCtrl对象相关联。
           3.确定工具条上的按钮如何显示:
               (1)使用位图图像。调用AddBitmap向工具条添加按钮位图
              (2)使用图像列表里面显示的图像。调用SetImageList函数、SetHotImageList函数、SetDisabledImageList函数指定图像列表
              (3)作用字符串标签。调用AddString和(或)AddStrings函数为工具栏添加字符串
           4.调用AddButtons函数为工具条添加按钮结构
           5.如果需要为不是CFrameWnd的拥有窗口添加工具提示,需要在工具条拥有窗口中传递TTN_NEEDTEXT消息,该消息在CToolBarCtrl: Handling Tool Tip Notifications中有所描述。

步骤:1.将要作为工具栏图标的位图或图标导入到VC资源管理器中。

            2.在C***Dlg类为添加两个成员变量:CImageList m_ImageList,CToolBarCtrl m_ToolBar

           3.在OnInitDialog()函数中添加如下代码:

/***************************************创建工具栏********************************************/

/***************************************创建工具栏********************************************/

             3.最终效果如图:

注解:TBBUTTON是定义工具条按钮的结构体,声明如下:

typedef struct _TBBUTTON {int iBitmap;// zero-based index of button imageint idCommand;  // command to be sent when button pressedBYTE fsState;     // button state--see belowBYTE fsStyle;     // button style--see belowDWORD dwData;     // application-defined valueint iString;// zero-based index of button label string} TBBUTTON;
调用AddButtons函数向工具栏添加按钮。函数原型如下:

BOOL AddButtons( int nNumButtons, LPTBBUTTON lpButtons );

 

其中nNumButtons是要添加的按钮数目,lpButtons是指向TBBUTTON结构体的指针。

 

 

 

原创粉丝点击