真彩位图工具栏的实现

 

下边是使用BMP制作 Ctoolbar 的一点总结。
    1. 各种色位的定义         #define ILC_COLOR4 0x0004 //16色
        #define ILC_COLOR8 0x0008 //256色
        #define ILC_COLOR16 0x0010 //16位色
        #define ILC_COLOR24 0x0018 //24位色
        #define ILC_COLOR32 0x0020 //32位色

 
    2. 如果不指定hot 和 Disable 状态的话,是看不到动态效果的。图标永远是静止的。
    3. 如果发现图标 一直处于 变灰状态。可能是由于你没有为每个图标指定命令。
   以下自林风
 
VC++6.0直接支持256色工具栏的编辑和使用,若想建立真彩(32位位图)工具栏,必须借助其它绘图工具绘制工具栏位图。
制作真彩工具栏的主要步骤:
①用绘图工具(我用的是Fireworks)制作三张32位位图。其中:
30.bmp为工具可用时显示的位图:
 
 
31.bmp为工具不可用(变灰)时显示的位图:
 
 
32.bmp为鼠标进入工具按钮时显示的位图:
 
 
 
这里,每张位图中按钮的个数和大小可根据实际应用进行修改,本例中,每张位图有7个按钮,每个按钮大小为48×48。
 
从网上搜集这类图标,并不是难事。但如何将他们无缝衔接在一起,倒是要费一番功夫。
这里推荐:      CVisualToolbar   免费工具生成。
 
②把三个位图导入资源(作为Bitmap资源),尽管在VC++下不能显示和编辑,但使用上没有问题。把三个位图的ID依次改为:
IDB_TOOLBARCOLOR、IDB_TOOLBARDISABLE、IDB_TOOLBARHOT
③在资源的工具栏项目中添加一个新的工具栏(如图),ID改为IDR_TOOLBAR,工具按钮的宽度和高度都改为20。在上面依次添加14个按钮并分好组,按钮上的内容无关紧要,只要不是空的就行了。删除系统原来的工具栏IDR_MAINFRAME。
 
这个工具栏在显示时,我们会用上面的位图替换各按钮。
④在MainFrm.h中定义三个位图列表对象和一个函数定义:
MainFrm.h:
CImageList    m_imageToolBar;
CImageList    m_imageToolBarDisable;
CImageList    m_imageToolBarHot;
void InitToolBar(); 
⑤在MainFrm.cpp中修改工具栏的设置部分:
MainFrm.cpp:
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_wndToolBar.LoadToolBar( IDR_TOOLBAR ))  //指定工具栏ID号
{
    TRACE0("Failed to create toolbar/n");
    return -1;      // fail to create
}
InitToolBar();    //设置真彩工具栏 
这里要把原工具栏的ID(IDR_MAINFRAME)修改为新工具栏的ID(IDR_TOOLBAR),再调用函数InitToolBar()设置新工具栏。
⑥添加设置工具栏的函数InitToolBar():
void CMainFrame::InitToolBar()
{
    CBitmap bm;
    //活动的工具
    m_imageToolBar.Create( 48, 48, TRUE | ILC_COLOR32, 7, 0 );
    bm.LoadBitmap( IDB_TOOLBARCOLOR );
    m_imageToolBar.Add( &bm,(CBitmap*)NULL );
    bm.Detach();
    m_wndToolBar.GetToolBarCtrl().SetImageList( &m_imageToolBar );
    //禁止的工具
    m_imageToolBarDisable.Create( 48, 48, TRUE | ILC_COLOR32, 7, 0 );
    bm.LoadBitmap( IDB_TOOLBARDISABLE );
    m_imageToolBarDisable.Add( &bm,(CBitmap*)NULL );
    bm.Detach();
    m_wndToolBar.GetToolBarCtrl().SetDisabledImageList( &m_imageToolBarDisable );
    //当前的工具
    m_imageToolBarHot.Create( 48, 48, TRUE | ILC_COLOR32, 7, 0 );
    bm.LoadBitmap( IDB_TOOLBARHOT );
    m_imageToolBarHot.Add( &bm,(CBitmap*)NULL );
    bm.Detach();
    m_wndToolBar.GetToolBarCtrl().SetHotImageList( &m_imageToolBarHot );
}
 
其中在Create()函数中的参数依次为按钮宽度、高度、32位位图和按钮个数。
如果你的绘图能力很强,可以修改32.bmp,使按钮具有很好的动感。至于各按钮的响应方法与普通工具栏是一样的。
示例程序界面:


 ====================================

CCmdUI does not have a base class.

When a user of your application pulls down a menu, each menu item needs to know whether it should be displayed as enabled or disabled. The target of a menu command provides this information by implementing an ON_UPDATE_COMMAND_UI handler. For each of the command user-interface objects in your application, use the Properties window to create a message-map entry and function prototype for each handler.

When the menu is pulled down, the framework searches for and calls each ON_UPDATE_COMMAND_UI handler, each handler calls CCmdUI member functions such as Enable and Check, and the framework then appropriately displays each menu item.

A menu item can be replaced with a control-bar button or other command user-interface object without changing the code within the ON_UPDATE_COMMAND_UI handler.

The following table summarizes the effect CCmdUI's member functions have on various command user-interface items.

User-Interface Item

Enable

SetCheck

SetRadio

SetText

Menu item

Enables or disables

Checks (×) or unchecks

Checks using dot (•)

Sets item text

Toolbar button

Enables or disables

Selects, unselects, or indeterminate

Same as SetCheck

(Not applicable)

Status-bar pane

Makes text visible or invisible

Sets pop-out or normal border

Same as SetCheck

Sets pane text

Normal button in CDialogBar

Enables or disables

Checks or unchecks check box

Same as SetCheck

Sets button text

Normal control in CDialogBar

Enables or disables

(Not applicable)

(Not applicable)

Sets window text

==================================================

When the user pulls down a menu, the window to which the menu is attached receives a series of messages. Among the first to arrive is a WM_INITMENU message notifying the window that a top-level menu item was selected. Before a submenu is displayed, the window receives a WM_INITMENUPOPUP message. Windows programs sometimes take this opportunity to update the submenu's menu items—for example, putting a check mark next to the Toolbar item in the View menu if the application's toolbar is displayed or unchecking the menu item if the toolbar is currently hidden. As the highlight travels up and down the menu, the window receives WM_MENUSELECT messages reporting the latest position in the menu. In SDK-style programs, WM_MENUSELECT messages are sometimes used to display context-sensitive menu help in a status bar.

The most important message of all is the WM_COMMAND message sent when the user selects an item from the menu. The low word of the message's wParam parameter holds the item's command ID. SDK programmers often use switch-case logic to vector execution to the appropriate handling routine, but MFC provides a better way. An ON_COMMAND statement in the message map links WM_COMMAND messages referencing a particular menu item to the class member function, or command handler, of your choice. The following message-map entry tells MFC to call OnFileSave when the ID_FILE_SAVE menu item is selected

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值