一种动态添加工具按钮的方式

 .h 文件中

 

public: afx_msg void OnDoing(UINT nCommandID); public: CButton btn; CImageList m_imgList; CToolBarCtrl m_ToolBar;

 

.Cpp文件中

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd) ON_WM_CREATE() ON_COMMAND_RANGE(IDI_ICON1, IDI_ICON5, OnDoing) END_MESSAGE_MAP() int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { m_imgList.Create(32,32,ILC_COLOR32|ILC_MASK,0,0); m_ToolBar.EnableAutomation(); m_ToolBar.Create(WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,IDR_TOOLBAR1); UINT Resource[ARRAY_MAX]={IDI_ICON1,IDI_ICON2,IDI_ICON3,IDI_ICON4,IDI_ICON5}; int i; TBBUTTON button[ARRAY_MAX]; for(i=0;i <ARRAY_MAX;i++) { m_imgList.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(Resource[i]))); } m_ToolBar.SetImageList(&m_imgList); CString str; for(i=0;i <ARRAY_MAX;i++) { button[i].dwData=0; button[i].iBitmap = i; button[i].fsState=TBSTATE_ENABLED; button[i].fsStyle=TBSTYLE_BUTTON; } button[0].idCommand = IDI_ICON1; button[1].idCommand = IDI_ICON2; button[2].idCommand = IDI_ICON3; button[3].idCommand = IDI_ICON4; button[4].idCommand = IDI_ICON5; m_ToolBar.AddButtons(ARRAY_MAX,button); TBBUTTON Wrap; Wrap.dwData = 0; Wrap.fsState = TBSTATE_ENABLED; Wrap.fsStyle = TBSTYLE_SEP; m_ToolBar.InsertButton(3,&Wrap); m_ToolBar.AutoSize(); m_ToolBar.SetStyle(TBSTYLE_FLAT|CCS_TOP); return 1; } void CChildFrame::OnDoing(UINT nCommandID) { switch (nCommandID) { case IDI_ICON1: AfxMessageBox(_T("IDI_ICON1")); break; case IDI_ICON2: AfxMessageBox(_T("IDI_ICON2")); break; case IDI_ICON3: AfxMessageBox(_T("IDI_ICON3")); break; case IDI_ICON4: AfxMessageBox(_T("IDI_ICON4")); break; case IDI_ICON5: AfxMessageBox(_T("IDI_ICON5")); break; default: break; } }

 

 

 

你可能感兴趣的:(command,工具,button)