MFC Application

初学MFC Application 这是MFC初学应用程式,可以让初学者熟悉MFC的架构。 立即下载

Your First MFC Application

It's time to build your first MFC application. And what better place to start than with one that displays "Hello, MFC" in a window? Based on the classic "Hello, world" program immortalized in Brian Kernighan and Dennis Ritchie's The C Programming Language (1988, Prentice-Hall), this very minimal program, which I'll call Hello, demonstrates the fundamental principles involved in using MFC to write a Windows application. Among other things, you'll get a close-up look at MFC's CWinApp and CFrameWnd classes and see firsthand how classes are derived from them and plugged into the application. You'll also learn about the all-important CPaintDC class, which serves as the conduit through which text and graphics are drawn in a window in response to WM_PAINT messages. Finally, you'll be introduced to message mapping, the mechanism MFC uses to correlate the messages your application receives with the member functions you provide to handle those messages.

Figure 1-3 lists the source code for Hello. Hello.h contains the declarations for two derived classes. Hello.cpp contains the implementations of those classes. Among C++ programmers, it's traditional to put class definitions in .h files and source code in .cpp files. We'll honor that tradition here and throughout the rest of this book. For large applications containing tens or perhaps hundreds of classes, it's also beneficial to put class declarations and implementations in separate source code files. That's overkill for the programs in the first few chapters of this book, but later on, when we begin working with documents and views, we'll give each class its own .h and .cpp files. On the CD in the back of the book, in the folder named Chap01, you'll find a folder with copies of Hello.h and Hello.cpp as well as a folder containing a copy of the compiled executable (Hello.exe).

Figure 1-3. The Hello application.

Hello.h

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

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

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

Hello.cpp

#include <afxwin.h>
#include "Hello.h"

CMyApp myApp;

/////////////////////////////////////////////////////////////////////////
// CMyApp member functions

BOOL CMyApp::InitInstance ()
{
    m_pMainWnd = new CMainWindow;
	

   m_pMainWnd->ShowWindow (m_nCmdShow);
    m_pMainWnd->UpdateWindow ();
    return TRUE;
}

/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions

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);
}

Figure 1-4 shows the output from Hello. When you run the application, notice that the window is entirely functional; you can move it, resize it, minimize it, maximize it, and close it. And when the window is resized, "Hello, MFC" is redrawn in the center of the window.

Most of Hello's functionality comes from Windows. Windows, for example, draws the exterior, or nonclient area, of the window: the title bar, the buttons on the title bar, and the window's border. It's your responsibility to create the window and process WM_PAINT messages indicating that all or part of the window's interior, or client area, needs updating. Let's examine the source code to see how Hello works.

Click to view at full size.

Figure 1-4. The Hello window.

The Application Object

The heart of an MFC application is an application object based on the CWinApp class. CWinApp provides the message loop that retrieves messages and dispatches them to the application's window. It also includes key virtual functions that can be overridden to customize the application's behavior. CWinApp and other MFC classes are brought into your application when you include the header file Afxwin.h. An MFC application can have one—and only one—application object, and that object must be declared with global scope so that it will be instantiated in memory at the very outset of the program.

Hello's application class is named CMyApp. It is instantiated in Hello.cpp with the statement

CMyApp myApp;

CMyApp's class declaration appears in Hello.h:

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

CMyApp declares no data members and overrides just one function inherited from CWinApp. InitInstance is called early in the application's lifetime, right after the application starts running but before the window is created. In fact, unless InitInstance creates a window, the application doesn't have a window. That's why even a minimal MFC application must derive a class from CWinApp and override CWinApp::InitInstance.

The InitInstance Function

CWinApp::InitInstance is a virtual function whose default implementation contains just one statement:

return TRUE;

The purpose of InitInstance is to provide the application with the opportunity to initialize itself. The value returned by InitInstance determines what the framework does next. Returning FALSE from InitInstance shuts down the application. If initialization goes as planned, InitInstance should return TRUE in order to allow the program to proceed. InitInstance is the perfect place to perform initializations that need to be done each time the program starts. At the very least, this means creating the window that will represent the application on the screen.

CMyApp's implementation of InitInstance, which appears in Hello.cpp, creates the Hello window by instantiating Hello's CMainWindow class. The statement

m_pMainWnd = new CMainWindow;

constructs a CMainWindow object and copies its address to the application object's m_pMainWnd data member. After the window is created, InitInstance displays it—remember, a window is not initially visible unless it is created with a WS_VISIBLE attribute—by calling ShowWindow and UpdateWindow through the CMainWindow pointer:

m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();

