MFC程序设计制作_第1页
MFC程序设计制作_第2页
MFC程序设计制作_第3页
MFC程序设计制作_第4页
MFC程序设计制作_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、mfc程序设计实验1 新建进入 vc+ 集成环境,选择“新建” ,单击“ mfc appwizard(exe)” ,进入该向导。注意: 项目名为 ex1 mfc appwizard(exe)向导步骤 step1 :选择文档类型; ( 选择多文档 ) step2 :选择是否在应用程序中包含数据库支持系统; step3 :选择是否在应用程序中包含组合文件支持系统; step4 :选择图形用户接口的有关选项;mfc程序设计实验2 step5 :选择一些其他相关的信息(应用程序风格、产生源文件备注选项、mfc类库的链接方式) step6 :关注新项目中各类和文件的名称。(由 appwizard程序生成

2、的 5 个类 ) 3. 熟悉项目工作区的三个视图: mfc程序设计实验3 其中:classview 类视图:程序中用到相关的类定义及成员。resourceview 资源视图:工具拦、对话框、菜单等资源。fileview文件视图:各种相关 .h 和.cpp 程序。mfc 中的菜单制作目的和要求 : 掌握 mfc 菜单制作方法;上机步骤:继续上一个实验:1. 在资源视图 (resourceview) 中,双击 menu选项,如图:到菜单后面的空矩形区 ( 红色椭圆区 )按右键:选择属性( properties )结果为:mfc程序设计实验4 在菜单“程序测试”下方框上,按右键再选择属性(prope

3、rties)基本菜单制作完毕。思考题:1. 菜单“程序测试”和“南京市公园分布”中,属性对话框有何区别?把选项框(方格)后的英语单词翻译成合适的汉语。2. 在菜单属性中, prompt 后面填上一句话,执行该程序,大鼠标移动到该菜单上,观察变化。实验三mfc 中的对话框制作目的和要求 : 掌握 mfc 对话框制作方法;掌握常用控件的添加、设置;mfc程序设计实验5 理解各种控件的作用;上机步骤:继续上一个实验:1. 在资源视图 (resourceview) 中展开,在dialog上单击右键,选择inest dialog ,如图:2. 添加控件:在原来只有两个命令按钮的基础上,利用右边的控件工具

4、栏,在对话框中,依此添加控件。有单选按钮、检查框、组合框、列表框等。常用控件符号及名称如图:mfc程序设计实验6 添加控件后:修改相关控件的属性,修改控件id 和 caption( 名字) :a. 调整单选按钮:mfc程序设计实验7 b.检查框c.命令按钮d.调整对话框属性:id 改为 idd_dialog_ctrl: mfc程序设计实验8 ( 注意:观察整个对话属性的改变) 。3. 生成对话框类双击对话框标题栏或在对话框按右键,选择classwizard ,系统会弹出如图形式的对话框。选择: “create a new class” ,然后“ ok ” ,显示如下对话框,对话框类名为:tes

5、t ,按“ ok ” 。mfc程序设计实验9 4. 观察 classview,应该看到 test类已经在其中。实验四为控件建立关联的数据成员目的和要求 : 理解数据成员的作用;( 注解:我们在类和对象中称为成员数据或数据成员,其英文单词variable词义是变量,所以有些书籍中也称为成员变量) 。上机步骤:要控制控件的行为,必须通过为控件建立关联的数据成员来实现。在上个实验中,我们建立了相关的控件,现在需要建立它们的数据成员。1. 在对话框上,按右键,选择classwizard ,在 classwizard 中,选择 member variable选项卡,为组合框和列表框建立数据成员。mfc程

6、序设计实验10 a. 选中 idc_combo1, 按 “add variable ” 命令按钮,定义数据成员名为m_comctl 。选择 category 为 control 。(注意红圈里的和你屏幕上的对照) 。b. 选中 idc_list1, 按 “add variable ” 命令按钮,定义数据成员名为m_listctl 。选择 category 为 control 。mfc程序设计实验11 3. 定义好以后,在 classview 中,可以看到定义的两个数据成员:m_comctl和m_listctl 。4. 打开 test.h文件,观察这两个数据成员在类中的位置。test.h程序清单

7、:#if !defined(afx_test_h_df5afb9e_81a2_4386_ac6a_29972981e9b7_included_) #define afx_test_h_df5afb9e_81a2_4386_ac6a_29972981e9b7_included_ #if _msc_ver 1000 #pragma once #endif / _msc_ver 1000 / test.h : header file / mfc程序设计实验12 / / test dialog class test : public cdialog / construction public: tes

8、t(cwnd* pparent = null); / standard constructor / dialog data /afx_data(test) enum idd = idd_dialog_ctrl ; clistbox m_listctl; ccombobox m_comctl; /afx_data / overrides / classwizard generated virtual function overrides /afx_virtual(test) protected: virtual void dodataexchange(cdataexchange* pdx); /

