欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MFC学习笔记(一): 不用MFC向导如何新建一个MFC程序

程序员文章站 2022-12-28 10:11:07
使用Visual Studio新建一个空项目,项目命名为HelloMFC,完成后,打开项目属性页面,将配置属性选项卡中的常规项打开,将其中的MFC的使用属性栏改为:在静态库中使用MFC或者在共享DLL中使用MFC,同时将字符集属性栏改为使用Unicode字符集。 图1-1 新建一个空项目 图1-2 ......

  使用visual studio新建一个空项目,项目命名为hellomfc,完成后,打开项目属性页面,将配置属性选项卡中的常规项打开,将其中的mfc的使用属性栏改为:在静态库中使用mfc或者在共享dll中使用mfc,同时将字符集属性栏改为使用unicode字符集。

MFC学习笔记(一): 不用MFC向导如何新建一个MFC程序

 

1-1 新建一个空项目

MFC学习笔记(一): 不用MFC向导如何新建一个MFC程序

1-2 更改项目属性

   完成以上更改后,在项目头文件中添加新建项头文件,命名为hello.h,头文件代码如下:

 1 class cmyapp : public cwinapp
 2 {
 3 public:
 4     virtual bool initinstance();
 5 };
 6 
 7 class cmainwindow : public cframewnd
 8 {
 9 public:
10     cmainwindow();
11 protected:
12     afx_msg void onpaint();
13     declare_message_map()
14 };

   同时在项目源文件中添加新建项c++文件,命名为hello.cpp,源文件代码如下:

 1 #include <afxwin.h>
 2 #include "hello.h"
 3 
 4 cmyapp myapp;
 5 
 6 bool cmyapp::initinstance()
 7 {
 8     m_pmainwnd = new cmainwindow();
 9     m_pmainwnd->showwindow(m_ncmdshow);
10     m_pmainwnd->updatedata();
11     return true;
12 }
13 
14 begin_message_map(cmainwindow, cframewnd)
15     on_wm_paint()
16 end_message_map()
17 
18 cmainwindow::cmainwindow()
19 {
20     create(null, _t("the hello application"));
21 }
22 
23 void cmainwindow::onpaint()
24 {
25     cpaintdc dc(this);
26 
27     crect rect;
28     getclientrect(&rect);
29 
30     dc.drawtext(_t("hello, mfc"), -1, &rect,
31         dt_singleline | dt_center | dt_vcenter);
32 }

  最后运行程序,会在窗口*画出hello mfc字样。

MFC学习笔记(一): 不用MFC向导如何新建一个MFC程序

1-3 程序运行结果