一、建立基于对话框工程,改变UI对象

1.改变菜单、工具栏
在资源栏中编辑菜单、工具栏,注意ID
编辑ID
【Pen/Thick Line】
ID : ID_PEN_THICK_OR_THIN
prompt : "Toggles the line thickness between thin and thick\nToggle pen"
【Pen/Pen Widths】
ID : ID_PEN_WIDTHS
prompt : "Sets the size of the thin and thick pen\nPen thickness"
【Edit/Clear All】
ID : ID_EDIT_CLEAR_ALL
prompt : "Clears the drawing\nErase All"
 
2.连接ID与处理函数
①添加成员函数
void CScribbleDoc::OnEditClearAll()
{
DeleteContents();
SetModifiedFlag(); // Mark the document as having been modified, for
// purposes of confirming File Close.
UpdateAllViews(NULL);
}
 
void CScribbleDoc::OnPenThickOrThin()
{
// Toggle the state of the pen between thin or thick.
m_bThickPen = !m_bThickPen;
 
// Change the current pen to reflect the new user-specified width.
ReplacePen();
}
 
void CScribbleDoc::ReplacePen()
{
m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
 
// Change the current pen to reflect the new user-specified width.
m_penCur.DeleteObject();
m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
}
 
②申明函数
protected:
void ReplacePen();
 
③添加成员变量
protected:
// The document keeps track of the current pen width on
// behalf of all views. We'd like the user interface of
// Scribble to be such that if the user chooses the Draw
// Thick Line command, it will apply to all views, not just
// the view that currently has the focus.
 
UINT m_nPenWidth; // current user-selected pen width
BOOL m_bThickPen; // TRUE if current pen is thick
UINT m_nThinWidth;
UINT m_nThickWidth;
CPen m_penCur; // pen created according to
  
 
④初始化函数
void CScribbleDoc::InitDocument()
{
m_bThickPen = FALSE;
m_nThinWidth = 2; // default thin pen is 2 pixels wide
m_nThickWidth = 5; // default thick pen is 5 pixels wide
ReplacePen(); // initialize pen according to current width
}
 
3.维护UI对象状态
Enable:灰色或正常
SetCheck:打勾或没打勾
 
void CScribbleDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_strokeList.IsEmpty());
}
 
 
void CScribbleDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bThickPen);
}
 
 
 
二、MFC与对话框
 
1.新建对话框,编辑对话框控件:Edit控件、Static控件、按钮
 
注意对话框的属性ID IDD_PEN_WIDTHS及控件属性ID
 
2.连接对话框与专用类
生成对话框.cpp和.h文件
3.对话框消息处理函数
 
4.对话框数据交换与校验(DDX&DDV)
DDX :是让我们把对话框类别中的成员变量与对话框中的控制组件产生关联,于是
当对话框结束时,控制组件的内容会自动传输到这些成员变量上。
DDV: 是允许我们设定对话框控制组件的内容类型以及资料(数值) 范围。
 
5.调用对话框
【Pen Widths】对话框是一个所谓的Modal 对话框,意思是除非它关闭(结束),否则
它会紧抓住这个程序的控制权,但不影响其它程序。相对于Modal 对话框,有一种
Modeless 对话框就不会影响程序其它动作的进行;通常你在文字处理软件中看到的文字
搜寻对话框就是Modeless 对话框。
 
我们希望Step3 的命令项【Pen/Pen Widths】被按下时,【Pen Widths】对话框能够执行
起来。要唤起此一对话框,得做到两件事情:
1. 产生一个CPenWidthsDlg 对象,负责管理对话框。
2. 显示对话框窗口。这很简单,调用DoMoal 即可办到。
编辑函数,注意头文件名称,与之前设置对应。
// SCRIBDOC.CPP
#include "pendlg.h"
...
void CScribbleDoc::OnPenWidths()
{
CPenWidthsDlg dlg;
// Initialize dialog data
dlg.m_nThinWidth = m_nThinWidth;
dlg.m_nThickWidth = m_nThickWidth;
// Invoke the dialog box
if (dlg.DoModal() == IDOK)
{
// retrieve the dialog data
m_nThinWidth = dlg.m_nThinWidth;
m_nThickWidth = dlg.m_nThickWidth;
// Update the pen that is used by views when drawing new strokes,
// to reflect the new pen width definitions for "thick" and "thin".
ReplacePen();
}
}

最新文章

  1. cached过高导致内存溢出 java head space
  2. 【HDOJ】2149 Public Sale
  3. python初探-数据类型
  4. 利用WebLog Experet分析日志获取性能需求
  5. 文本三剑客---awk(gawk)基础
  6. logback使用注意点1
  7. OpenSSL-Win64创建IdentityServer证书
  8. PHPStorm + Homestead + Xdebug + Chrome Xdebug Helper 调试配置
  9. Go Example--defer
  10. Image Lazy Load:那些延时加载图片的开源插件(jQuery)
  11. day03用户交互、基本数据类型、运算符
  12. React Native API之 ActionSheetIOS
  13. 【贪心算法】POJ-2393 简单贪心水题
  14. 给MySQL中某表增加一个新字段,设为主键值为自动增长。
  15. CIE-LUV是什么颜色特征
  16. PostgreSQL 连接的问题
  17. 毕业 迷茫 继续OR放弃?
  18. 【SAM】BZOJ3998-弦论
  19. MyBatis联合查询association使用
  20. MVC身份验证.MVC过滤器.MVC6关键字Task,Async.前端模拟表单验证,提交.自定义匿名集合.Edge导出到Excel.BootstrapTree树状菜单的全选和反选.bootstrap可搜索可多选可全选下拉框

热门文章

  1. [Notes-DS-2]线性结构
  2. pands 编码知识
  3. VMware Workstation 未能启动VMware Authentication Service
  4. prepare
  5. C# 数据结构之嵌套加法、嵌套乘法
  6. SVN: E155004: THERE ARE UNFINISHED WORK ITEMS IN ''; RUN 'SVN CLEANUP' FIRST
  7. fetch 小分析
  8. Outlook网页版怎么设置自动回复?节假日怎么设置邮件自动回复?
  9. 【TensorFlow】InternalError: Failed copying input tensor
  10. 关于centos防火墙的一些问题