第一,创建一个基于对话框的工程
第二,在主窗口头文件中声明一个CToolBarCtrl 类对象和两个图像列表对象。
CImageList m_Images;
CImageList m_HotImages;
CToolBarCtrl m_ToolBar;
第三,在工作区的资源视图窗口中导入图标资源。
第三,在对话框的OnInitDialog方法中加载图标,关联图像列表,创建工具栏。
BOOL CGraphicToolbarDlg::OnInitDialog() CDialog::OnInitDialog();
// Add "About..." menu item to systemmenu.
// IDM_ABOUTBOX must be in the system commandrange.
ASSERT((IDM_ABOUTBOX & 0xFFF0) ==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL) CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!strAboutMenu.IsEmpty()) pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX, strAboutMenu); }
// Set the icon for thisdialog. The framework does thisautomatically
// when the application's mainwindow is not a dialog
SetIcon(m_hIcon,TRUE); // Setbig icon
SetIcon(m_hIcon,FALSE); // Set small icon //创建图像列表
m_Images.Create(32,32,ILC_COLOR24|ILC_MASK,1,1);
m_HotImages.Create(32,32,ILC_COLOR24|ILC_MASK,1,1); m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON9));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON4));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON5));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON6));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON7));
m_Images.Add(AfxGetApp()->LoadIcon(IDI_ICON8));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON10));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON11));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON12));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON13));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON14));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON15));
m_HotImages.Add(AfxGetApp()->LoadIcon(IDI_ICON16));
//创建工具栏
m_Toolbar.Create(WS_CHILD|WS_VISIBLE,CRect(20,20,100,60),this,12345);
TBBUTTON btns[9];
TBBUTTONINFO btnInfo[9]; for (int i = 0;i<8;i++) memset(&btns[i],0,sizeof(TBBUTTON));
if (i==3 || i==7)
btns[i].fsStyle= TBSTYLE_SEP ; //第4、8个按钮为分隔条
else
btns[i].idCommand=1001+i;
btns[i].iBitmap = i;
btns[i].fsState =TBSTATE_ENABLED ;
}
//添加工具栏按钮
m_Toolbar.AddButtons(9,btns);
for (i = 0; i<9; i++) btnInfo[i].cbSize = sizeof(TBBUTTONINFO);
btnInfo[i].dwMask = TBIF_TEXT;
char buffer[20] ;
memset(buffer,0,20);
itoa(i,buffer,10);
strcat( buffer,"按钮");
btnInfo[i].pszText =buffer;
m_Toolbar.SetButtonInfo(1001+i,&btnInfo[i]);
}
//设置工具栏关联的图像列表控件
m_Toolbar.SetImageList(&m_Images);
m_Toolbar.SetHotImageList(&m_HotImages); m_Toolbar.SetButtonWidth(50,60); m_Toolbar.SetBitmapSize(CSize(40,30)); m_Toolbar.SetStyle(TBSTYLE_FLAT|CCS_TOP); RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0); return TRUE;