• (转)使用AfxGetMainWnd函数的一个心得


    作者:朱金灿

    来源:http://blog.csdn.net/clever101/

          使用AfxGetMainWnd函数获取MFC程序中的主框架类指针是一个常用作法。但是你会发现这一做法有时也会失灵。不信, 你测试一下下面的代码:

        

    [cpp] view plaincopy
     
    1. unsigned __stdcall SecondThreadFunc( void* pArguments )  
    2. {  
    3.    CMainFrame* pMainWnd =  (CMainFrame*)AfxGetMainWnd();  
    4.     if (NULL!=pMainWnd)  
    5.     {  
    6.         CView *pView = pMainWnd->GetActiveView();  
    7.         if (NULL!=pView)  
    8.         {  
    9.             CDC *pDC = pView->GetDC();  
    10.             ASSERT(NULL!=pDC);  
    11.             pDC->TextOut(100,100,_T("来自线程的字符串"));  
    12.             pView->ReleaseDC(pDC);  
    13.         }  
    14.     }  
    15.     return 0;  
    16. }  
    17.     void CMainFrame::OnTest1()  
    18. {  
    19.     // TODO: 在此添加命令处理程序代码  
    20.     HANDLE hThread;  
    21.     unsigned threadID;  
    22.     hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );  
    23.     // Destroy the thread object.  
    24.     CloseHandle( hThread );  
    25. }   

         运行OnTest1函数,你会发现客户区并没有打印"来自线程的字符串"。下面我们把线程函数变一下:

    [cpp] view plaincopy
     
    1. unsigned __stdcall SecondThreadFunc( void* pArguments )  
    2. {  
    3. //   CMainFrame* pMainWnd =  (CMainFrame*)AfxGetMainWnd();  
    4.    COwnerApp *pApp = (COwnerApp *)AfxGetApp();  
    5.     CMainFrame* pMainWnd = (CMainFrame*) pApp->m_pMainWnd;  
    6.     if (NULL!=pMainWnd)  
    7.     {  
    8.         CView *pView = pMainWnd->GetActiveView();  
    9.         if (NULL!=pView)  
    10.         {  
    11.             CDC *pDC = pView->GetDC();  
    12.             ASSERT(NULL!=pDC);  
    13.             pDC->TextOut(100,100,_T("来自线程的字符串"));  
    14.             pView->ReleaseDC(pDC);  
    15.         }  
    16.     }  
    17.     return 0;  
    18. }  

          运行OnTest1函数,我们发现视图客户区出现了"来自线程的字符串"。接下来我们调试进去AfxGetMainWnd函数,发现AfxGetMainWnd函数如下:

        

    [cpp] view plaincopy
     
    1. _AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()  
    2.     { CWinThread* pThread = AfxGetThread();  
    3.         return pThread != NULL ? pThread->GetMainWnd() : NULL; }   

          由于AfxGetThread()函数返回为NULL,所以AfxGetMainWnd函数返回为NULL。为什么会这样呢?下面我提出我的猜想(本人暂时验证不了,仅起抛砖引玉的作用)。我估计是MFC在多线程中大量运用了TLS(线程本地存储)来保存某些状态,主框架窗口指针属于主线程的TLS(线程本地存储)保存的状态,但是应用程序类指针不属于TLS保存的状态,它可以在该进程的任何线程获取。

  • 相关阅读:
    caffe: compile error: Could not open or find file your path~~/resized_data/0 and a total of 2 images .
    caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.
    caffe: test code for Deep Learning approach
    C++ little errors , Big problem
    VGG_19 train_vali.prototxt file
    matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。
    利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning
    matlab 相关代码记录
    论文阅读之 Inferring Analogous Attributes CVPR 2014
    布局的几种方式(静态布局、自适应布局、流式布局、响应式布局、弹性布局)
  • 原文地址:https://www.cnblogs.com/lihaiping/p/AfxGetMainWnd.html
Copyright © 2020-2023  润新知