ShowWindow and UpdateWindow are CWnd member functions common to all window objects, including objects of the CFrameWnd class from which CMainWindow is derived. These functions are little more than wrappers around the API functions of the same name. To call a regular Windows API function from an MFC program, make it a practice to preface the function name with the global scope resolution operator ::, as in

::UpdateWindow (hwnd);

This notation will ensure that the API function is called even if the object that makes the call has a member function with the same name. In the remainder of this book, Windows API functions will be prefaced with :: to distinguish them from MFC member functions.

ShowWindow accepts just one parameter: an integer that specifies whether the window should initially be shown minimized, maximized, or neither minimized nor maximized. In accordance with Windows programming protocol, Hello passes ShowWindow the value stored in the application object's m_nCmdShow variable, which holds the nCmdShow parameter passed to WinMain. The m_nCmdShow value is usually SW_SHOWNORMAL, indicating that the window should be displayed in its normal unmaximized and unminimized state. However, depending on how the user starts an application, Windows will occasionally slip in a value such as SW_SHOWMAXIMIZED or SW_SHOWMINIMIZED. Unless there is a specific reason for it to do otherwise, InitInstance should always pass the m_nCmdShow variable instead of a hardcoded SW_ value to ShowWindow.

UpdateWindow completes the job that ShowWindow started by forcing an immediate repaint. Its work done, InitInstance returns TRUE to allow the application to proceed.

Other CWinApp Overridables

InitInstance is just one of several virtual CWinApp member functions you can override to customize the behavior of the application object. Look up the CWinApp overridables in your MFC documentation and you'll see a list of more than a dozen others with names such as WinHelp and ProcessWndProcException. Many of these functions are seldom overridden, but they're handy to have around nonetheless. For example, you can use ExitInstance to clean up when an application terminates. If you use InitInstance to allocate memory or other resources, ExitInstance is the perfect place to free those resources. The default implementation of ExitInstance performs some routine cleanup chores required by the framework, so you should be sure to call the base class version if you've overridden ExitInstance. Ultimately, the value returned by ExitInstance is the exit code returned by WinMain.

Other interesting CWinApp overridables include OnIdle, Run, and PreTranslateMessage. OnIdle is handy for performing background processing chores such as garbage collection. Because OnIdle is called when an application is "idle"—that is, when there are no messages waiting to be processed—it provides an excellent mechanism for performing low-priority background tasks without spawning a separate thread of execution. OnIdle is discussed at length in Chapter 14. You can override Run to customize the message loop, replacing it with a message loop of your own. If you just want to perform some specialized preprocessing on certain messages before they are dispatched, you can override PreTranslateMessage and save yourself the trouble of writing a whole new message loop.

How MFC Uses the Application Object

To someone who has never seen an MFC application's source code, one of the more remarkable aspects of Hello will be that it contains no executable code outside of the classes it defines. It has no main or WinMain function, for example; the only statement in the entire program that has global scope is the statement that instantiates the application object. So what actually starts the program running, and when does the application object come into the picture?

