MFC—课程设计
void CTriangle::Serialize(CArchive &ar) void CCircle::Serialize(CArchive &ar) void CEllipse::Serialize(CArchive &ar) void CText::Serialize(CArchive &ar) 10 CSquare() CSquare() CRectangle() CCircle() CEllipse() CTriangle() Ctext() CSquare::CSquare() Shape.cpp 缺省构造函数,初始化 接受由对话框传递过来的参数 CSquare::CSquare() CRectangle::CRectangle() CCircle::CCircle() 11 Shape.cpp CEllipse::CEllipse() CTriangle::CTriangle() CText::CText() void CSquare::Draw(CDC*pDC) void CRectangle::Draw(CDC*pDC) void CCircle::Draw(CDC*pDC) 12 Draw(CDC*pDC) Shape.cpp void CEllipse::Draw(CDC*pDC) void CTriangle::Draw(CDC*pDC) void CText::Draw(CDC*pDC) bool CSquare::IsMatched(CPoint pnt) bool CRectangle::IsMatched(CPoint pnt) bool CCircle::IsMatched(CPoint pnt) 13 IsMatched(CPoint pnt) Shape.cpp bool CEllipse::IsMatched(CPoint pnt) bool CTriangle::IsMatched(CPoint pnt) bool CText::IsMatched(CPoint pnt) 绘制函数 图元匹配函数
图3.主要函数OnLButtonDown()
6
MFC—课程设计
2.工程的新建
打开Microsoft Visual C++ 6.0 —> 点击File —> New 在Projects中选择 MFC AppWizard (exe) ; 在 Project name中输入工程的名字Lwh,在 Location中选择工程存放的路径。填完后点击OK按钮。填完后点击OK按钮,在弹出来的对话框中我们选择Single document,再点击Finish,完成工程的创建。之后编译运行,结果参见图3。
图4.新建工程结果示意图
3.建立图元属性对话框
点击ResouceView,右击Dialog—>插入Dialog,这时会弹出来一个对话框,双击“确定”按钮,选择“Create a new class”,点击OK,设置类名为CType—>OK,之后设置对话框属性为IDD_TYType,参见图4,绘制对话框,参见附表2.属性表给各复选框、编辑框、按钮修改属性。 附表2
7
MFC—课程设计
属性表
序号 1 名称 ID IDC_m_ComBox_TuYuanType IDC_LineColorButton 7 角度 IDC_Angle 图元类型 8 线型 线色(R/G/B) 填充色(R/G/B) 原点X 原点Y IDC_m_ComBoxLineType 2 线色按钮 9 IDC_LineColor(R/G/B) 3 填充色按钮 IDC_FillColorButton 10 4 5 6
高度 宽度 线宽 IDC_High IDC_Width IDC_LineWidth 11 12 IDC_FillColor(R/G/B) IDC_OrgX IDC_OrgY 13 填充风格 IDC_m_ComBoxFillType
图5.构造对话框结果示意图
8
MFC—课程设计
4.控件的关联
要实现这个步骤,首先要明白在MFC中变量的声明定义,对于控件关联型的数值变量,value类型(double,int,CString等等),在生成时向导帮你做了3件事:
1)在.h文件中进行声明;
2)在.cpp中的构造函数中,进行了初始化;
3)在.cpp文件中的DoDataExchange()中进行控件关联;
DDX_Text(pDX, IDOK, m_btnTest);
所以,要进行控件关联,要做的也是这三件事(可在ClassWizard中进行添加,则系统自动生成。下面介绍手动添加,以图元类型IDC_m_ComBox_TuYuanType为例)
1) 在Type.h中声明: protected:
CComboBox m_ComBox_TuYuanType;
2)在Type.cpp中进行初始化:
BOOL CType::OnInitDialog() (在ClassWizard中添加) {
CDialog::OnInitDialog();
// TODO: Add extra initialization here m_ComBox_TuYuanType.AddString(\正方形\m_ComBox_TuYuanType.AddString(\矩形\m_ComBox_TuYuanType.AddString(\三角形\m_ComBox_TuYuanType.AddString(\圆\m_ComBox_TuYuanType.AddString(\椭圆\m_ComBox_TuYuanType.AddString(\文本\m_ComBox_TuYuanType.SetCurSel(0); }
3)在Type.cpp中进行控件关联(DoDataExchange()中):
DDX_Control(pDX, IDC_ComBox_TuYuanType, m_ComBox_TuYuanType);
9
MFC—课程设计
5.枚举型
枚举类型定义的一般形式为:enum 枚举名{ 枚举值表 }。在枚举值表中应罗列出所有可用值。这些值也称为枚举元素。注意枚举值是常量,不是变量,不能再对它赋值。 枚举元素本身由系统定义了一个表示序号的数值,从0开始顺序定义为0,1,2?? 1)定义(Type.h) public:
EnumTuYuanTyp m_TuYuanTyp; 2)枚举(Type.h) enum EnumTuYuanTyp {
EnumTuYuanTyp_Square,//正方形 EnumTuYuanTyp_Rectangle,//矩形 EnumTuYuanTyp_Triangle,//三角形 EnumTuYuanTyp_Circle,// 圆 EnumTuYuanTyp_Ellipse,// EnumTuYuanTyp_Text// 文本 };
3)利用枚举型传参(Type.cpp) void CType::OnOK() {
UpdateData();
// TODO: Add extra validation here
int iSel=m_ComBox_TuYuanType.GetCurSel();//得到当前选择的索引值
椭圆
switch(iSel)
{ case 0:
m_TuYuanTyp=EnumTuYuanTyp_Square;
10