9、 ddx/ddv support /afx_virtual / implementation protected: / generated message map functions /afx_msg(test) / note: the classwizard will add member functions here /afx_msg declare_message_map() ; /afx_insert_location / microsoft visual c+ will insert additional declarations immediately before the pre

10、vious line. #endif / !defined(afx_test_h_df5afb9e_81a2_4386_ac6a_29972981e9b7_included_) mfc程序设计实验13 5. 加入其它数据成员a. 在 classvi ew 视图中;b. 选择 test 类;c. 按右键,选择 add member variable ;d. 添加二维数组 str1 成员e. 通过同样方法增加其它数据成员,最终结果为:数据成员名数据类型访问权限str1210 char public str2210 char public dis_str11020 char public mfc程序

11、设计实验14 dis_str21020 char public dis_str31020 char public dis_str41020 char public have_check bool public 观察类的变化和test.h程序清单。6. 为数据成员初始化有了数据成员,需要进行初始化工作。在对话框打开 classwizard ,为 test类建立 wm_initdialog 消息映射。接受系统默认的函数名oninitdialog(),添加方法:在 member function ,增加了一行:按 edit code 按钮:添加后的代码:bool test:oninitdialog(

12、) cdialog:oninitdialog(); / todo: add extra initialization here strcpy(str10,玄武区 ); strcpy(str11,白下区 ); strcpy(str20,下关区 ); strcpy(str21,鼓楼区 ); strcpy(dis_str10,玄武湖 ); strcpy(dis_str11,情侣园 ); strcpy(dis_str12,白马公园 ); strcpy(dis_str20,月牙湖 ); mfc程序设计实验15 strcpy(dis_str21,中山陵 ); strcpy(dis_str22,郑和公园 )

13、; strcpy(dis_str30,望江楼 ); strcpy(dis_str31,挹江门 ); strcpy(dis_str32,绣球公园 ); strcpy(dis_str40,鼓楼公园 ); strcpy(dis_str41,古林公园 ); strcpy(dis_str42,国防园 ); m_comctl.addstring(lpctstr)str10); /addstring函数是向组合框中添加字符串m_comctl.addstring(lpctstr)str11); m_listctl.setcursel(0);/在列表框中选定一字符串have_check=false; m_lis

14、tctl.enablewindow(false); return true; / return true unless you set the focus to a control / exception: ocx property pages should return false 实验五建立消息映射和响应函数目的和要求 : 理解消息映射的概念和作用掌握建立消息映射的方法;上机步骤:前面我们所建立的对话框, 在运行程序的时候,一是不可用,二是操作该对话框,程序也没有反应,要使得对话框中的控件,在操作过程中作出反应,就需要建立消息映射以及该消息程序所作出的反应响应函数。继续上一个实验:1. 在

15、对话框中打开classwizard( 如图) mfc程序设计实验16 建立下列消息映射:id 消息响应函数idc_radio_east bn_clicked onradioeast idc_radio_west bn_clicked onradiowest idc_check1 bn_clicked oncheck1 idc_combo1 cbn_selchange onselchangecombo1 test.h程序清单中就会包含这些消息映射。#if !defined(afx_test_h_df5afb9e_81a2_4386_ac6a_29972981e9b7_included_) #de

16、fine afx_test_h_df5afb9e_81a2_4386_ac6a_29972981e9b7_included_ #if _msc_ver 1000 #pragma once #endif / _msc_ver 1000 / test.h : header file / / / test dialog mfc程序设计实验17 class test : public cdialog / construction public: bool have_check; char dis_str41020; char dis_str31020; char dis_str21020; char

17、dis_str11020; char str2210; char str1210; test(cwnd* pparent = null); / standard constructor / dialog data /afx_data(test) enum idd = idd_dialog_ctrl ; clistbox m_listctl; ccombobox m_comctl; /afx_data / overrides / classwizard generated virtual function overrides /afx_virtual(test) protected: virtu

18、al void dodataexchange(cdataexchange* pdx); / ddx/ddv support /afx_virtual / implementation protected: / generated message map functions /afx_msg(test) virtual bool oninitdialog(); afx_msg void onradioeast(); afx_msg void onradiowest(); afx_msg void oncheck1(); afx_msg void onselchangecombo1(); /afx

19、_msg declare_message_map() ; mfc程序设计实验18 /afx_insert_location / microsoft visual c+ will insert additional declarations immediately before the previous line. #endif / !defined(afx_test_h_df5afb9e_81a2_4386_ac6a_29972981e9b7_included_) 特别注意:程序中/afx_msg(test) ,/afx_msg 在过去 c+ 程序设计中,这是注解,可以有也可以删除,但这里不允

