如果直接在线程里调用AfxGetMainWnd可能会出错,原因:

_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
{ CWinThread* pThread = AfxGetThread();
   return pThread != NULL ? pThread->GetMainWnd() : NULL; }

而AfxGetThread获取的是当前线程,而不是主线程!
CWinThread* AFXAPI AfxGetThread()
{
// check for current thread in module thread state
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
CWinThread* pThread = pState->m_pCurrentWinThread;
return pThread;
}

解决办法可以参考http://blog.csdn.net/genius52/archive/2009/01/16/3797863.aspx

其实AfxGetApp()->m_pMainWnd就是“真正”的主线程窗口了!也就是说:
CWnd* pMainWnd = AfxGetMainWnd();
要改写成下面就OK了:
CWnd* pMainWnd = AfxGetApp()->m_pMainWnd;

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-02-19
  • 2022-12-23
  • 2021-11-01
  • 2022-02-06
  • 2021-05-13
  • 2021-08-08
猜你喜欢
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案