3. MFC原理介绍

包括C++封装原理和MFC六大关键技术,以及类向导和MFC应用程序向导的使用方法。讲解MFC消息映射机制的原理、消息映射函数的建立,以及消息发送和接收的方法。

CTime类,C语言中time函数

void CTmDlg::OnButton1() 
{
    CTime t = CTime::GetCurrentTime();
    int nYear = t.GetYear();
    int nMonth = t.GetMonth();
    int nDay = t.GetDay();
    int nHour = t.GetHour();
    int nMin = t.GetMinute();
    int nSec = t.GetSecond();
    int nWeek = t.GetDayOfWeek();//周三
    CString str;
    str.Format("当前时间是:%d年%02d月%02d日 %02d:%02d:%02d",
        nYear,nMonth,nDay,nHour,nMin,nSec);
    //AfxMessageBox(str);
    SetWindowText(str);//SetTimer
}

void CTmDlg::OnButton2() 
{
    time_t tt = time(NULL);
    tm * pTime = localtime(&tt);
    int nYear = pTime ->tm_year+1900;
    int nMonth = pTime ->tm_mon+1;
    int nDay = pTime ->tm_mday;
    int nHour = pTime ->tm_hour;
    int nMin = pTime ->tm_min;
    int nSec = pTime ->tm_sec;
    CString str;
    str.Format("当前时间是:%d年%02d月%02d日 %02d:%02d:%02d",
        nYear,nMonth,nDay,nHour,nMin,nSec);
    //AfxMessageBox(str);
    SetWindowText(str);//SetTimer

}

// MyTime.h: interface for the CMyTime class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MYTIME_H__A7F50C7B_E6B7_4ED5_BB03_E00EB9935406__INCLUDED_)
#define AFX_MYTIME_H__A7F50C7B_E6B7_4ED5_BB03_E00EB9935406__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//面向对象(C++)特点:抽象,封装和派生,(多态)
#include <time.h>
class CMyTime  
{
	time_t m_time;
public:
	int GetYear() const;
	static CMyTime GetCurrentTime();
	CMyTime();
	~CMyTime();

};

#endif // !defined(AFX_MYTIME_H__A7F50C7B_E6B7_4ED5_BB03_E00EB9935406__INCLUDED_)
// MyTime.h: interface for the CMyTime class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MYTIME_H__A7F50C7B_E6B7_4ED5_BB03_E00EB9935406__INCLUDED_)
#define AFX_MYTIME_H__A7F50C7B_E6B7_4ED5_BB03_E00EB9935406__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//面向对象(C++)特点:抽象,封装和派生,(多态)
#include <time.h>
class CMyTime  
{
    time_t m_time;
public:
    int GetYear() const;
    static CMyTime GetCurrentTime();
    CMyTime();
    ~CMyTime();

};

#endif // !defined(AFX_MYTIME_H__A7F50C7B_E6B7_4ED5_BB03_E00EB9935406__INCLUDED_)
#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include "MyTime.h"
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    CMyTime t = CMyTime::GetCurrentTime();
    int nYear = t.GetYear();
     //CTime time;
/*    time_t tt = time(NULL);
    tm * pTime = localtime(&tt);
    int nYear = pTime ->tm_year+1900;
    int nMonth = pTime ->tm_mon+1;
    int nDay = pTime ->tm_mday;
    int nHour = pTime ->tm_hour;
    int nMin = pTime ->tm_min;
    int nSec = pTime ->tm_sec;
    char s[200];
    sprintf(s,"当前时间是:%d年%02d月%02d日 %02d:%02d:%02d",
        nYear,nMonth,nDay,nHour,nMin,nSec);
    MessageBox(NULL,s,"提示",0);*/
    return 0;
}

MFC所有封装类一共200多个,但是MFC的内部技术不只是简单的封装。
MFC的内部总共有六大关键技术,架构起了整个MFC的开发平台。

一、MFC的六大关键技术包括:
a)MFC程序的初始化过程;
b)消息映射机制;
c)运行时类型识别(RTTI);
d)动态创建;
e)永久保存;
f)消息传递。

六大关键技术的目的是为了提高开发效率,开发者只要在局部做简单地修改,即可处理大部分窗口事物。

二、SendMessage和PostMessage函数的功能:
a)能够向指定的窗口内发送窗口消息,既可以是本进程内窗口也可以是其他进程的;
b)既可以发送系统内部消息,消息编号的范围是:1-WM_USER-1;
例如:WM_LBUTTONDOW,WM_MOUSEMOVE等;
c)也可以发送非系统消息(开发者定义的消息),范围是WM_USER-0x7FFF。
d)在MFC下非系统消息映射,使用宏定义时ON_MESSAGE
三、SendMessage和PostMessage两个函数的区别是:
a)SendMessage是阻塞型函数,PostMessage是非阻塞型函数:
SendMessage用于调用指定窗口的内部程序,直到窗口程序处理完成以后再返回;
PostMessage是将一个消息寄送到一个窗口内的消息队列后就立即返回。
b)两个函数的返回值不同:
LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam);
BOOL PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam);
SendMessage的返回值是消息处理结果产生的数值,依赖于消息处理函数;
PostMessage的返回值是返送成功或失败,一般很少失败除非窗口不存在;
c)跨线程或跨进程发送消息,推荐使用PostMessage函数。

原文地址:https://www.cnblogs.com/wanglinjie/p/10665469.html