20、许更改,它是用作消息映射表的开始与结束部分。(参考实验要求 ) test.cpp程序清单:/ test.cpp : implementation file / #include stdafx.h #include ex1.h #include test.h #ifdef _debug #define new debug_new #undef this_file static char this_file = _file_; #endif / / test dialog test:test(cwnd* pparent /*=null*/) : cdialog(test:idd, pparent)

21、 /afx_data_init(test) / note: the classwizard will add member initialization here /afx_data_init void test:dodataexchange(cdataexchange* pdx) cdialog:dodataexchange(pdx); /afx_data_map(test) mfc程序设计实验19 ddx_control(pdx, idc_list1, m_listctl); ddx_control(pdx, idc_combo1, m_comctl); /afx_data_map beg

22、in_message_map(test, cdialog) /afx_msg_map(test) on_bn_clicked(idc_radio_east, onradioeast) on_bn_clicked(idc_radio_west, onradiowest) on_bn_clicked(idc_check1, oncheck1) on_cbn_selchange(idc_combo1, onselchangecombo1) /afx_msg_map end_message_map() / / test message handlers bool test:oninitdialog()

23、 cdialog:oninitdialog(); / todo: add extra initialization here strcpy(str10,玄武区 ); strcpy(str11,白下区 ); strcpy(str20,下关区 ); strcpy(str21,鼓楼区 ); strcpy(dis_str10,玄武湖 ); strcpy(dis_str11,情侣园 ); strcpy(dis_str12,白马公园 ); strcpy(dis_str20,月牙湖 ); strcpy(dis_str21,中山陵 ); strcpy(dis_str22,郑和公园 ); strcpy(dis_

24、str30,望江楼 ); strcpy(dis_str31,挹江门 ); strcpy(dis_str32,绣球公园 ); strcpy(dis_str40,鼓楼公园 ); strcpy(dis_str41,古林公元 ); strcpy(dis_str42,国防园 ); m_comctl.addstring(lpctstr)str10); m_comctl.addstring(lpctstr)str11); m_listctl.setcursel(0); have_check=false; mfc程序设计实验20 m_listctl.enablewindow(false); return tr

25、ue; / return true unless you set the focus to a control / exception: ocx property pages should return false void test:onradioeast() / todo: add your control notification handler code here void test:onradiowest() / todo: add your control notification handler code here void test:oncheck1() / todo: add

26、 your control notification handler code here void test:onselchangecombo1() / todo: add your control notification handler code here test.cpp中的是消息响应函数。2. 添加扩充响应函数 (填写响应函数的实际内容) ( 在 test.cpp程序后部 ) a. 单选按钮映射函数b. 检查框映射函数c. 组合框映射函数test.cpp部分扩充后的程序清单:void test:onradioeast() / todo: add your control notific

27、ation handler code here m_comctl.resetcontent(); m_comctl.addstring(lpctstr)str10); m_comctl.addstring(lpctstr)str11); m_comctl.setcursel(0); mfc程序设计实验21 void test:onradiowest() / todo: add your control notification handler code here m_comctl.resetcontent(); m_comctl.addstring(lpctstr)str20); m_comc

28、tl.addstring(lpctstr)str21); m_comctl.setcursel(0); void test:oncheck1() / todo: add your control notification handler code here if(have_check) have_check=false; m_listctl.resetcontent(); m_listctl.enablewindow(false); else have_check=true; m_listctl.resetcontent(); m_listctl.enablewindow(true); voi

29、d test:onselchangecombo1() / todo: add your control notification handler code here int i; cstring v_str; if(have_check) m_comctl.getlbtext(m_comctl.getcursel(),v_str); if(v_str=(cstring)str10) m_listctl.resetcontent(); for(i=0;i3;i+) m_listctl.addstring(lpctstr)dis_str1i); m_listctl.setcursel(0); if

30、(v_str=(cstring)str11) mfc程序设计实验22 m_listctl.resetcontent(); for(i=0;i3;i+) m_listctl.addstring(lpctstr)dis_str2i); m_listctl.setcursel(0); if(v_str=(cstring)str20) m_listctl.resetcontent(); for(i=0;i3;i+) m_listctl.addstring(lpctstr)dis_str3i); m_listctl.setcursel(0); if(v_str=(cstring)str21) m_lis

31、tctl.resetcontent(); for(i=0;i3;i+) m_listctl.addstring(lpctstr)dis_str4i); m_listctl.setcursel(0); 实验六编译与运行目的和要求 : 理解程序各个部分所完成的功能掌握通过菜单调用对话框的实现方法;理解程序的编译、连接和运行过程。上机步骤:前面我们所建立的对话框,在运行程序的时候,并不可用,要使用对话框,需要在菜单上添加调用对话框项,才能完成程序。那么成是如何运行的呢?程序运行包括程序的初始化、 运行和结束工作是由应用程序通过定义类的实例(对象)来控制完成的。对于非windows应用程序,均有一个main 函数,对于 windows应用程序,均有一个winmain 函数。由于对任一个mfc 应用程序,win

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论