MFC 框架程序的分析认知与编程实践.docx
- 文档编号:23812440
- 上传时间:2023-05-21
- 格式:DOCX
- 页数:17
- 大小:387.50KB
MFC 框架程序的分析认知与编程实践.docx
《MFC 框架程序的分析认知与编程实践.docx》由会员分享,可在线阅读,更多相关《MFC 框架程序的分析认知与编程实践.docx(17页珍藏版)》请在冰豆网上搜索。
MFC框架程序的分析认知与编程实践
------MFC框架程序的分析认知与编程实践
姓名:
专业:
学号:
2012.3.24
Windows编程实验二
----MFC框架程序的分析认知与编程实践
一.实验目的:
1.熟悉在VisualC++6.0IDE中编辑、编译、调试和运行一个MFC应用程序的基本思路;
2.理解并掌握利用MFC开发应用程序的一般步骤和过程;
3.加深对MFC框架程序的特性认识,掌握其应用方法。
二.实验要求:
1.要求同学们能对利用MFCAppWizard、ClassWizard等实用工具编写Wi的方法、步骤有一个较为全面的了解,并注意理解由MFCAppWizard所生成的WinApp框架的组织架构和作用;
2.深刻理解在此基础上开发WinApp的主要工作是“填充框架和添加必要的实现代码”的含义,切实做到用理论指导实践,以实践促进理论,提高实际动手能力和再学习的能力。
三、实验内容:
题目一:
程序设计题:
以MFC编程方式,编写并实现一个简易计算器功能的应用程序。
其界面架构形式和具体内容自我设计,原则:
架构美观、布局合理,内容实用,具有可计算特性。
1.打开VisualC++6.0,新建一个MFC文档,按照要求添加代码。
2.在代码编辑窗口内添加如下代码:
#include"stdafx.h"
#include"456.h"
#include"456Dlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMy456Dlgdialog
CMy456Dlg:
:
CMy456Dlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMy456Dlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CMy456Dlg)
m_operator=-1;
m_num1=0.0;
m_num2=0.0;
m_result=0.0;
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCMy456Dlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy456Dlg)
DDX_Radio(pDX,IDC_ADD,m_operator);
DDX_Text(pDX,IDC_NUM1,m_num1);
DDX_Text(pDX,IDC_NUM2,m_num2);
DDX_Text(pDX,IDC_RESULT,m_result);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy456Dlg,CDialog)
//{{AFX_MSG_MAP(CMy456Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_NUM1,OnEnChangeNum1)
ON_BN_CLICKED(IDC_ADD,OnBnClickedAdd)
ON_BN_CLICKED(IDC_SUB,OnBnClickedSub)
ON_BN_CLICKED(IDC_MUL,OnBnClickedMul)
ON_BN_CLICKED(IDC_DIV,OnBnClickedDiv)
ON_EN_CHANGE(IDC_NUM2,OnEnChangeNum2)
ON_BN_CLICKED(IDC_RESET,OnBnClickedReset)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMy456Dlgmessagehandlers
BOOLCMy456Dlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCMy456Dlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCMy456Dlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCMy456Dlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
//TODO:
IfthisisaRICHEDITcontrol,thecontrolwillnot
//sendthisnotificationunlessyouoverridetheCDialog:
:
OnInitDialog()
//functionandcallCRichEditCtrl().SetEventMask()
//withtheENM_CHANGEflagORedintothemask.
//TODO:
Addyourcontrolnotificationhandlercodehere
voidCMy456Dlg:
:
OnBnClickedAdd()
{
UpdateData(TRUE);
m_result=m_num1+m_num2;
UpdateData(FALSE);
//TODO:
Addyourcontrolnotificationhandlercodehere
}
voidCMy456Dlg:
:
OnBnClickedSub()
{
UpdateData(TRUE);
m_result=m_num1-m_num2;
UpdateData(FALSE);
//TODO:
Addyourcontrolnotificationhandlercodehere
}
voidCMy456Dlg:
:
OnBnClickedMul()
{UpdateData(TRUE);
m_result=m_num1*m_num2;
UpdateData(FALSE);
//TODO:
Addyourcontrolnotificationhandlercodehere
}
voidCMy456Dlg:
:
OnBnClickedDiv()
{
UpdateData(TRUE);
m_result=m_num1/m_num2;
UpdateData(FALSE);
//TODO:
Addyourcontrolnotificationhandlercodehere
}
voidCMy456Dlg:
:
OnEnChangeNum1()
{
}
voidCMy456Dlg:
:
OnEnChangeNum2()
{
//TODO:
IfthisisaRICHEDITcontrol,thecontrolwillnot
//sendthisnotificationunlessyouoverridetheCDialog:
:
OnInitDialog()
//functionandcallCRichEditCtrl().SetEventMask()
//withtheENM_CHANGEflagORedintothemask.
//TODO:
Addyourcontrolnotificationhandlercodehere
}
voidCMy456Dlg:
:
OnBnClickedReset()
{UpdateData(TRUE);
m_result=m_num1=m_num2=m_operator=0;
UpdateData(FALSE);
//TODO:
Addyourcontrolnotificationhandlercodehere
}
3.选择单击工具栏上的
按钮,进行调试,未出现错误则单击
按钮运行程序,查看运行结果。
4.保存程序代码,进行分析。
题目二.以MFC编程方式,编写并实现一个SDI界面的“奥运五环”绘图应用程序。
(提高)提示:
编写在窗口客户区上“绘制一张笑脸”的WinApp。
1.打开VisualC++6.0,建立MFC文档对话,打开添加的文件进行编译。
2.在代码编辑窗口内添加如下代码:
//��View.cpp:
implementationoftheCMyViewclass
//
#include"stdafx.h"
#include"��.h"
#include"��Doc.h"
#include"��View.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CMyView
IMPLEMENT_DYNCREATE(CMyView,CView)
BEGIN_MESSAGE_MAP(CMyView,CView)
//{{AFX_MSG_MAP(CMyView)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG_MAP
//Standardprintingcommands
ON_COMMAND(ID_FILE_PRINT,CView:
:
OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT,CView:
:
OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CView:
:
OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMyViewconstruction/destruction
CMyView:
:
CMyView()
{
//TODO:
addconstructioncodehere
}
CMyView:
:
~CMyView()
{
}
BOOLCMyView:
:
PreCreateWindow(CREATESTRUCT&cs)
{
//TODO:
ModifytheWindowclassorstylesherebymodifying
//theCREATESTRUCTcs
returnCView:
:
PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
//CMyViewdrawing
voidCMyView:
:
OnDraw(CDC*pDC)
{
CMyDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
CPenpen,*oldpen;
pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
oldpen=pDC->SelectObject(&pen);
pDC->Ellipse(275,170,425,320);
pDC->Arc(360,215,410,240,410,225,360,225);
pDC->Arc(290,215,340,240,340,225,290,225);
pDC->Arc(320,240,380,300,320,270,380,270);
pDC->SelectObject(oldpen);
//TODO:
adddrawcodefornativedatahere
}
/////////////////////////////////////////////////////////////////////////////
//CMyViewprinting
BOOLCMyView:
:
OnPreparePrinting(CPrintInfo*pInfo)
{
//defaultpreparation
returnDoPreparePrinting(pInfo);
}
voidCMyView:
:
OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
{
//TODO:
addextrainitializationbeforeprinting
}
voidCMyView:
:
OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
{
//TODO:
addcleanupafterprinting
}
/////////////////////////////////////////////////////////////////////////////
//CMyViewdiagnostics
#ifdef_DEBUG
voidCMyView:
:
AssertValid()const
{
CView:
:
AssertValid();
}
voidCMyView:
:
Dump(CDumpContext&dc)const
{
CView:
:
Dump(dc);
}
CMyDoc*CMyView:
:
GetDocument()//non-debugversionisinline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return(CMyDoc*)m_pDocument;
}
#endif//_DEBUG
/////////////////////////////////////////////////////////////////////////////
//CMyViewmessagehandlers
3.选择单击工具栏上的
按钮,进行调试,未出现错误则单击
按钮运行程序,查看运行结果。
4.保存实验代码,分析程序。
4.实验总结:
1.MFC类的组织结构划分为:
1)根类:
CObject;2)应用程序体系结构类;3)窗口、对话框和控件类;4)绘图和打印类;5)简单数据类型类;6)数组、列表和映射类;7)文件和数据库类;8)Internet和网络工作类;9)OLE类;10)调试和异常类。
2.控件的创建分为静态创建(指在对话框模板创建控件,并指定控件属性。
);动态创建(指在程序运行中椐需要定义一个控件对象,再通过CreateWindow()/CreateWindowEx()创建控件,ShowWindow()显示控件。
如同操作一个子窗口。
3.静态控件不发送消息;其它控件对于用户的操作都能发送消息。
不同类的控件发送的消息通知代码是不相同的。
.....
5.实验心得:
................
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MFC 框架程序的分析认知与编程实践 框架 程序 分析 认知 编程 实践