微软 MFC 官方文档:https://docs.microsoft.com/zh-cn/cpp/mfc/mfc-desktop-applications?view=vs-2019
MFC 层次结构图以及下载地址:https://docs.microsoft.com/zh-cn/cpp/mfc/hierarchy-chart?view=vs-2019
VC6.0/VS2005/VS2010/VS2012/VS2013/VS2015的MFC类库继承图:
https://download.csdn.net/download/freeking101/11803983
微软 MSDN 帮助文档 :https://msdn.microsoft.com/library
MFC Widnows程序设计 第二版(带源码):https://pan.baidu.com/s/1SL6KEL0rv0KxktYEZnDATw 提取码:2c43
MFC 层次结构图
- 1. 下图表示 派生自 MFC 类 CObject :
- 2. 下图表示 派生自 MFC 类 CWnd 和 CCmdTarget :
- 3. 下图表示 不是 派生自 MFC 类CObject:
1.1 Windows 编程模型
SDK应用程序 与 MFC应用程序 运行过程的 对比:
http://www.jizhuomi.com/software/145.html
实例代码:
#include <windows.h>
LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{WNDCLASS wc;HWND hwnd;MSG msg;wc.style = 0; //类样式wc.lpfnWndProc = (WNDPROC)WndProc; //window 程序地址wc.cbClsExtra = 0; //类 额外的字节wc.cbWndExtra = 0; //window 额外的字节wc.hInstance = hInstance; //实例句柄 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); //图标句柄wc.hCursor = LoadIcon(NULL, IDC_ARROW); //鼠标句柄wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // 背景颜色wc.lpszMenuName = NULL; //菜单名wc.lpszClassName = "MyWndClass"; //WNDCLASS 名RegisterClass(&wc);hwnd = CreateWindow("MyWndClass", //WNDCLASS 名"SDK_Application", //window titleWS_OVERLAPPEDWINDOW, //window styleCW_USEDEFAULT, //水平位置CW_USEDEFAULT, //垂直位置CW_USEDEFAULT, //初始化宽度CW_USEDEFAULT, //初始化高度HWND_DESKTOP, //父窗口句柄NULL, //菜单句柄hInstance, //应用程序的 实例 句柄NULL //window 创建数据);ShowWindow(hwnd, nShowCmd);UpdateWindow(hwnd);while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{PAINTSTRUCT ps;HDC hdc;switch (message){case WM_PAINT:hdc = BeginPaint(hwnd, &ps);Ellipse(hdc, 0, 0, 200, 100);EndPaint(hwnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd, message, wParam, lParam);
}
运行截图:
程序解释:
示例代码 2:
#include <windows.h> LRESULT CALLBACK myWndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{const static TCHAR appName[] = TEXT("Hello world");WNDCLASSEX myWin;myWin.cbSize = sizeof(myWin);myWin.style = CS_HREDRAW | CS_VREDRAW;myWin.lpfnWndProc = myWndProc;myWin.cbClsExtra = 0;myWin.cbWndExtra = 0;myWin.hInstance = hInstance;myWin.hIcon = 0;myWin.hIconSm = 0;myWin.hCursor = 0;myWin.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);myWin.lpszMenuName = 0;myWin.lpszClassName = appName;//Register if (!RegisterClassEx(&myWin)) return 0;const HWND hWindow = CreateWindow(appName,appName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance,0);ShowWindow(hWindow, iCmdShow);UpdateWindow(hWindow);{MSG msg;while (GetMessage(&msg, 0, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return (int)msg.wParam;}
}LRESULT CALLBACK myWndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{if (msg == WM_PAINT){PAINTSTRUCT ps;const HDC hDC = BeginPaint(hWindow, &ps);RECT rect;GetClientRect(hWindow, &rect);DrawText(hDC, TEXT("HELLO WORLD"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);EndPaint(hWindow, &ps);return 0;}else if (msg == WM_DESTROY){PostQuitMessage(0);return 0;}return DefWindowProc(hWindow, msg, wParam, lParam);
}
1.3 第一个 MFC 程序
源代码:
hello.h
class CMyApp : public CWinApp
{
public:virtual BOOL InitInstance ();
};class CMainWindow : public CFrameWnd
{
public:CMainWindow ();protected:afx_msg void OnPaint ();DECLARE_MESSAGE_MAP ()
};
MFC 中 afx_msg 是什么,afx_msg void function() 是什么意思:https://www.cnblogs.com/linkzijun/p/6196165.html
hello.cpp
#include <afxwin.h>
#include "Hello.h"CMyApp myApp;/
// CMyApp member functionsBOOL CMyApp::InitInstance ()
{m_pMainWnd = new CMainWindow;m_pMainWnd->ShowWindow (m_nCmdShow);m_pMainWnd->UpdateWindow ();return TRUE;
}/
// CMainWindow message map and member functionsBEGIN_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);
}
运行结果截图:
1.3.1 应用程序对象
1.3.2 MFC 如何使用应用程序对象
1.3.3 框架窗口对象
1.3.4 绘制窗口
1.3.5 消息映射
1.3.6 消息映射的工作方式
1.3.7 Windows、字符集 和 _T 宏