AfxGetMainWnd()的使用依赖于线程
具体参加源代码
1
_AFXWIN_INLINE CWnd
*
AFXAPI AfxGetMainWnd()
2
{ CWinThread* pThread = AfxGetThread();
3
return pThread != NULL ? pThread->GetMainWnd() : NULL; }
4
5
//
而AfxGetThread获取的是当前线程,而不是主线程!
6
CWinThread
*
AFXAPI AfxGetThread()
7
{
8
// check for current thread in module thread state
9
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
10
CWinThread* pThread = pState->m_pCurrentWinThread;
11
return pThread;
12
}
13

2

3

4

5

6

7

8

9

10

11

12

13

所以在非主线程里使用可能会有问题,解决方法,在新创建的线程里使用AfxGetApp()->m_pMainWnd;