第1章 Hello MFC

 

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

运行结果截图:

 

 

1.3.1 应用程序对象

 

1.3.2 MFC 如何使用应用程序对象

 

1.3.3 框架窗口对象

 

1.3.4 绘制窗口

 

1.3.5 消息映射

 

1.3.6 消息映射的工作方式

 

1.3.7 Windows、字符集 和 _T 宏

 

 

 

 

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值