MFC学习记录2

一、建立基于对话框工程,改变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();
}
}

 

posted @ 2017-09-06 21:05  920101yz  Views(194)  Comments(0Edit  收藏  举报