建立一个Win32空项目后,然后手工打造的最简单的MFC程序:
1: //stdafx.h
2: #pragma once3: #include <sdkddkver.h>4: #include <afxwin.h>5: #include <afxwinappex.h>6: #include <afxframewndex.h>
1: //stdafx.cpp
2: #include "stdafx.h"
1: //MainApp.h
2: #pragma once3: class CMainApp : public CWinAppEx4: {5: public:
6: virtual BOOL InitInstance();
7: };
1: //MainApp.cpp
2: #include "stdafx.h"
3: #include "MainApp.h"
4: #include "MainWindow.h"
5:6: CMainApp mianApp;7:8: BOOL CMainApp::InitInstance()9: {10: CWinAppEx::InitInstance();11:12: SetRegistryKey(TEXT("Hello MFC app."));
13:14: m_pMainWnd = new CMainWindow();
15: m_pMainWnd->ShowWindow(SW_SHOW);16: m_pMainWnd->UpdateWindow();17: return TRUE;
18: }
1: //MainWindow.h
2: #pragma once3: class CMainWindow : public CFrameWndEx4: {5: public:
6: CMainWindow(void);
7:8: DECLARE_MESSAGE_MAP()9:10: afx_msg void OnPaint();
11: afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
12: };
1: //MainWindow.cpp
2: #include "stdafx.h"
3: #include "MainWindow.h"
4:5: CMainWindow::CMainWindow(void)
6: {7: Create(NULL,TEXT("Main window"));
8: }9:10: BEGIN_MESSAGE_MAP(CMainWindow, CFrameWndEx)11: ON_WM_PAINT()12: ON_WM_LBUTTONDOWN()13: END_MESSAGE_MAP()14:15: void CMainWindow::OnPaint()
16: {17: CPaintDC dc(this); // device context for painting18: // TODO: Add your message handler code here
19: CRect rc;20: GetClientRect(&rc);21: dc.DrawText(TEXT("It is a simple MFC application!"),&rc,DT_VCENTER|DT_SINGLELINE|DT_CENTER);
22: }23:24: void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
25: {26: // TODO: Add your message handler code here and/or call default
27: AfxMessageBox(TEXT("Left mouse button clicked!"));
28: CFrameWndEx::OnLButtonDown(nFlags, point);29: }