MFC Windows 程序设计---Hello MFC!

10 篇文章 0 订阅

1 源代码如下 :

HelloMFC.h

#include<afxwin.h>

class CMyApp : public CWinApp
{
public:
	virtual BOOL InitInstance();
};

class CMainWindow : public CFrameWnd
{
public:
	CMainWindow();

protected:
 	afx_msg void OnPaint();
 	DECLARE_MESSAGE_MAP();
}

HelloMFC.cpp

#include"HelloMFC.h"

CMyApp myApp;

BOOL CMyApp::InitInstance()
{
	m_pMainWnd = new CMainWindow;
	m_pMainWnd -> ShowWindow(m_nCmdShow);
	m_pMainWnd -> UpdateWindow();

	return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
	Create(NULL, _T("The Hello Application"));
}

void CMainWindow::OnPaint()
{
	CPaintDC dc(this);

	CRect rect;
 	GetClientRect(&rect);

	dc.DrawText(_T("Hello,MFC"), -1, &rect, 
   		DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}

2 怎么使用MFC类库?

(前提新建一个工程项目) 工程 --> 设置 --> Microsoft基础类 --> 使用MFC作为静态链接库
在这里插入图片描述
在这里插入图片描述
3 结果如下图:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
此代码是随书光盘拷贝。 此版本是网上最详细的书签版本,也是最清晰的版本。 MFC Windows程序设计(第2版修订版) (Programming Windows with MFC, 2nd Edition) 基本信息 作者: (美)Jeff Prosise [作译者介绍] 译者: 北京博彦科技发展有限责任公司 出版社:清华大学出版社 ISBN:9787302150428 上架时间:2007-5-22 出版日期:2007 年5月 开本:16开 页码:1166 版次:1-1 内容简介   《mfc windows程序设计(第2版)》是对其极为经典的第1版的全面更新,本书不仅扩展了已被认为是权威的关于microsoft用于windows api的功能强大的c++类库的阐述,还新增了有关com、ole和activex的内容。本书的作者,jeff prosise,用其无与伦比的技巧向读者讲述了mfc程序设计中的基本概念和主要技术——再次阐释了在32位windows平台上进行了快速的面向对象开发的完美方法。    本书涵盖了以下专题:    事件驱动程序设计和mpc的基础知识    文档/视图体系结构    位图、调色板和区域    多线程和线程同步    mfc与组件对象模型(com)    activex控件    《mfc windows程序设计(第2版)》见解深刻,并附带有辅助学习的资源——包括在随附的cd-rom中提供了大量代码实例。    要理解如何利用mfc库中提供的成千上万的预编译、预测试的代码的优点,本书——及其作者——将是您独一无二的选择。 作译者 本书提供作译者介绍   Jeff Prosise是一位作者、教员和讲师,他以Windows编程和教授别人如何进行Windows为生。作为一位在Windows程序设计MFC和COM领域世界知名的权威,他还是《PC Magazinge》和《Microsoft Systems Journal》杂志的组稿编辑。 目录 鸣谢 序言 第ⅰ部分 windowsmfc基础  第1章 hellomfc  第2章 在窗口中绘图  第3章 鼠标和键盘  第4章 菜单  第5章 mfc集合数  第6章 文件i/o和串行化  第7章 控件  第8章 对话框和属性表 第ⅱ部分 文档/视图体系结构  第9章 文档、视图和单文档界面  第10章 滚动视图、html视图以及其他视图类型  第11章 多文档和多视图  第12章 工具栏、状态栏和组合栏  第13章 打印和打印预览 第ⅲ部分 高级篇  第14章 计时器和空闲处理  第15章 位图、调色板以及区域 . 第16章 公用控件  第17章 线程和线程同步化 第ⅳ部分 com,ole和activex  第18章 mfc和组件对象模型  第19章 剪贴板和ole拖放  第20章 automation  第21章 activex控件 序言   像我的许多同行一样,我学习Windows编程是从读Petzold的书《Windows程序设计》——一本所有Windows程序员都使用的Windows编程圣经——开始的。在刚刚成为一名MFC程序员时,那天我冲进书店,想买一本能够与ProgrammingWindows相媲美的MFC编程书籍,但是却没有结果。于是我决定自己写一本这样的书。它正是您手中所拿到的这本书,也正是当初我在初学MFC时,希望拥有的那本书。.   正如您所知,MFC是用于Windows编程的Microsoft C++类库。本书并不是一本关于C++的书,而是一本关于使用MFC而不是Windows APl,进行32位Windows应用程序编程,以访问操作系统的基本特性和服务的书。它原本为以下两种编程人员所写:   曰打算学习MFCWindowsAPl程序员   曰未进行过Windows编程的程序员   无论您属于哪种人,我假设您已经了解C++,而且熟悉C++语法,如派生类及虚函数。如果是这样的话,您已在征肠MFC编程之山的历险中迈出了坚实的第一步。   即使是有经验的Windo;。程序员在初读MFC代码时也会感到迷惑。一部分是由于VisualC++代码生成向导所生成的代码所致,另一个原因是因为无数行代码隐藏在MFC类库中。这正是本书使用特殊的方法来写MFC的缘由。本书从让您亲自手写代码开始(不用向导),使用MFCl.0的应用程序结构风格,也就是说,既不用文档也不用视图。只有在您掌:握了上述的基础知识,初步认识了简单的MFC类库如CWnd和CWinApp之后,我才开始介;绍向导并教您使用MFC的文档视图结构的方法。您将逐渐理解Windows的关键组件及它的消息驱动机制,如图形设备接口(GDl)。我相信用这种方法可以使MFC的学习变得生动有趣而不是令人气馁。我认为一旦您认真地学完这本书,然后再站在老练的Windows程序员的角度来回顾学习过程中的甜酸苦辣,您将同意我的观点。   本书分为4个部分。第1部分,介绍了WindowsMFC编程的主要原则,以一个示例程序"HelloMFC"开始,然后逐一简要讲解菜单、控件、对话框以及其他用于组建应用程序的模块。第2部分,在第1部分的基础之上,对文档视图结构做详细讲解,特别是第9、10、11章,揭示了实现文档视图的神奇之处,不仅介绍了如何编写简单的文档视图程序,而且教您一些高级功能,如打印预览和拆分窗口视图。第3部分涵盖了一些WindowsMFC的更鬲级的功能,如颜色选项板、位图句柄及多线程程序。在第4部分,您将了解到MFC如何包容COM、OLE和ActiveX,如何编写支持COM的组件及应用程序。在读完第21章之后,您将更加精通使用MFC进行Windows 32位编程的技术。您也将拥有丰富的源代码用于您的第一   第2版的新特点   读过本书第1版的朋友将会注意到第2版的两个相当明显的变化。首先,本版包括7个新章节,1章用于讲解MFC视图类,1章涵盖MFC集合类;1章介绍MFC文件I/0和串行化机制,4章讨论了MFC和COM的关系。MFC不是类似活动模板库(ATL)的通用COM框架,但是MFC使编写某些COM程序更容易,使编写ActiveX控件的过程更简单,而且它使编写自动化(Automation)服务器程序(使用COM技术来供脚本客户调用的程序)变成了二件轻而易举的事。..   第2版的主要变化是关于向导的知识。在第1版中并没有提到向导。在第2版中1-3章使用手写的示例程序,但到了第4章就使用AppWizard和ClassWizard来生成代码。这样做的原因是,我始终认为代码生成向导会影响MFC的学习,它只应由老练的程序员来使用。但我也逐渐认识到,实际上,有很多MFC程序员在使用MFC向导来做某些工作,如编写ActiveX控件,在此时不用向导是很不明智的。经过深思熟虑之后,我决定加上这些内容。   尽管这些新内容是关于向导的使用,然而它现在不是,以后也不会是一本仅仅介绍如何在向导中单击按钮的书。在介绍了一个像如何用ClassWizard编写消息句柄之类的基本技巧之后,我在给出代码时,将不再赘述这些内容。当然,。向导永远不能超越您的能力去做任何事,因此键人所有手写的代码是完全可行的。   在讲述MFC编程的书中使用向导的缺点是,向导生成的程序不适合发表。本书的第一版印刷了每个程序文件的代码。本版书没有这样做,而只是包括了“相关”的源代码文件,其他的都放在CD-ROM中,为什么?因为印刷这些代码会使本书增加一倍的厚度而没有相应增加内涵。其中一些代码是由Visual C++6.0的MFC向导产生的,甚至没有被编译(详细内容请参见第4章)。我并不为本书中向导生成的代码而得意,因为其中充斥着随意生成的空行、不连贯的注释和无用的函数。对那些以编写清晰易读的示例源代码为骄傲的作者来说,向导生成的东西是一剂苦药。   然而,向导代表了Windows编程的新法则,它们是你、我及所有人都必须习惯的重要事物。但令人遗憾的是,VisualC++开发小组没有给我们一个真正的向导,而只是一个冒充向导的玩具。在他们能够做到以前,我们得习惯现有的向导。   CD.ROM中的内容   本书随附的CD-ROM包括书中全部示例程序的源代码,它们都在Visual C十十6.0和MFC 6.0环境下编写、编译而成,并在Win32平台上测试通过。如果没有特别说明它们都与Windows 98、WindowsNT4.0及Windows 2000兼容,其中大部分也与Windows 95及WindowsNT 3.51兼容。   您可用CD—ROM的根目录下的安装程序来将CD-ROM中,的内容复制到硬盘里,也可只复制其中\Code目录中的代码。我们在此目录中为书中每章各建了一个子目录——Chap01、Chap02,依次类推。在这些子目录中存放示例程序。每个程序的源代码文件都有一个相应的发布版本的EXE文件,以及您可使用Visual C++的Open Workspace命令来打开的workspace(DSW)文件。   JeffProsise   1999年3月12日...   
Introduction Like many of my colleagues in this industry, I learned Windows programming from Charles Petzold's Programming Windows—a classic programming text that is the bible to an entire generation of Windows programmers. When I set out to become an MFC programmer in 1994, I went shopping for an MFC equivalent to Programming Windows. After searching in vain for such a book and spending a year learning MFC the old-fashioned way, I decided to write one myself. It's the book you hold in your hands. And it's the book I would like to have had when I was learning to program Windows the MFC way. MFC, as you probably already know, is Microsoft's C++ class library for Windows programming. Programming Windows with MFC isn't a book about C++; rather, it's a book about writing 32-bit Windows applications in C++ using MFC rather than the Windows API as the chief means of accessing the operating system's essential features and services. It was written with two kinds of people in mind: Windows API programmers who want to learn MFC Programmers who have never before programmed Windows Whichever camp you fall into, I assume that you know the C++ programming language already and are comfortable with basic C++ idioms such as derived classes and virtual functions. If these assumptions are true, you're ready to begin climbing the hill that is MFC programming. Even veteran Windows programmers frequently find MFC code confusing the first time they see it, in part because of the presence of code created by the MFC code-generating wizards in Visual C++ and in part because of the countless lines of code hidden away in MFC classes such as CFrameWnd, CDocument, and CView. That's why this book takes a rather unusual approach to teaching MFC. It begins by having you write MFC code by hand (without the wizards) and by utilizing MFC 1.0-style application architectures—that is, applications that use neither documents nor views. Only after you've mastered the fundamentals and become acquainted with basic MFC classes such as CWnd and CWinApp do I introduce the wizards and teach you how to take advantage of MFC's document/view architecture. Along the way, you build a understanding from the ground up of the message-oriented nature of Windows and of key components of Windows itself, such as the Graphics Device Interface (GDI). I believe that this approach makes learning MFC not only less intimidating, but also more enjoyable. I think that you'll agree once you've worked your way through the book and can look back on the learning experience from the standpoint of a knowledgeable Windows programmer. Programming Windows with MFC is divided into four parts. Part I introduces the core tenets of MFC and Windows programming, beginning with a simple "Hello, MFC" application and introducing, one by one, menus, controls, dialog boxes, and other application building blocks. Part II builds on the foundation laid in Part I with a detailed look at the document/view architecture. In particular, Chapters 9, 10, and 11 reveal much of the "magic" behind documents and views and explain not only how to write basic document/view applications but also how to implement some not so basic features such as split-window views of a document and print previews. Part III covers some of the more advanced features of Windows and MFC—features such as color palettes, bitmap handling, and multiple threads of execution. In Part IV, you'll learn how MFC wraps its arms around COM, OLE, and ActiveX and how to write COM-enabled applications and software components. By the time you're finished with Chapter 21, you'll be well versed in the art of 32-bit Windows programming using MFC. And you'll have prodigious amounts of sample code to draw from when it's time to strike out on your own and write your first great Windows application. What's New in the Second Edition Those of you who read the first edition of this book will notice two rather obvious changes in the second edition. First, this edition contains seven new chapters. One is devoted to the MFC view classes; another covers the MFC collection classes; one introduces MFC file I/O and serialization mechanisms; and four cover the relationship between MFC and COM. MFC is not the general-purpose COM framework that the Active Template Library (ATL) is, but MFC makes certain types of COM programming exceptionally easy. For example, MFC greatly simplifies the task of writing ActiveX controls, and it makes writing Automation servers—programs that use COM to expose their functionality to scripting clients—a breeze. The second major change in this edition has to do with wizards. The first edition didn't cover the MFC wizards at all. The second edition uses hand-generated code in Chapters 1 through 3 but then shifts gears and begins using AppWizard and ClassWizard in Chapter 4. Why the change of heart? I still believe that code-generating wizards are an impediment to learning and should be used only by knowledgeable programmers, but I've also come to realize that in the real world, MFC programmers use the wizards. For certain tasks—writing ActiveX controls, for example—it doesn't make sense not to use the wizards. So after much deliberation, I decided I would be remiss not to cover them. Despite the new material regarding wizards, however, this is not—and never will be—a book about clicking buttons in AppWizard. After introducing a fundamental skill, such as how to write a message handler with ClassWizard, I thereafter let the source code do the talking and assume that you can figure out how the source code was created. Keep in mind that the wizards never do anything you can't do yourself, so it's perfectly feasible to type in every source code listing by hand if you'd like to. The downside to using wizards in a book that teaches MFC programming is that they produce code that isn't fit to publish. The first edition of this book included printed listings for each and every source code file. This one does not. It contains printed copies of relevant source code files and provides the others on CD. Why? Because printing a source code file that's 50 percent meat and 50 percent fat adds bulk to a book without adding content. Some of the code produced by the MFC AppWizard in Visual C++ 6.0 won't even compile. (For details, see Chapter 4.) I'm not very proud of the parts of my book that the wizards created, because those portions are littered with arbitrary blank lines, comments that lack consistent style, and unnecessary functions. For someone who takes pride in writing concise, readable sample code, wizard output is a bitter pill to swallow. Nevertheless, wizards represent the new world order in Windows programming, and they're something that you, I, and everyone else must get used to. It's a shame that the Visual C++ team won't give us real wizards to play with instead of the toys that they pass off as wizards today. Until they do, we must make do with what we have. What's On the CD The CD that accompanies this book contains source code and executables for all the sample programs presented in the book. All samples were written and compiled with Visual C++ 6.0 and MFC 6.0 and tested on various Win32 platforms. Unless otherwise noted, all are compatible with Windows 98, Windows NT 4.0, and Windows 2000. Most are also compatible with Windows 95 and Windows NT 3.51. You can copy the contents of the CD to your hard disk by running the setup program found in the CD's root directory, or you can retrieve the files directly from the CD's \Code directory. The \Code directory contains one subdirectory for each chapter of the book—Chap01, Chap02, and so on. Inside these subdirectories you'll find the sample programs. Each set of source code files is accompanied by a release-build EXE as well as a Visual C++ workspace (DSW) file that you can open with Visual C++'s Open Workspace command. From Me to You (and You to Me) From the day in 1995 when I began writing the first edition of Programming Windows with MFC, my goal has been to provide C++ programmers with the same kind of timeless, irreplaceable resource that Programming Windows is to C programmers. Whether I've achieved that goal, I'll let you be the judge. I want to know what you think about Programming Windows with MFC, and I particularly want to hear from you if you find mistakes. You can reach me by sending mail to jeffpro@msn.com or by visiting my Web site at www.prosise.com. At that site you'll find up-to-date information regarding the book, a list of errata, and information about other projects that I'm working on. Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I know that you could have chosen any number of MFC books besides this one. I thank you for purchasing Programming Windows with MFC, and I sincerely hope you conclude that your money was well spent. Enjoy! Jeff Prosise
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃不起饭的小陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值