The best way to understand what goes on under the hood is to look at the framework's source code. One of the source code files provided with MFC—Winmain.cpp—contains an AfxWinMain function that is MFC's equivalent of WinMain. (That's right: when you purchase Visual C++, you get the source code for MFC, too.) AfxWinMain makes extensive use of the application object, which is why the application object must be declared globally. Global variables and objects are created before any code is executed, and the application object must be extant in memory before AfxWinMain starts.

Right after starting, AfxWinMain calls a function named AfxWinInit to initialize the framework and copy hInstance, nCmdShow, and other AfxWinMain function parameters to data members of the application object. Then it calls InitApplication and InitInstance. In 16-bit versions of MFC, InitApplication is called only if the hPrevInstance parameter passed to AfxWinMain is NULL, indicating that this is the only instance of the application currently running. In the Win32 environment, hPrevInstance is always NULL, so the framework doesn't bother to check it. A 32-bit application could just as easily use InitApplication to initialize itself as InitInstance, but InitApplication is provided for compatibility with previous versions of MFC and should not be used in 32-bit Windows applications. If AfxWinInit, InitApplication, or InitInstance returns 0, AfxWinMain terminates instead of proceeding further and the application is shut down.

Only if all of the aforementioned functions return nonzero values does AfxWinMain perform the next critical step. The statement

pThread->Run();

calls the application object's Run function, which executes the message loop and begins pumping messages to the application's window. The message loop repeats until a WM_QUIT message is retrieved from the message queue, at which point Run breaks out of the loop, calls ExitInstance, and returns to AfxWinMain. After doing some last-minute cleaning up, AfxWinMain executes a return to end the application.

MFCApplication1.rar 基于VS2017+MFC对话框设计子对话框的简单操作,实现通过按钮打开三个子对话框,后续可以进行其他处理行为。 立即下载

相关推荐

MFCApplication1.zip

这是一个用VS2015建立测试用例,并对MFC工程中的类进行单元测试的示例工程,内含一个工程,两个测试工程。

mfc修改只读文件_VS2019/MFC编程入门之(如何利用MFC向导生成单文档应用程序框架)...

在VS的使用介绍中已经讲了解决方案与工程的概念,这里再重提一下。每个应用程序都作为一个工程来处理,它包含了头文件、源文件和资源文件等,这些文件通过工程集中管理。在VS中,工程都是在解决方案管理之下的。一个解决方案可以管理多个工程,可以把解决方案理解为多个有关系或者没有关系的工程的集合。VS2010提供了一个Solution Explorer解决方案浏览器视图,可以显示当前解决方案的内容,当新建一个...

weixin_36108913的博客 429

MFCApplication3

MFCApplication3

利用MFC向导生成单文档应用程序框架

HelloWorld 点击文件(File)->新建(New)->项目(Project)。弹出新建项目(New Project)

qq_36314864的博客 760

MFCApplication2.rar

MFC串口接收图片数据 通过picture control控件来显示图片 MFC串口接收图片数据 通过picture control控件来显示图片 MFC串口接收图片数据 通过picture control控件来显示图片 代码已经验证过 串口接收显示图片正常

MFCApplication1.0.zip

可以发送和接收信号

简述MFC程序生与死

    经常从网上下载好程序,但打开VC看了不到几行,鼠标就移到最到最右上角,对准那个“X”,咔嚓...(心还暗念:嚓死你!),二话不说在“程序”菜单里寻找QQ的存在,开始进入休闲时光!!这可是我经常做的事情,唉!苦于基础不扎实,经常被美好的代码踢出门外。但幸好我还对她有一斯感觉,近来有幸买到侯先生的《深入浅出MFC》,看到第六章:MFC程序的生死因果,觉得是学MFC,喔不,应该是看MFC程序代码

pcvc的专栏 2048

5分钟搞定:用Pyannote和Whisper实现带说话人分离的语音转文字(附完整代码)

本文详细介绍了如何快速构建一个集成了说话人分离功能的语音转文字系统。通过结合OpenAI的Whisper模型进行高精度语音识别和Pyannote模型实现说话人分离,并提供了完整的代码实现、环境搭建指南以及核心的对齐与合并逻辑,帮助开发者高效处理会议录音、访谈等多说话人音频,实现自动化转录。

threejs5artist的博客 640

帮你快速理解什么是MFC(Windows环境下)

帮你快速理解什么是MFC(Windows环境下)

荔园微风的博客 1万+

HFSS15 win10_64位破解

HFSS15 win10_64位破解 找到的别人的软件,但在首次安装时失败了,并不能patched。寻找了几种方法,已成功安装。在此写下安装经验,供大家参考。 软件安装包以及破解软件: 链接:https://pan.baidu.com/s/1MMjV_31maU5QD6fmI4zfIA 提取码:yj4q 在安装之前,建议关闭杀毒软件。 Step1:进入HFSS150Win64安装包,点击au...

玉笙寒 4万+

最新版电子发票样式html+css--普通发票+增值发票

【代码】最新版电子发票样式html+css--普通发票+增值发票。

weixin_44110923的博客 3718

如何在win32 Application 中使用MFC

在Virtual C++ 6.0创建的Win32 Application 和 Win32 Console Application 中使用 MFC 是可能的,主要的困难在于绕过MFC提供的WinMain函数。下面我提供一个方法以供参考: 进入 Project-->Setting--> C/C++ Page,做以下修改: 1.    在Preprocessor definitions中加入

梦想起飞的地方 1127

GraphSense介绍

GraphSense是一个跨账本加密货币分析平台 它的介绍在这里 代码也是开源的 graphsense-dashboard 用于交互式加密货币分析的Web仪表板。 可以看导进数据的情况 graphsense-transformation 基于spark/cassandra/java服务运行 GraphSense转换管道,用于将加密货币的区块/交易/标签等数据导入,可以看它指定的json格式: t...

幸福诗歌的博客 1086

在 Win32 Application 和 Win32 Console Application 中使用 MFC

 在 Win32 Application 和 Win32 Console Application 中使用 MFC  在Virtual C++ 6.0创建的Win32 Application 和 Win32 Console Application 中使用 MFC 是可能的,主要的困难在于绕过MFC提供的WinMain函数。下面我提供一个方法以供参考:  进入 Project-->Setting-->

1158

Linux单独编译设备树,开发者分享 | Petalinux 工程中设备树的介绍

本文来自XILINX嵌入式产品应用工程师 Terry Ni设备树是 Petalinux kernel 的关键组件,接下来以 2020.1 版本为例,为大家介绍一下在Xilinx Petalinux 工程中的设备树是如何产生,配置以及修改的。Petalinux 工程中设备树的位置当我们创建了一个 petalinux 工程后(以zcu102为例),在编译的时候,会自动生成对应的设备树文件。这些设备树的...

weixin_28877107的博客 2638

基于STM32的智慧农业大棚监测与控制系统设计与实现

摘要:本文设计了一种基于STM32F103C8T6单片机的智慧农业大棚监测与控制系统,集成多参数环境监测、智能控制和远程管理功能。系统可实时采集光照、CO2、温湿度和土壤湿度等参数,通过OLED显示并采用双模式控制机制:自动模式下根据阈值调节设备,手动模式下支持APP或按键控制。通过ESP8266模块接入机智云平台实现数据云存储和远程控制。测试表明系统具有高精度(温度±0.5℃、CO2±50ppm)、快速响应(<2s)和稳定运行(30天无故障)等特点,有效解决了传统大棚调控不及时等问题。创新点包括多参

3254968421的博客 491

VC: Win32 Application和Win32 Console Application、MFC APPWIZARD区别

都是工作在32位Windows环境的程序。 Win32 Application :是普通的常见的窗口应用程序,当然有的界面做得比较个性化,比如 圆形的、不规则形状的…… 它们都是所谓的GUI(Graphics User Interface图形用户接口),我们 可以通过鼠标点击来完成控制。 Win32 Application:是为你开发windows应用程序所准备的,程序 以WinMain()为入口,#include,能够使用win32 API函数。 而Win32 Console

fengyunjh的专栏 1939

38.XDMA寄存器详解2-H2C、C2H通道寄存器组剖析

目录 1.上节回顾 2. H2C寄存器剖析 2.1 H2C通道标识寄存器(0x00) 2.2 H2C通道控制寄存器(0x04) 2.3 H2C通道状态寄存器(0x40) 2.4 H2C通道完成描述符数量寄存器(0x48) 2.5 H2C通道对齐寄存器(0x4C) 2.6 H2C通道查询模式回写地址寄存器(0x88、0x8C) 2.7 H2C通道中断掩码寄存器(0x90)​ 2.8 H2C通道性能监视控制寄存器(0xC0) 2.9 H2C通道周期计数性能寄存器(0xC4、0xC8) 2

一个早起的程序员 5218

Win32 Application 、Win32 Console Application、MFC工程项目

Win32 Application和Win32 Console Application 都是工作在32位Windows环境的程序。 Win32  Application 是标准 windows程序,完全拥有windows的特性,也即我们常说的窗口应用程序!它通过GUI(Graphics User Interface图形用户接口)可以把窗口界面做得比较个性化,比如圆形的、不规则形状的等等,另外,我

anhuidelinger的专栏 5734

2、集中式MIMO雷达研究进展:正交波形设计与信号处理

集中式多输入多输出(Multiple Input Multiple Output,MIMO)雷达通常利用正交波形增加发射波形 自由度,采用数字阵列拓展空间收发自由度,使得雷达接收机的天线孔径获得明显扩展,最终带来空间分辨率、 测角精确度、杂波抑制能力等大幅度提升。但是,这些性能提升的前提是发射波形具有正交特性。事实上,在 实际应用中,在不牺牲时域/频域资源情况下,受限于时宽带宽积,无法获得完全正交的波形集合,从而限制了 MIMO雷达系统性能。本文对集中式MIMO雷达正交波形复用的技术原理进行了系统回顾,分别

qq_43416206的博客 2778

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Win32 Application和Win32 Console Application,MFC Appliwizerd区别

Win32 Application 就是普通的常见的窗口应用程序,当然有的界面做得比较个性化,比如圆形的、不规则形状的…… 它们都是所谓的GUI(Graphics User Interface图形用户接口),我们可以通过鼠标点击来完成控制。 Win32 Application是为你开发windows应用程序所准备的,程序以WinMain()为入口,#include,能够使用win32 API函数。

tluyk的博客 467

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Win32 Console Application、Win32 Application、MFC三者之间的联系和区别

本文主要介绍了MFC、Win32(SDK程序,控制台程序)的区别和联系。

Keep Moving~ 9897

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

上一篇: Introducing MFC
下一篇: MFC Application---The Frame Window Object
lemonade
博客等级 码龄26年 8粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值