一 、一个简单的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
- 链接 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;
}
- 重新链接
link mfc.obj
点击 mfc.exe,效果如下
总结
在这个过程中最烦的就是头文件找不到了,需要去配置环境变量才可,实现一个 MFC 程序主要的两个类:CFrameWnd(窗口框架类) 和 CWinApp(应用程序类)。