http://jetyi.blog.51cto.com/1460128/1074315/
- class CUIThread : public CWinThread
- {
- DECLARE_DYNCREATE(CUIThread)
- protected:
- CUIThread(); // protected constructor used by dynamic creation
- virtual ~CUIThread();
- public:
- virtual BOOL InitInstance();
- virtual int ExitInstance();
- protected:
- DECLARE_MESSAGE_MAP()
- public:
- HWND m_hParentWnd; //注意,它是父窗口句柄,不能是CWnd*对象.
- };
- BOOL CUIWinThread::InitInstance()
- {
- // TODO: perform and per-thread initialization here
- ASSERT(::IsWindow(m_ hParentWnd));
- CWnd* pParent = CWnd::FromHandle(m_hParentWnd);//注意这行
- CUIChildDlg* pDlg = new CUIChildDlg(pParent);
- pDlg->Create(CUIChildDlg::IDD, pParent);
- pDlg->ShowWindow(SW_SHOW);
- return TRUE;
- }
- void CUIChildDlg::OnNcDestroy()
- {
- CDialog::OnNcDestroy();
- // TODO: Add your message handler code here
- ::PostQuitMessage(0);//为了使线程自动退出.
- }
- BOOL CMFCSingleDocTestApp::InitInstance()
- {
- ... …
- // The one and only window has been initialized, so show and update it
- m_pMainWnd->ShowWindow(SW_SHOW);//主窗口
- m_pMainWnd->UpdateWindow();
- // call DragAcceptFiles only if there's a suffix
- // In an SDI app, this should occur after ProcessShellCommand
- m_pUIThread = (CUIWinThread*)AfxBeginThread(RUNTIME_CLASS(CUIWinThread),
- THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);//创建后先不要启动.
- m_pUIThread->m_hParentWnd = m_pMainWnd->m_hWnd;//主窗口句柄.
- m_pUIThread->ResumeThread();
- return TRUE;
- }
- int CMFCSingleDocTestApp::ExitInstance()
- {
- //TODO: handle additional resources you may have added
- AfxOleTerm(FALSE);
- ASSERT(NULL != m_pUIThread);
- ::WaitForSingleObject(m_pUIThread->m_hThread, INFINITE);
- return CWinAppEx::ExitInstance();
- }