CWnd* m_pMainWnd; // main window (usually same AfxGetApp()->m_pMainWnd)
...
m_pMainWnd = &dlg;
Use this data member to store a pointer to your thread’s main window object. The Microsoft Foundation Class Library will automatically terminate your thread when the window referred to by m_pMainWnd is closed. If this thread is the primary thread for an application, the application will also be terminated. If this data member is NULL, the main window for the application’s CWinApp object will be used to determine when to terminate the thread. m_pMainWnd is a public variable of type CWnd*.
Typically, you set this member variable when you override InitInstance. In a worker thread, the value of this data member is inherited from its parent thread.
用該成員變量去存儲(chǔ)你的線程主窗口對(duì)象。當(dāng)和m_pMainWnd 相關(guān)的窗口被關(guān)閉后,MFC會(huì)自動(dòng)終止你的線程。如果該線程是應(yīng)用程序主線程,程序也將會(huì)被終止。如果該數(shù)據(jù)成員為NULL,應(yīng)用程序CWinApp對(duì)象的主窗口將用來(lái)決定什么時(shí)候去終止線程。m_pMainWnd是一個(gè)CWnd*類型的public變量。
很明顯,你需要在重載InitInstance時(shí)為m_pMainWnd賦值。在工作線程中,m_pMainWnd自動(dòng)繼承其父線程的值。