`
isiqi
  • 浏览: 16081968 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

MFC控件篇之利用AppWizard创建并使用ToolBar StatusBar Dialog Bar

阅读更多
利用AppWizard创建并使用ToolBar StatusBar Dialog Bar

运行时程序界面如界面图,该程序拥有一个工具条用于显示两个命令按钮,一个用于演示如何使按钮处于检查状态,另一个根据第一个按钮的状态来禁止/允许自身。(设置检查状态和允许状态都通过OnUpdateCommand实现)此外Dialog Bar上有一个输入框和按钮,这两个子窗口的禁止/允许同样是根据工具条上的按钮状态来确定,当按下Dialog Bar上的按钮时将显示输入框中的文字内容。状态条的第一部分用于显示各种提示,第二部分用于利用OnUpdateCommand显示当前时间。同时在程序中演示了如何设置菜单项的命令解释字符(将在状态条的第一部分显示)和如何设置工具条的提示字符(利用一个小的ToolTip窗口显示)。
生成应用:利用AppWizard生成一个MFC工程,并设置为单文档界面,最后选择工具条,状态条和ReBar支持,
修改菜单:利用资源编辑器删除多余的菜单并添加一个新的弹出菜单和三个子菜单,分别是:
名称
ID
说明字符
Check
IDM_CHECK
SetCheck Demo\nSetCheck Demo
Disable
IDM_DISABLE
Disable Demo\nDisable Demo
ShowText on DialogBar
IDM_SHOW_TXT
ShowText on DialogBar Demo\nShowText on DialogBar
\n前的字符串将显示在状态条中作为命令解释,\n后的部分将作为具有相同ID的工具条按钮的提示显示在ToolTip窗口中。
修改Dialog Bar:在Dialog Bar中添加一个输入框和按钮,按钮的ID为IDM_SHOW_TXT与一个菜单项具有相同的ID,这样可以利用映射菜单消息来处理按钮消息(当然使用不同ID值也可以利用ON_COMMAND来映射Dialog Bar上的按钮消息,但是ClassWizard没有提供为Dialog Bar上按钮进行映射的途径,只能手工添加消息映射代码)。
修改工具条:在工具条中添加两个按钮,ID值为IDM_CHECK和IDM_DISABLE和其中两个菜单项具有相同的ID值。
利用ClassWizard为三个菜单项添加消息映射和更新命令。
修改MainFrm.h文件
//添加一个成员变量来记录工具条上Check按钮的检查状态。
protected:
BOOL m_fCheck;
//手工添加状态条第二部分用于显示时间的更新命令,
和用于禁止/允许输入框的更新命令
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnCheck();
afx_msg void OnUpdateCheck(CCmdUI* pCmdUI);
afx_msg void OnDisable();
afx_msg void OnUpdateDisable(CCmdUI* pCmdUI);
afx_msg void OnShowTxt();
afx_msg void OnUpdateShowTxt(CCmdUI* pCmdUI);
//}}AFX_MSG
//上面的部分为ClassWizard自动产生的代码
afx_msg void OnUpdateTime(CCmdUI* pCmdUI); //显示时间
afx_msg void OnUpdateInput(CCmdUI* pCmdUI); //禁止/允许输入框
修改MainFrm.cpp文件
//修改状态条上各部分ID
#define ID_TIME 0x705 //作为状态条上第二部分ID
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_SEPARATOR, //先设置为ID_SEPARATOR,
在状态条创建后再进行修改
};
//修改消息映射
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(IDM_CHECK, OnCheck)
ON_UPDATE_COMMAND_UI(IDM_CHECK, OnUpdateCheck)
ON_COMMAND(IDM_DISABLE, OnDisable)
ON_UPDATE_COMMAND_UI(IDM_DISABLE, OnUpdateDisable)
ON_COMMAND(IDM_SHOW_TXT, OnShowTxt)
ON_UPDATE_COMMAND_UI(IDM_SHOW_TXT, OnUpdateShowTxt)
//}}AFX_MSG_MAP
//以上部分为ClassWizard自动生成代码
ON_UPDATE_COMMAND_UI(ID_TIME, OnUpdateTime) ////显示时间
ON_UPDATE_COMMAND_UI(IDC_INPUT_TEST, OnUpdateInput)
//禁止/允许输入框
//修改OnCreate函数,重新设置状态条第二部分ID值
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
....
// by wenyy 修改状态条上第二部分信息
m_wndStatusBar.SetPaneInfo(1,ID_TIME,SBPS_NORMAL,60);
//set the width
return 0;
}
//修改经过映射的消息处理函数代码
void CMainFrame::OnCheck()
{
//在Check按钮被按下时改变并保存状态
m_fCheck=!m_fCheck;
}
void CMainFrame::OnUpdateCheck(CCmdUI* pCmdUI)
{
//Check按钮是否设置为检查状态
pCmdUI->SetCheck(m_fCheck);
}
void CMainFrame::OnDisable()
{
//Disable按钮被按下
AfxMessageBox("you press disable test");
}
void CMainFrame::OnUpdateDisable(CCmdUI* pCmdUI)
{
//根据Check状态决定自身禁止/允许状态
pCmdUI->Enable(m_fCheck);
}
void CMainFrame::OnShowTxt()
{
//得到Dialog Bar上输入框中文字并显示
CEdit* pE=(CEdit*)m_wndDlgBar.GetDlgItem(IDC_INPUT_TEST);
CString szO;
pE->GetWindowText(szO);
AfxMessageBox(szO);
}
void CMainFrame::OnUpdateShowTxt(CCmdUI* pCmdUI)
{
//Dialog Bar上按钮根据Check状态决定自身禁止/允许状态
pCmdUI->Enable(m_fCheck);
}
void CMainFrame::OnUpdateInput(CCmdUI* pCmdUI)
{
//Dialog Bar上输入框根据Check状态决定自身禁止/允许状态
pCmdUI->Enable(m_fCheck);
}
void CMainFrame::OnUpdateTime(CCmdUI* pCmdUI)
{
//根据当前时间设置状态条上第二部分文字
CTime timeCur=CTime::GetCurrentTime();
char szOut[20];
sprintf( szOut, "%02d:%02d:%02d", timeCur.GetHour(),
timeCur.GetMinute(),timeCur.GetSecond());
pCmdUI->SetText(szOut);
}


分享到:
评论

相关推荐

    Visual C++/MFC学习笔记.doc

    框架窗口间的关系和消息传送规律3.2 接收用户输入3.3 使用菜单3.4 文档、视、框架之间相互作用3.5 利用序列化进行文件读写3.6 MFC中所提供的各种视类介绍第四章 窗口控件4.1 Button4.2 Static Box4.3 Edit Box4.4 ...

    Visual C++MFC入门教程

    4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar 37 4.E General Window 40 4.F 关于WM_NOTIFY的使用方法 41 第五章 对话框 42 5.1 使用资源编辑器编辑对话框 42 5.2 创建有模式对话框 43 5.3 创建无模式...

    VC++初级编程.zip

    4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar 4.E General Window 4.F 关于WM_NOTIFY的使用方法 第五章 对话框 5.1 使用资源编辑器编辑对话框 5.2 创建有模式对话框 5.3 创建无模式对话框 5.4 在对话框...

    Visual_C++MFC入门教程

    |------ 4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar |------ 4.E General Window |------ 4.F 关于WM_NOTIFY的使用方法 +-- 第五章 对话框 |------ 5.1 使用资源编辑器编辑对话框 |------ 5.2 ...

    Visual C++ MFC入门教程[TXT]

    |------ 4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar |------ 4.E General Window |------ 4.F 关于WM_NOTIFY的使用方法 +-- 第五章 对话框 |------ 5.1 使用资源编辑器编辑对话框 |------ 5.2 创建有...

    VC++6.0 从入门到精通

    |------ 4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar |------ 4.E General Window |------ 4.F 关于WM_NOTIFY的使用方法 +-- 第五章 对话框 |------ 5.1 使用资源编辑器编辑对话框 |------ 5.2 创建有...

    Visual C++/MFC入门教程

    ------ 4.9 Tab Ctrl |------ 4.A Tool Bar |------ 4.B Status Bar |------ 4.C Dialog Bar |------ 4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar |------ 4.E General ...

    闻怡洋VC基础教程

    ------ 4.B Status Bar |------ 4.C Dialog Bar |------ 4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar |------ 4.E General Window |------ 4.F 关于WM_NOTIFY的使用方法 +-- 第五章 ...

    深入浅出MFC【侯捷】

    深入浅出MFC(第二版) 目录 第0章 你一定要知道(导读) 这本书适合谁 你需要什么技术基础 你需要什么软硬件环境 让我们使用同一种语言 本书符号习惯 本书例程的取得 范例程序说明 与前版本之差异 如何联络作者 第...

    深入浅出MFC 2e

    工具栏和状态栏的诞生(Toolbar&Status bar) 鼠标拖放(Drag and Drop) 消息映射(Message Map) 标准菜单File/Edit/View/Window/Help 对话框 改用CEditView 第四篇 深入MFC程序设计 第8章 Document-View深入探讨 ...

    侯捷- -深入浅出MFC

    工具栏和状态栏的诞生(Toolbar&Status bar) 鼠标拖放(Drag and Drop) 消息映射(Message Map) 标准菜单File/Edit/View/Window/Help 对话框 改用CEditView 第四篇 深入MFC程序设计 第8章 Document-View深入探讨 ...

    深入浅出mfc简体中文版

    工具列和狀態列的誕生(Toolbar & Status bar) / 440 滑鼠拖放(Drag and Drop) / 442 訊息映射(Message Map) / 445 標準選單 File/Edit/View/Window/Help / 446 對話盒/ 449 改用CEditView / 450 第㆕篇 深入 ...

    C++MFC教程

    |------ 4.D 利用AppWizard创建并使用ToolBar StatusBar Dialog Bar |------ 4.E General Window |------ 4.F 关于WM_NOTIFY的使用方法 +-- 第五章 对话框 |------ 5.1 使用资源编辑器编辑对话框 |------ 5.2 创建有...

    c++ mfc入门教程

    4.D 利用 AppWizard 创建并使用 ToolBar StatusBar Dialog Bar ............................... 51 4.E General Window ..........................................................................................

    类似qq的开发

    The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap using the resource editor, and update the IDR_MAINFRAME TOOLBAR array in ChatClient.rc to add ...

    StringToken

    The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap along with the array in MainFrm.cpp to add more toolbar buttons. AppWizard creates one ...

    细胞模式统计识别的VC++实现

    The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap using the resource editor, and update the IDR_MAINFRAME TOOLBAR array in Cell.rc to add toolbar...

    细胞识别统计系统

    The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap using the resource editor, and update the IDR_MAINFRAME TOOLBAR array in Cell.rc to add ...

    mydraw绘图软件

    The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap using the resource editor, and update the IDR_MAINFRAME TOOLBAR array in MyDraw.rc to add ...

Global site tag (gtag.js) - Google Analytics