一.基础知识:
1.工具条类: CToolBar.(在MSDN中查看其成员Member)
①CToolBar::CreateEx
Call this function to create a Windows toolbar (a child window) and associate it with the CToolBar object.
- virtual BOOL CreateEx(
- CWnd* pParentWnd,
- DWORD dwCtrlStyle = TBSTYLE_FLAT,
- DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP,
- CRect rcBorders = CRect(
- 0,
- 0,
- 0,
- 0
- ),
- UINT nID = AFX_IDW_TOOLBAR
- );
②.CToolBar::SetButtonText
Call this function to set the text on a button.
- BOOL SetButtonText(
- int nIndex,
- LPCTSTR lpszText
- );
2.位图类: CBitmap.(在MSDN中查看其成员Member)
①CBitmap::GetBitmap
Fills a BITMAP structure with information about the bitmap.
Call this member function to retrieve information about a CBitmap object.
- int GetBitmap(
- BITMAP* pBitMap
- );
3.位图类: CImageList.(在MSDN中查看其成员Member)
①CImageList::m_hImageList
A handle containing the image list attached to this object.
②CImageList::Create
Initializes an image list and attaches it to a CImageList object.
- BOOL Create(
- int cx,
- int cy,
- UINT nFlags,
- int nInitial,
- int nGrow
- );
③CImageList::Add
Call this function to add one or more images or an icon to an image list.
- int Add(
- CBitmap* pbmImage,
- COLORREF crMask
- );
④CImageList::Detach
Call this function to detach an image list object from a CImageList object.
- HIMAGELIST Detach( );
二.具体实现:
1.在资源视图里面,创建工具条资源(Toolbar),
重设ID为:IDR_TOOLBAR_MAIN.
2.导入BMP资源(Bitmap):
重设ID为:IDB_BITMAP_MAIN.
3.复制TrueColorToolBar文件,添加CTrueColorToolBar类.
[项目]-->>[添加现有项...]-->>选择"TrueColorToolBar.h" "TrueColorToolBar.cpp" 加入即可。
4.在对话框类所在的头文件中,添加:
- #include "TrueColorToolBar.h"
5.在对话框类中,声明保护变量:
- CTrueColorToolBar m_ToolBar;
6.添加CreateToolBar()函数:
- if (!m_ToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_ToolBar.LoadToolBar(IDR_TOOLBAR_MAIN))
- {
- TRACE0("Failed to create toolbar\n");
- return; // fail to create
- }
- m_ToolBar.ModifyStyle(0, TBSTYLE_FLAT); //Fix for WinXP
- m_ToolBar.LoadTrueColorToolBar
- (
- 48, //加载真彩工具条
- IDB_BITMAP_MAIN,
- IDB_BITMAP_MAIN,
- IDB_BITMAP_MAIN
- );
- RECT rt,rtMain;
- GetWindowRect(&rtMain);
- rt.left=0;
- rt.top=0;
- rt.bottom=80;
- rt.right=rtMain.right-rtMain.left+10;
- m_ToolBar.MoveWindow(&rt,TRUE);
- m_ToolBar.SetButtonText(0,"终端管理");
- m_ToolBar.SetButtonText(1,"进程管理");
- m_ToolBar.SetButtonText(2,"窗口管理");
- m_ToolBar.SetButtonText(3,"桌面管理");
- m_ToolBar.SetButtonText(4,"文件管理");
- m_ToolBar.SetButtonText(5,"语音管理");
- m_ToolBar.SetButtonText(6,"视频管理");
- m_ToolBar.SetButtonText(7,"服务管理");
- m_ToolBar.SetButtonText(8,"注册表管理");
- m_ToolBar.SetButtonText(10,"参数设置");
- m_ToolBar.SetButtonText(11,"生成服务端");
- m_ToolBar.SetButtonText(12,"帮助");
- RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
7.OnSize中添加代码:
- if(m_ToolBar.m_hWnd!=NULL) //工具条
- {
- CRect rc;
- rc.top=rc.left=0;
- rc.right=cx;
- rc.bottom=80;
- m_ToolBar.MoveWindow(rc); //设置工具条大小位置
- }
8.Oninitdialog 中调用CreateToolBar函数:
- CreateToolBar();
9.修改工具条里面各个图标的属性: 对应到右键菜单的功能ID,及顶部菜单的功能ID。
10.修复最小化时出现的BUG:
在OnSize函数中,添加如下代码:
- if(SIZE_MINIMIZED == nType) // 如果是最小化,则直接跳出函数
- {
- return;
- }
11.运行效果: