UINT CTestDlgDlg::ThreadFunc(PVOID pv)
{
//模态对话框
// CAboutDlg dlg;
// dlg.DoModal();
//非模态对话框
CTestDlgDlg *Testdlg = (CTestDlgDlg*)pv;
CDlgThread *dlg = new CDlgThread;
dlg->Create(IDD_DIALOG1);
Testdlg->m_MFCThreadHwnd = dlg->m_hWnd;
Testdlg->dlg = dlg;
dlg->ShowWindow(SW_SHOW);
//The message loop
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
dlg->DestroyWindow();
delete dlg;
Testdlg->dlg = NULL;
TRACE(_T("释放对话框"));
return 0;
}
void CTestDlgDlg::OnButton4()
{
// TODO: Add your control notification handler code here
m_MFCThreadWnd = AfxBeginThread((AFX_THREADPROC)ThreadFunc,this);
}
在线程中创建窗口的重点在于,消息循环必须要跟创建窗口在同一线程;但是如果你是在线程中创建的是模态窗口的话,就不需要了