程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> 關於VC++ >> 關於MFC內部結構實用的一點看法

關於MFC內部結構實用的一點看法

編輯:關於VC++

我們在編寫程序或是在看MFC類的書籍的時候,總會看到書上讓我們在某個地方添加某某 函數,響應什麼消息。那麼,到底為什麼要在這地方加呢?為什麼不能在其他地方添加消息 響應函數呢?

接下來,我們就共同來討論一下吧。

1.MFC的運行步驟:(這 是對於文檔視圖結構的應用程序的)

theApp(theApp())->//應用程序全局對象 的構造和內存分配
WinMain()->//進入主函數
AfxWinMain()->//調用 API
InitAplacation()(應用程序的內部管理)->//用於應用程序的內部管理的初始 化
Initinstance()(theApp)->//初始化應用程序對象
應用程序的 Initinstance()->//應用程序的初始化(創建和注冊窗口類、顯示和更新應用程序框架)
注冊窗口類->
AfxEndDeferRegisterClass ()(該函數根據不同的應用程序注 冊不同的窗口類)->
AfxRegisterClass()->
CMainFrame::PreCreateWindow()->
CFrameWnd::PreCreateWindow()->
AfxDeferRegisterClass()(如果沒有注冊,就注冊窗口類;它實際上是 AfxEndRegisterClass()的宏)(一般都是在這裡注冊窗口類,由於文檔窗口涉及到文檔視圖類 的管理)->
LoadFrame()->//載入框架
CFrameWnd::Create()->/
CWnd::CreateEx()->//創建應用程序窗口
CMainFrame::PreCreateWindow()(實際 上該函數是一個虛函數,也就是說如果子類為應用程序定義了該函數,那麼它將調用子類的 函數,因此,我們可以重載該函數來修改應用程序框架的外觀)->//在框架窗口顯示之前 調用
ShowWindow()->UpdateWindow()->//顯示和更新
消息循環:- >
CwinThread::Run()->
do-while()循環(->PumpMessage()- >GetMessage()->TranslateMessage()->DispatchMessage())

簡單的說 就是初始化全局對象進入主函數再進行窗口類的創建和注冊並顯示,然後進入消息循環。

theApp:全局變量或是類對象在程序運行前初始化。即分配內存空間,初始成員。( 每一個MFC應用程序都只有一個全局對象theApp對象,我們用該全局對象唯一的表示該應用程 序。)

下面我們給出這些函數如何跟蹤(我的是裝在C:盤下的他們都在。。\MFC\SRC\ 下)

