VC++中添加工具栏方法
1、 使用VC生成的工具栏
通过VC资源窗口insert一个工具栏资源IDR_TOOLBAR1,并进行编辑
为对话框类添加一个工具栏变量
CToolBarm_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");
returnFALSE;
}
m_Toolbar.ShowWindow(SW_SHOW);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST, 0);
2、 通过图像列表自定义工具栏
通过VC资源窗口insert所需的ico文件,并分别命名
为对话框类添加工具栏变量和图像列表变量
CToolBarm_ToolBar;
CImageListm_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));
UINTarray[8];
for(inti=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);
3、导入图片生成工具栏,并将工具栏与菜单栏对应项绑定
通过VC资源窗口insert所需的ico文件,并分别命名,然后insert一个菜单资源IDR_MAINFRAME_MENU,并进行编辑
为对话框类添加菜单栏变量、工具栏变量和图像列表变量
CMenum_Menu;
CToolBarm_ToolBar;
CImageListm_ImageList;
在OnInitDialog()函数中添加如下代码,将图像列表与工具栏关联,并将工具栏和对应菜单项ID进行关联,从而与菜单项共用处理函数
m_Menu.LoadMenu(IDR_MAINFRAME_MENU);
SetMenu(&m_Menu);
TBBUTTONtbButton[ICONSUM] = {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.SetImageList(&m_ImageList);
for(inti=0; i< ICONSUM; 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.AddButtons(ICONSUM,tbButton);
m_ToolBar.AutoSize();
m_ToolBar.SetStyle(TBSTYLE_FLAT| CCS_TOP);
注:若要把工具栏的按钮和菜单项相关联,也可将它们的ID设置为同一个ID来实现
VC++中添加状态栏方法
转自:http://hi.baidu.com/show_it/item/2a12a5dd72ef2517e1f46f20
方法一:
1.添加成员变量CStatusBarCtrl m_StatusBar;
2.在OnInitDialog()中加入:
m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW,CRect(0,0,0,0), this, 0);
intstrPartDim[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));//为第二个分栏中加的图标
方法二:
1.添加成员变量CStatusBar m_wndStatusBar;
2.在OnInitDialog()中加入:
static UINT indicators[] =
{
D_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);
本文详细介绍了在VC++中添加工具栏和状态栏的方法,包括使用VC资源窗口创建工具栏,通过图像列表自定义工具栏,以及绑定菜单项,最后讲解了两种添加状态栏的方法,包括设置分栏文本和图标。
4935

被折叠的 条评论
为什么被折叠?



