MFC窗口程序基础

一 、一个简单的MFC程序

1.新建一个项目
在这里插入图片描述
在这里插入图片描述
2.对象类的关系
CObject类是MFC绝大多数类的基类。完成动态空间的分配与回收。
CWinApp类是应用程序的主线程类。
CDialog 类用来控制对话框窗口。
一个MFC程序中只有一个从WinAPP类派生来的类,也只有一个从应用程序实例化的对象,即应用程序本身。
3.MFC简单窗口的实现

# include <windows.h>

# include <stdio.h>

LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	WNDCLASS wndcls;
	wndcls.cbClsExtra=0;
	wndcls.cbWndExtra=0;
	wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
	wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndcls.hInstance=hInstance;
	wndcls.lpfnWndProc=WinSunProc;
	wndcls.lpszClassName="Weixin2003";
	wndcls.lpszMenuName=NULL;
	wndcls.style=CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndcls);

HWND hwnd;
hwnd=CreateWindow("Weixin2003","窗口",WS_OVERLAPPEDWINDOW,
	0,0,600,400,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}
return 0;

}

LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	switch(uMsg)
	{
	case WM_CHAR:
		char szChar[20];
		sprintf(szChar,"char is %d",wParam);
		MessageBox(hwnd,szChar,"weixin",0);
		break;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd,"点击了一次鼠标","提示",0);
		HDC hdc;
		hdc=GetDC(hwnd);
		TextOut(hdc,0,0,"第一次作业",strlen("第一次作业"));
		ReleaseDC(hwnd,hdc);
		break;
	case WM_PAINT:
		HDC hDC;
		PAINTSTRUCT ps;
		hDC=BeginPaint(hwnd,&ps);
		TextOut(hDC,0,0,"窗口创建",strlen("窗口创建"));
		EndPaint(hwnd,&ps);
		break;
	case WM_CLOSE:
		if(IDYES==MessageBox(hwnd,"是否结束?","weixin",MB_YESNO))
		{
			DestroyWindow(hwnd);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
	return 0;
}

点击运行
在这里插入图片描述

二、用命令行工具编译链接 Windows API 程序

1 环境配置
右击此电脑–>属性–>系统高级设置,点击环境变量
在这里插入图片描述

2.2在用户变量中点击path,点击编辑
在这里插入图片描述

2.3选择新建,在电脑中找到cl.exe文件,复制文件路径。
在这里插入图片描述

2.4确定后在用户变量界面点击新建输入变量名:LIB,变量值输入复制的路径
确认后在此点击新建输入变量名:INCLUDE,变量值输入复制的路径
2.5环境变量配置完成。
在这里插入图片描述
2. 编译链接 Windows API 程序
(1) 找到 WindowsProject.c 所在目录,然后在上面地址栏输入 cmd 进入命令行窗口
(2)编译 WindowsProject.c 程序:

cl /c /D "UNICODE" /EHsc WindowsProject.c

(3)链接 WindowsProject.c 程序:点击 “项目 —> 属性 —> 链接器 —> 输入”:
(4)链接完后将会生成一个 .exe 文件,点击即可运行,效果如下(
在这里插入图片描述

三、用命令行工具编译链接 MFC 程序

1.环境变量的配置:
添加道 LIB 中:

D:\Vs2019\VC\Tools\atlmfc\include

添加道 LIB 中:

D:\Vs2019\VC\Tools\atlmfc\lib\x64

编译 mfc.cpp 文件

cl /c /D "UNICODE" /EHsc mfc.cpp
  1. 链接 mfc.obj 文件
link mfc.obj

4.mfc.cpp文件加上

extern int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	_In_ LPTSTR lpCmdLine, int nCmdShow);

extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	_In_ LPTSTR lpCmdLine, int nCmdShow)
#pragma warning(suppress: 4985)
{
	// call shared/exported WinMain
	return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	_In_ LPTSTR lpCmdLine, int nCmdShow)
{
	ASSERT(hPrevInstance == NULL);

	int nReturnCode = -1;
	CWinThread* pThread = AfxGetThread();
	CWinApp* pApp = AfxGetApp();

	// AFX internal initialization
	if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
		goto InitFailure;

	// App global initializations (rare)
	if (pApp != NULL && !pApp->InitApplication())
		goto InitFailure;

	// Perform specific initializations
	if (!pThread->InitInstance())
	{
		if (pThread->m_pMainWnd != NULL)
		{
			TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd\n");
			pThread->m_pMainWnd->DestroyWindow();
		}
		nReturnCode = pThread->ExitInstance();
		goto InitFailure;
	}
	nReturnCode = pThread->Run();

InitFailure:
#ifdef _DEBUG
	// Check for missing AfxLockTempMap calls
	if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
	{
		TRACE(traceAppMsg, 0, "Warning: Temp map lock count non-zero (%ld).\n",
			AfxGetModuleThreadState()->m_nTempMapLock);
	}
	AfxLockTempMaps();
	AfxUnlockTempMaps(-1);
#endif

	AfxWinTerm();
	return nReturnCode;
}

  1. 重新链接
link mfc.obj

点击 mfc.exe,效果如下
在这里插入图片描述

总结

在这个过程中最烦的就是头文件找不到了,需要去配置环境变量才可,实现一个 MFC 程序主要的两个类:CFrameWnd(窗口框架类) 和 CWinApp(应用程序类)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值