MFC动态生成工具栏

来源:互联网 发布:kk源码论坛 编辑:程序博客网 时间:2024/06/05 05:47
文章来自:http://hi.baidu.com/cqmking/item/1ab1a6e049df04aece2d4f71

MFC动态生成工具栏


//头文件定义,状态栏等其他窗口略
CToolBar    m_wndToolBar;

if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
   | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) )
{
   TRACE0("Failed to create toolbar\n");
   return -1;      // fail to create
}


//创建状态栏
if (!m_wndStatusBar.Create(this) ||
   !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
{
   TRACE0("Failed to create status bar\n");
   return -1;      // fail to create
}

//////////////////////////////////////////////////////////////////////////
//动态生成工具栏按钮
CToolBarCtrl &toolBarCtrl=m_wndToolBar.GetToolBarCtrl();
CBitmap bitMap[6];
TBBUTTON tbButton[6] = {0};
m_ImageList.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);

bitMap[0].LoadBitmap(IDB_BITMAP1);
m_ImageList.Add(&bitMap[0],&bitMap[0]);
bitMap[1].LoadBitmap(IDB_BITMAP2);
m_ImageList.Add(&bitMap[1],&bitMap[1]);
bitMap[2].LoadBitmap(IDB_BITMAP3);
m_ImageList.Add(&bitMap[2],&bitMap[2]);
bitMap[3].LoadBitmap(IDB_BITMAP4);
m_ImageList.Add(&bitMap[3],RGB(0,0,255));
bitMap[4].LoadBitmap(IDB_BITMAP5);
m_ImageList.Add(&bitMap[4],RGB(0,0,255));
bitMap[5].LoadBitmap(IDB_BITMAP6);
m_ImageList.Add(&bitMap[5],RGB(0,0,255));
toolBarCtrl.SetImageList(&m_ImageList);
for(int i=0; i< 6; i++)
{
   tbButton[i].dwData   = NULL;
   tbButton[i].fsState   = TBSTATE_ENABLED;
   tbButton[i].fsStyle   = (BYTE)TBSTYLE_FLAT;
   tbButton[i].iBitmap   = i;
 
}
tbButton[0].idCommand = ID_EDIT_FIND;
tbButton[1].idCommand = ID_EDIT_FIND;
tbButton[2].idCommand = ID_EDIT_FIND;
tbButton[3].idCommand = ID_EDIT_FIND;
tbButton[4].idCommand = ID_EDIT_FIND;
//tbButton[5].idCommand = ID_APP_ABOUT;
toolBarCtrl.AddButtons(6, tbButton);
toolBarCtrl.AutoSize();
toolBarCtrl.SetStyle(TBSTYLE_FLAT | CCS_TOP);

//////////////////////////////////////////////////////////////////////////
//我的控制窗口
if (!m_wndMyBar1.Create(_T("我的控制条"),this,123))
{
   TRACE0("Failed to create mybar\n"); return -1;
}
if (!m_wndMyBar2.Create(_T("我的控制条2"),this,CSize(100,100),TRUE,124))
{
   TRACE0("Failed to create mybar\n"); return -1;
}
if (!m_wndMyBar3.Create(_T("我的控制条3"),this,CSize(100,100),TRUE,124))
{
   TRACE0("Failed to create mybar\n"); return -1;
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable

//////////////////////////////////////////////////////////////////////////
//停靠控制//////////////////////////
m_wndMyBar1.SetBarStyle(m_wndMyBar1.GetBarStyle() |
   CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndMyBar2.SetBarStyle(m_wndMyBar2.GetBarStyle() |
   CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndMyBar3.SetBarStyle(m_wndMyBar2.GetBarStyle() |
   CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndToolBar.EnableDocking(0);
m_wndMyBar1.EnableDocking(CBRS_ALIGN_ANY);
m_wndMyBar2.EnableDocking(0);
m_wndMyBar3.EnableDocking(0);
EnableDocking(CBRS_ALIGN_ANY);
//DockControlBar(&m_wndToolBar);
DockControlBar(&m_wndMyBar1, AFX_IDW_DOCKBAR_LEFT);

CRect nRect;
GetWindowRect(&nRect);
CPoint point(nRect.right,nRect.top);
point.y+=80;
point.x-=200;

//使工具栏窗口浮动
FloatControlBar(&m_wndMyBar2,point);
point.y+=200;
FloatControlBar(&m_wndMyBar3,point);
//m_wndMyBar2.MoveWindow(0,0,400,TRUE);
point.x-=500;
FloatControlBar(&m_wndToolBar,point);
m_wndToolBar.MoveWindow(0,0,400,TRUE);   //解决添加工具栏按钮事件可能无法显示工具按钮