WinMain()函數(C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\APPMODUL.CPP)
WinApp(C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\THRDCORE.CPP)
AfxWinMain()(C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\WINMAIN.CPP)
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
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()) // C:\Program Files\Microsoft Visual   Studio\VC98\MFC\include\AFXWIN.H)
  goto InitFailure;
 // Perform specific initializations
 if (!pThread->InitInstance()) // C:\Program Files\Microsoft Visual Studio\VC98\MFC\include\AFXWIN.H)
 {
  if (pThread->m_pMainWnd != NULL)
  {
   TRACE0("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)
 {
  TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
  AfxGetModuleThreadState()->m_nTempMapLock);
 }
 AfxLockTempMaps();
 AfxUnlockTempMaps(-1);
 #endif
 AfxWinTerm();
 return nReturnCode;
}
AfxEndDeferRegisterClass
#define AfxDeferRegisterClass(fClass) AfxEndDeferRegisterClass(fClass)
BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)
{
 // mask off all classes that are already registered
 AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
 fToRegister &= ~pModuleState->m_fRegisteredClasses;
 if (fToRegister == 0)
  return TRUE;
 LONG fRegisteredClasses = 0;
 // common initialization
 WNDCLASS wndcls;
 memset(&wndcls, 0, sizeof(WNDCLASS)); // start with NULL defaults
 wndcls.lpfnWndProc = DefWindowProc;
 wndcls.hInstance = AfxGetInstanceHandle();
 wndcls.hCursor = afxData.hcurArrow;
 INITCOMMONCONTROLSEX init;
 init.dwSize = sizeof(init);
 //各種各樣的窗口類的注冊
 // work to register classes as specified by fToRegister, populate fRegisteredClasses as we go
 if (fToRegister & AFX_WND_REG)
 {
  // Child windows - no brush, no icon, safest default class styles
  wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  wndcls.lpszClassName = _afxWnd;
  if (AfxRegisterClass(&wndcls))
   fRegisteredClasses |= AFX_WND_REG;
 }
 if (fToRegister & AFX_WNDOLECONTROL_REG)
 {
  // OLE Control windows - use parent DC for speed
  wndcls.style |= CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  wndcls.lpszClassName = _afxWndOleControl;
  if (AfxRegisterClass(&wndcls))
   fRegisteredClasses |= AFX_WNDOLECONTROL_REG;
 }
 if (fToRegister & AFX_WNDCONTROLBAR_REG)
 {
  // Control bar windows
  wndcls.style = 0; // control bars don't handle double click
  wndcls.lpszClassName = _afxWndControlBar;
  wndcls.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  if (AfxRegisterClass(&wndcls))
   fRegisteredClasses |= AFX_WNDCONTROLBAR_REG;
 }
 if (fToRegister & AFX_WNDMDIFRAME_REG)
 {
  // MDI Frame window (also used for splitter window)
  wndcls.style = CS_DBLCLKS;
  wndcls.hbrBackground = NULL;
  if (_AfxRegisterWithIcon(&wndcls, _afxWndMDIFrame, AFX_IDI_STD_MDIFRAME))
   fRegisteredClasses |= AFX_WNDMDIFRAME_REG;
 }
 if (fToRegister & AFX_WNDFRAMEORVIEW_REG)
 {
  // SDI Frame or MDI Child windows or views - normal colors
  wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  if (_AfxRegisterWithIcon(&wndcls, _afxWndFrameOrView, AFX_IDI_STD_FRAME))
   fRegisteredClasses |= AFX_WNDFRAMEORVIEW_REG;
 }
 if (fToRegister & AFX_WNDCOMMCTLS_REG)
 {
  // this flag is compatible with the old InitCommonControls() API
  init.dwICC = ICC_WIN95_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WIN95CTLS_MASK);
  fToRegister &= ~AFX_WIN95CTLS_MASK;
 }
 if (fToRegister & AFX_WNDCOMMCTL_UPDOWN_REG)
 {
  init.dwICC = ICC_UPDOWN_CLASS;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_UPDOWN_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_TREEVIEW_REG)
 {
  init.dwICC = ICC_TREEVIEW_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_TREEVIEW_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_TAB_REG)
 {
  init.dwICC = ICC_TAB_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_TAB_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_PROGRESS_REG)
 {
  init.dwICC = ICC_PROGRESS_CLASS;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_PROGRESS_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_LISTVIEW_REG)
 {
  init.dwICC = ICC_LISTVIEW_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_LISTVIEW_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_HOTKEY_REG)
 {
  init.dwICC = ICC_HOTKEY_CLASS;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_HOTKEY_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_BAR_REG)
 {
  init.dwICC = ICC_BAR_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_BAR_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_ANIMATE_REG)
 {
  init.dwICC = ICC_ANIMATE_CLASS;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_ANIMATE_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_INTERNET_REG)
 {
  init.dwICC = ICC_INTERNET_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_INTERNET_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_COOL_REG)
 {
  init.dwICC = ICC_COOL_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_COOL_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_USEREX_REG)
 {
  init.dwICC = ICC_USEREX_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_USEREX_REG);
 }
 if (fToRegister & AFX_WNDCOMMCTL_DATE_REG)
 {
  init.dwICC = ICC_DATE_CLASSES;
  fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_DATE_REG);
 }
 // save new state of registered controls
 pModuleState->m_fRegisteredClasses |= fRegisteredClasses;
 // special case for all common controls registered, turn on AFX_WNDCOMMCTLS_REG
 if ((pModuleState->m_fRegisteredClasses & AFX_WIN95CTLS_MASK) == AFX_WIN95CTLS_MASK)
 {
  pModuleState->m_fRegisteredClasses |= AFX_WNDCOMMCTLS_REG;
  fRegisteredClasses |= AFX_WNDCOMMCTLS_REG;
 }
 // must have registered at least as mamy classes as requested
 return (fToRegister & fRegisteredClasses) == fToRegister;
}
BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
CWnd* pParentWnd, CCreateContext* pContext)
{
 // only do this once
 ASSERT_VALID_IDR(nIDResource);
 ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);
 m_nIDHelp = nIDResource; // ID for help context (+HID_BASE_RESOURCE)
 CString strFullString;
 if (strFullString.LoadString(nIDResource))
  AfxExtractSubString(m_strTitle, strFullString, 0); // first sub-string
 VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG));
 // attempt to create the window
 LPCTSTR lpszClass = GetIconWndClass(dwDefaultStyle, nIDResource);
 LPCTSTR lpszTitle = m_strTitle;
 if (!Create(lpszClass, lpszTitle, dwDefaultStyle, rectDefault,
pParentWnd, MAKEINTRESOURCE(nIDResource), 0L, pContext))
 {
  return FALSE; // will self destruct on failure normally
 }
 // save the default menu handle
 ASSERT(m_hWnd != NULL);
 m_hMenuDefault = ::GetMenu(m_hWnd);
 // load accelerator resource
 LoadAccelTable(MAKEINTRESOURCE(nIDResource));
 if (pContext == NULL) // send initial update
  SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);
 return TRUE;
}
CWnd::CreateEx()
virtual BOOL CreateEx(
 DWORD dwExStyle,
 LPCTSTR lpszClassName,
 LPCTSTR lpszWindowName,
 DWORD dwStyle,
 int x,
 int y,
 int nWidth,
 int nHeight,
 HWND hWndParent,
 HMENU nIDorHMenu,
 LPVOID lpParam = NULL
);
virtual BOOL CreateEx(
 DWORD dwExStyle,
 LPCTSTR lpszClassName,
 LPCTSTR lpszWindowName,
 DWORD dwStyle,
 const RECT& rect,
 CWnd* pParentWnd,
 UINT nID,
 LPVOID lpParam = NULL
);
CMainFrame::PreCreateWindow()
virtual BOOL PreCreateWindow(
 CREATESTRUCT& cs
);
typedef struct tagCREATESTRUCT {
 LPVOID lpCreateParams;
 HANDLE hInstance;
 HMENU hMenu;
 HWND hwndParent;
 int cy;
 int cx;
 int y;
 int x;
 LONG style;
 LPCSTR lpszName;
 LPCSTR lpszClass;
 DWORD dwExStyle;
} CREATESTRUCT;
CwinThread::Run()
int CWinApp::Run()
{
 if (m_pMainWnd == NULL && AfxOleGetUserCtrl())
 {
  // Not launched /Embedding or /Automation, but has no main window!
  TRACE0("Warning: m_pMainWnd is NULL in CWinApp::Run - quitting application.\n");
  AfxPostQuitMessage(0);
 }
 return CWinThread::Run();
}
int CWinThread::Run()
{
 ASSERT_VALID(this);
 // for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
// acquire and dispatch messages until a WM_QUIT message is received.
for (;;)/進入消息循環
{
 // phase1: check to see if we can do idle work
 while (bIdle && !::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE))
 {
  // call OnIdle while in bIdle state
  if (!OnIdle(lIdleCount++))
   bIdle = FALSE; // assume "no idle" state
 }
 // phase2: pump messages while available
 do
 {
  // pump message, but quit on WM_QUIT
  if (!PumpMessage())
   return ExitInstance();
  // reset "no idle" state after pumping "normal" message
  if (IsIdleMessage(&m_msgCur))
  {
   bIdle = TRUE;
   lIdleCount = 0;
  }
 } while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE));
}
ASSERT(FALSE); // not reachable
}

MFC中的窗口銷毀時,對象不一定銷 毀,窗口只是與對象的m-hWnd關聯;但是如果一個對象銷毀了,則擁有該對象的窗口也要銷 毀。

好了,現在相信大家應該至少知道對於什麼時候添加消息和在什麼地方捕獲消息 有一個大致的了解了吧,寫的倉促,難免有錯,還請多多見量,謝謝。希望大家可以一起進 步。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved