记事缘由:虽接触良久,但一直不求甚解,关键也没开源,大概开源我也懒得看吧。不求甚解带来了很多恶果就是,诡异情况频出,鬼知道我怎么会碰到这么多问题。
一同事为了解决程序主窗口启动前的登录窗口显示期间也显示任务栏图标,于是就在登录前把AfxGetApp()->m_pMainWnd设为了登录窗口,登录完毕后再重置为主窗口。乍听没啥问题,后面我在Initinstance中DoModal()返回-1,才发现了此处的问题。
原因:用该成员变量m_pMainWnd 去存储你的线程主窗口对象。当和m_pMainWnd 相关的窗口被关闭后,MFC会自动终止你的线程。如果该线程是应用程序主线程,程序也将会被终止。如果该数据成员为NULL,应用程序CWinApp对象的主窗口将用来决定什么时候去终止线程。也就是说我的ui线程被终止了,后面再进行Domodal(),都不会成功了,顺带的其他用到AfxGetApp()->m_pMainWnd的地方例如打印等功能也无法使用。
MSDN上的原文解释:
CWinThread::m_pMainWnd
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.
找到原因,要改只能给前任填坑了,登录框也显示图标,话说直接修改登录对话框的Application Window置为true不久行了= =