微软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. 下图表示 :
- 2.下图表示 :
- 3.下图表示 :
1.1 Windows编程模型
实例代码:
#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 title WS_OVERLAPPEDWINDOW, //window style CW_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 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);
}
运行结果截图: