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()
 };


//hello.cpp
#include <afxwin.h>
#include "hello.h"

CMyApp myApp;


BOOL CMyApp::InitInstance()
{
 m_pMainWnd=new CMainWindow;
 m_pMainWnd->ShowWindow(m_nCmdShow);
 m_pMainWnd->UpdateWindow();
 return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
 ON_WM_PAINT()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
 Create(NULL,"MFC");
}
void CMainWindow::OnPaint()
{
 CPaintDC dc(this);
 CRect rect;
 GetClientRect(&rect);
 dc.DrawText("Hello MFC",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}


调试程序如下:(vs2008)

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;

//由入口函数传进的参数初始化类中对象数据的成员
BOOL AFXAPI AfxWinInit(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance,
 _In_z_ LPTSTR lpCmdLine, _In_ int nCmdShow)
{
 ASSERT(hPrevInstance == NULL);


 // handle critical errors and avoid Windows message boxes
 SetErrorMode(SetErrorMode(0) |
  SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);

 // set resource handles
 AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
 pModuleState->m_hCurrentInstanceHandle = hInstance;
 pModuleState->m_hCurrentResourceHandle = hInstance;
 pModuleState->CreateActivationContext();

 // fill in the initial state for the application
 CWinApp* pApp = AfxGetApp();
 if (pApp != NULL)
 {
  // Windows specific initialization (not done if no CWinApp)
  pApp->m_hInstance = hInstance;
  hPrevInstance; // Obsolete.
  pApp->m_lpCmdLine = lpCmdLine;
  pApp->m_nCmdShow = nCmdShow;
  pApp->SetCurrentHandles();
 }

 // initialize thread specific data (for main thread)
 if (!afxContextIsDLL)
  AfxInitThread();

 // Initialize CWnd::m_pfnNotifyWinEvent
 HMODULE hModule = ::GetModuleHandle(_T("user32.dll"));
 if (hModule != NULL)
 {
  CWnd::m_pfnNotifyWinEvent = (CWnd::PFNNOTIFYWINEVENT)::GetProcAddress(hModule,

"NotifyWinEvent");
 }

 return TRUE;
}


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

BOOL CWinApp::InitApplication()
{
 if (CDocManager::pStaticDocManager != NULL)
 {
  if (m_pDocManager == NULL)
   m_pDocManager = CDocManager::pStaticDocManager;
  CDocManager::pStaticDocManager = NULL;
 }

 if (m_pDocManager != NULL)
  m_pDocManager->AddDocTemplate(NULL);
 else
  CDocManager::bStaticInit = FALSE;

 LoadSysPolicies();

 return TRUE;
}                                                                                           //完成了注册窗口
///

 // 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();                                        //进入成员函数的消息循环

/
int CWinApp::Run()
{
 if (m_pMainWnd == NULL && AfxOleGetUserCtrl())
 {
  // Not launched /Embedding or /Automation, but has no main window!
  TRACE(traceAppMsg, 0, "Warning: m_pMainWnd is NULL in CWinApp::Run - quitting

application.\n");
  AfxPostQuitMessage(0);
 }
 return CWinThread::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;
}

 
mfc对系统的api函数进行封装形成类,但程序的运行始终是从入口函数开始的。只不过在进入口前完成全局对象myApp的初始化,然后入口函数通过类对象调用成员函数完成窗口注册,创建,显示,消息循环,入口函数结束,程序也就结束了。

转载于:https://www.cnblogs.com/aswater-yuanye/archive/2012/05/07/2487659.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值