在其它对话框的线程函数中获取主对话框的指针!!!
有一个主对话框MainDlg和另外一个对话框NewDlg,现在在NewDlg中里面有一个线程,要用到MainDlg的指针,我不太确定这个NewDlg是否是MainDlg的子对话框,而且用GetParent也的确取不到CMainDlg的指针。
- C/C++ code
UINT CNewDlg::PlayProc(LPVOID lpParameter){ if (lpParameter) { CNewDlg *pDlg = (CNewDlg*)lpParameter; CMainDlg *pMainDlg = (CMainDlg*)pDlg->GetParent(); if(pMainDlg->m_str == _T("Yes")) {//这个if判断会出错 AfxMessageBox(_T("获取主对话框指针成功")); } }}而如果直接用AfxGetMainWnd取主对话框的指针似乎也不行。
CMainDlg *pMainDlg = (CMainDlg*)AfxGetMainWnd(); //获取主窗口指针
添加这句话也会出错。和用GetParent出现同样的错误,都是会进入下面的这个语句
- C/C++ code
operator T*() const throw() { return( m_p ); }请高手指教啊!!!!
[解决办法]
在创建NewDlg的时候,把MainDlg句柄传入进去
[解决办法]
CWnd* pWnd = AfxGetApp()->GetMainWnd();
这个直接就是主对话框,不用 GetParent 了。
[解决办法]
2楼正解
AfxGetMainWnd取的是当前线程的主窗口
只有在主线程中才跟 AfxGetApp()->GetMainWnd() 返回同一个窗口指针
[解决办法]
If AfxGetMainWnd is called from the application's primary thread, it returns the application's main window according to the above rules. If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that made the call.所以1,2楼说的都是对的