如何在基于MFC对话框的应用程序中创建工具栏 [英] how to create toolbars in an MFC dialog-based application
问题描述
我不知道这个人在程序中创建了这个工具栏:
这是一个无家可归的对话框,由这个人创建,我想:
,但我的对话框是一个模态的。我不认为这会有很大的改变!
,而他写的代码是使用res文件夹中提供的工具栏:
MainFrm.h
protected://控制栏嵌入成员
CMFCMenuBar m_wndMenuBar;
CMFCToolBar m_wndToolBar;
CMFCStatusBar m_wndStatusBar;
CMFCToolBarImages m_UserImages;
MainFrm.cpp
在函数中添加的代码: code> CMainFrame :: OnCreate(LPCREATESTRUCT lpCreateStruct)初始化资源中提供的工具栏:
if(!m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)||
!m_wndToolBar.LoadToolBar(theApp.m_bHiColorIcons?IDR_MAINFRAME_256:IDR_MAINFRAME))
{
TRACE0(无法创建toolbar\\\
);
return -1; //无法创建
}
CString strToolBarName;
bNameValid = strToolBarName.LoadString(IDS_TOOLBAR_STANDARD);
ASSERT(bNameValid);
m_wndToolBar.SetWindowText(strToolBarName);
CString strCustomize;
bNameValid = strCustomize.LoadString(IDS_TOOLBAR_CUSTOMIZE);
ASSERT(bNameValid);
m_wndToolBar.EnableCustomizeButton(TRUE,ID_VIEW_CUSTOMIZE,strCustomize);
//允许用户定义的工具栏操作:
InitUserToolbars(NULL,uiFirstUserToolBarId,uiLastUserToolBarId);
if(!m_wndStatusBar.Create(this))
{
TRACE0(无法创建状态栏\);
return -1; //无法创建
}
m_wndStatusBar.SetIndicators(indicators,sizesof(indicators)/ sizeof(UINT));
// TODO:如果您不希望工具栏和菜单栏可停靠,则删除这五行
m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockPane(& m_wndMenuBar);
DockPane(& m_wndToolBar);
//启用Visual Studio 2005样式对接窗口行为
CDockingManager :: SetDockingMode(DT_SMART);
//启用Visual Studio 2005样式对接窗口自动隐藏行为
EnableAutoHidePanes(CBRS_ALIGN_ANY);
//启用工具栏和对接窗口菜单替换
EnablePaneMenu(TRUE,ID_VIEW_CUSTOMIZE,strCustomize,ID_VIEW_TOOLBAR);
//启用快速(Alt +拖动)工具栏自定义
CMFCToolBar :: EnableQuickCustomization();
if(CMFCToolBar :: GetUserImages()== NULL)
{
//加载用户定义的工具栏图像
if(m_UserImages.Load(_T( .\\\UserImages.bmp)))
{
CMFCToolBar :: SetUserImages(& m_UserImages);
}
}
//启用菜单个性化(最近使用的命令)
// TODO:定义您自己的基本命令,确保每个下拉菜单至少有一个基本命令。
CList< UINT,UINT> lstBasicCommands;
lstBasicCommands.AddTail(ID_FILE_NEW);
lstBasicCommands.AddTail(ID_FILE_OPEN);
lstBasicCommands.AddTail(ID_FILE_SAVE);
lstBasicCommands.AddTail(ID_FILE_PRINT);
lstBasicCommands.AddTail(ID_APP_EXIT);
lstBasicCommands.AddTail(ID_EDIT_CUT);
lstBasicCommands.AddTail(ID_EDIT_PASTE);
lstBasicCommands.AddTail(ID_EDIT_UNDO);
lstBasicCommands.AddTail(ID_APP_ABOUT);
lstBasicCommands.AddTail(ID_VIEW_STATUS_BAR);
lstBasicCommands.AddTail(ID_VIEW_TOOLBAR);
CMFCToolBar :: SetBasicCommands(lstBasicCommands);
**事实上,我不明白,浪费了我的时间两天,使我发布本网站中的四个问题是在这里创建一个新的工具栏之后:
并导致工具栏编辑器
1 - 如何在已经提供的工具栏上创建一个新的工具栏按钮?
2 - 如何将此按钮的形状更改为标准的24位深度图像或所需的图标?
最后我想要在我的res文件夹中有一个像我的工具栏的图像,当然我希望按钮可以单独点击我在我的程序的窗口中。
如果需要项目的来源,我已经在这里上传了该人的项目
使用mainframe.cpp的程序不是一个无模式的对话框,它是一个MFC文档/视图程序,旨在支持菜单栏和工具栏。
基于对话框的程序没有设计工具栏支持。MFC示例程序DLGCBR32(在MSDN中)显示了如何在对话框上放置工具栏。 p>
I wonder how this guy has created this toolbar on the program:
this is a modeless dialog created by the guy, I think:
but my dialog is a modal one. I don't think it makes a lot of change!
and this the code written by him to use the toolbar supplied in the res folder:
MainFrm.h
protected: // control bar embedded members
CMFCMenuBar m_wndMenuBar;
CMFCToolBar m_wndToolBar;
CMFCStatusBar m_wndStatusBar;
CMFCToolBarImages m_UserImages;
MainFrm.cpp
the code added in function:CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
to initialize the toolbar supplied in the resource:
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
CString strToolBarName;
bNameValid = strToolBarName.LoadString(IDS_TOOLBAR_STANDARD);
ASSERT(bNameValid);
m_wndToolBar.SetWindowText(strToolBarName);
CString strCustomize;
bNameValid = strCustomize.LoadString(IDS_TOOLBAR_CUSTOMIZE);
ASSERT(bNameValid);
m_wndToolBar.EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize);
// Allow user-defined toolbars operations:
InitUserToolbars(NULL, uiFirstUserToolBarId, uiLastUserToolBarId);
if (!m_wndStatusBar.Create(this))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));
// TODO: Delete these five lines if you don't want the toolbar and menubar to be dockable
m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockPane(&m_wndMenuBar);
DockPane(&m_wndToolBar);
// enable Visual Studio 2005 style docking window behavior
CDockingManager::SetDockingMode(DT_SMART);
// enable Visual Studio 2005 style docking window auto-hide behavior
EnableAutoHidePanes(CBRS_ALIGN_ANY);
// Enable toolbar and docking window menu replacement
EnablePaneMenu(TRUE, ID_VIEW_CUSTOMIZE, strCustomize, ID_VIEW_TOOLBAR);
// enable quick (Alt+drag) toolbar customization
CMFCToolBar::EnableQuickCustomization();
if (CMFCToolBar::GetUserImages() == NULL)
{
// load user-defined toolbar images
if (m_UserImages.Load(_T(".\\UserImages.bmp")))
{
CMFCToolBar::SetUserImages(&m_UserImages);
}
}
// enable menu personalization (most-recently used commands)
// TODO: define your own basic commands, ensuring that each pulldown menu has at least one basic command.
CList<UINT, UINT> lstBasicCommands;
lstBasicCommands.AddTail(ID_FILE_NEW);
lstBasicCommands.AddTail(ID_FILE_OPEN);
lstBasicCommands.AddTail(ID_FILE_SAVE);
lstBasicCommands.AddTail(ID_FILE_PRINT);
lstBasicCommands.AddTail(ID_APP_EXIT);
lstBasicCommands.AddTail(ID_EDIT_CUT);
lstBasicCommands.AddTail(ID_EDIT_PASTE);
lstBasicCommands.AddTail(ID_EDIT_UNDO);
lstBasicCommands.AddTail(ID_APP_ABOUT);
lstBasicCommands.AddTail(ID_VIEW_STATUS_BAR);
lstBasicCommands.AddTail(ID_VIEW_TOOLBAR);
CMFCToolBar::SetBasicCommands(lstBasicCommands);
**In fact something that I don't understand and has wasted my time two days and made me to post four questions in this site is that after making a new toolbar supply here:
and leading to the toolbar editor
1-how can I create a new toolbar button here on the toolbar that has been supplied?
2-how can I change the shape of this button to the standard 24bit depth images or to a desired icon?
at last I want to have an image like this for my toolbar in my res folder and surely I want the buttons to be seperately clickable in my program's window.
If there's a need to the source of the project, I have uploaded the guy's project here
The program with mainframe.cpp is not a modeless dialog, it is an MFC doc/view program that is designed to support a menu bar and toolbar.
A dialog-based program does not have the toolbar support designed in. The MFC sample program DLGCBR32 (in MSDN) shows how to put a toolbar on a dialog.
这篇关于如何在基于MFC对话框的应用程序中创建工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!