图解VC++工具栏和状态栏编程实例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bcbobo21cn/article/details/51068541

demo工程

http://pan.baidu.com/s/1mh83aru


VB6附带的graphics文件夹资源

http://pan.baidu.com/s/1sl5RmI5


一 在对话框添加工具栏









如上图;插入工具栏资源,并编辑;

对话框头文件中加入;

CToolBar m_ToolBar;


OnInitDialog()函数中添加

if(!m_ToolBar.CreateEx( this,TBSTYLE_FLAT , WS_CHILD | WS_VISIBLE |CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS,
              CRect(4,4,0,0))||!m_ToolBar.LoadToolBar(IDR_TOOLBAR1) )
       {
              TRACE0("failedto create toolbar\n");
              return FALSE;
       }
       m_ToolBar.ShowWindow(SW_SHOW);
       RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST, 0);

效果:



二 通过图像列表自定义工具栏







如上图,通过VC资源窗口insert所需的ico文件,并分别命名;

对话框头文件中加入

CToolBar m_ToolBar;
CImageList m_ImageList;

OnInitDialog()函数中添加如下代码,将图像列表与工具栏关联并进行显示

m_ImageList.Create(32,32,ILC_COLOR24|ILC_MASK,1,1);
       //向图像列表中添加图标
       m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICONUSER));
       m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICONSET));
       m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICONQUERY));
       m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICONRECORD));
       m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICONIMPORT));
       m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICONEXPORT));
       UINT array[8];
       for(int i=0;i<8;i++)        //初始化工具栏ID
       {
              if(i==2|| i==5)
                     array[i]= ID_SEPARATOR; //第3、6个按钮为分隔条
              else
                     array[i]= i+1001;
       }
       m_ToolBar.Create(this);
       m_ToolBar.SetButtons(array,8);
       //关联图像列表
       m_ToolBar.GetToolBarCtrl().SetImageList(&m_ImageList);
       m_ToolBar.SetSizes(CSize(50,60),CSize(32,32));//设置按钮和图标的大小
/*The sizeImage parameter must contain thesize, in pixels, of the images in the toolbar's bitmap.
The dimensions in sizeButton must besufficient to hold the image plus 7 pixels extra in width and 6 pixels extra inheight.
This function also sets the toolbar heightto fit the buttons.*/
       //设置工具栏按钮的显示文本
       m_ToolBar.SetButtonText(0,"  用户  ");
       m_ToolBar.SetButtonText(1,"  设置  ");
       m_ToolBar.SetButtonText(3,"  查询  ");
       m_ToolBar.SetButtonText(4,"  记录  ");
       m_ToolBar.SetButtonText(6,"  导入  ");
       m_ToolBar.SetButtonText(7,"  导出  ");
       RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
效果:



三 导入图片生成工具栏,并将工具栏与菜单栏对应项绑定




如上图;通过VC资源窗口insert所需的ico文件,并分别命名,然后insert一个菜单资源,并进行编辑;

头文件中添加

CMenu m_Menu;
    CToolBar m_ToolBar;
    CImageList m_ImageList;

OnInitDialog()函数中添加如下代码,将图像列表与工具栏关联,并将工具栏和对应菜单项ID进行关联,从而与菜单项共用处理函数

m_Menu.LoadMenu(IDR_MENU1);
    SetMenu(&m_Menu);
 
       TBBUTTON tbButton[5] = {0};
       m_ImageList.Create(32,32, ILC_COLOR32 | ILC_MASK, 0, 0);
       m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_ICON3));
       m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_ICON1));
       m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_ICON4));
       m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_ICON5));
       m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_ICON2));
       //m_ToolBar.Create(WS_CHILD| WS_VISIBLE, CRect(0, 0, 0, 0), this, 0);
	   //m_ToolBar.Create(this,WS_CHILD| WS_VISIBLE,0);
	   m_ToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP );
       m_ToolBar.GetToolBarCtrl().SetImageList(&m_ImageList);
       for(int i=0; i< 5; i++)
       {
              tbButton[i].dwData        = NULL;
              tbButton[i].fsState         = TBSTATE_ENABLED;
              tbButton[i].fsStyle         = (BYTE)TBSTYLE_FLAT;
              tbButton[i].iBitmap        = i;
       }
       tbButton[0].idCommand= ID_ATTENDANCE_RECORD;
       tbButton[1].idCommand= IDM_REQUEST_EMPLOYEE;
       tbButton[2].idCommand= IDM_PAYLIP;
       tbButton[3].idCommand= IDM_PORTFOLIO;
       tbButton[4].idCommand= ID_ABOUT;
       m_ToolBar.GetToolBarCtrl().AddButtons(5,tbButton);
       m_ToolBar.GetToolBarCtrl().AutoSize();
       m_ToolBar.GetToolBarCtrl().SetStyle(TBSTYLE_FLAT| CCS_TOP);

效果:


若要把工具栏的按钮和菜单项相关联,也可将它们的ID设置为同一个ID来实现

四 添加状态栏方法一

添加成员变量
CStatusBarCtrl m_StatusBar;
OnInitDialog()中加入
m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW,CRect(0,0,0,0), this, 0);
       int strPartDim[3]= {100, 200, -1}; //分割数量
       m_StatusBar.SetParts(3,strPartDim);
       //设置状态栏文本
       m_StatusBar.SetText("分栏一", 0, 0);
       m_StatusBar.SetText("分栏二", 1, 0);
       m_StatusBar.SetText("分栏三", 2, 0);
       //下面是在状态栏中加入图标
       m_StatusBar.SetIcon(1,SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME),FALSE));//为第二个分栏中加的图标
效果:



五 添加状态栏方法二

添加成员变量
CStatusBar m_wndStatusBar;
在OnInitDialog()中加入
static UINT indicators[] =
       {
              ID_SEPARATOR,                            //status line indicator
              ID_INDICATOR_CAPS,             //CAP lock indicator.
              ID_INDICATOR_NUM,              //NUM lock indicator.
              ID_INDICATOR_SCRL,             //SCRL lock indicator.
       };
       if(!m_wndStatusBar.Create(this)||!m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT)))
       {
              TRACE0("Failed to create statusbarn");
              return -1;         // fail to create
       }
       UINT nID;        //控制状态栏里面的分栏
       m_wndStatusBar.SetPaneInfo(0,nID,SBPS_STRETCH|SBPS_NOBORDERS,100);              //返回值存nID中
       m_wndStatusBar.SetPaneText(0,"就绪");
       m_wndStatusBar.SetPaneInfo(1,nID,SBPS_NORMAL,100);
       m_wndStatusBar.SetPaneText(1,"大写");
       m_wndStatusBar.SetPaneInfo(2,nID,SBPS_POPOUT,100);
       m_wndStatusBar.SetPaneText(2,"数字");
//               SetPaneInfo()函数的第三个参数的可选项如下:
//               The following indicator styles are supported:
//                           SBPS_NOBORDERS               No 3-D border around the pane.
//                           SBPS_POPOUT                        Reverse border so that text "pops out."
//                           SBPS_DISABLED                     Do not draw text.
//                           SBPS_STRETCH                      Stretch pane to fill unused space. Only one pane per status bar can have thisstyle.
//                           SBPS_NORMAL                        No stretch, borders, or pop-out.
//----------------让这个状态栏最终显示在对话框中-------------
       RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
效果:



所用图标是VB6附带的graphics文件夹下的内容;见前面下载链接;

展开阅读全文

没有更多推荐了,返回首页