• CWnd::Attach()具体解释


    CWnd::Attach

    Attaches a Windows window to a CWnd object.

    BOOL Attach(

       HWND hWndNew

    );

    Parameters

    hWndNew

    Specifies a handle to a Windows window.

    一个概念:C++对象和Windows对象的差别(来自MSDN)

    The window object is an object of the C++ CWnd class (or a derived class) that your program creates directly. It comes and goes in response to your program's constructor and destructor calls. The Windows window, on the other hand, is an opaque handle to an internal Windows data structure that corresponds to a window and consumes system resources when present. A Windows window is identified by a "window handle" (HWND) and is created after the CWndobject is created by a call to the Create member function of class CWnd. The window may be destroyed either by a program call or by a user's action. The window handle is stored in the window object's m_hWnd member variable. The following figure shows the relationship between the C++ window object and the Windows window. Creating windows is discussed in Creating Windows. Destroying windows is discussed in Destroying Window Objects

     

    一段解说:

     

    首先。你要明确Windows对象和C++对象的差别。

    C++对象(即MFC类CWnd)实际上并没有把整个Windows对象都包装在当中,它仅仅是有一个窗体句柄。

    MSDN上查看CWnd类的成员变量。确实仅仅有一个HWND hWnd。成员函数有Attach()Dettach()Create())。

    这个窗体句柄假设指向一个实际存在的窗体对象,那么这个C++对象就是有效的,否则这个MFC对象是空的。

    假设你还不明确,请回顾一下。当我们使用MFC创建一个窗体时。是分两步进行的:

    第一步,new一个CWnd对象。这一步是创建C++对象,可是当中的HWND还是非法的。由于相应的Windows对象还没有被创建出来;

    第二步。调用CWnd的成员函数Create创建真正的Windows对象。同一时候,把先前创建的MFC的CWnd对象的HWND成员指向该窗体,这样才算创建完毕一个窗体。

    而假设你是用SDK方式。那么仅仅要创建一个WNDCLASS结构,然后调用Create或者CreateEx就创建了一 个窗体。

          好,如今回答你的问题,你能够假设,如今你已经有了一个有效窗体句柄(指向(并标识)了一个详细的Windows window。那么你想把这个窗体和一个CWnd对象关联起来怎么办?非常easy,用Attach:事实上就是让一个C++对象——CWnd对象的HWND成员等于这个窗体句柄。这就是Attach主要完毕的任务。 

         第二个。关于Detach。如前所述,WNDCLASS事实上和CWnd根本没有什么关系。它们之间仅仅是通过CWnd的成员HWND联系起来的。假设把 Attach看做“联姻”的话。那么Detach就是“离婚”了,通俗地说,就是切断一个CWnd对象和一个有效窗体的脐带。为什么要切断呢?由于 CWnd是C++的对象,C++的对象有一个生存期的概念,脱离了该对象的作用域,这个对象就要被销毁,可是Windows对象没有这个特点,当销毁 CWnd对象的时候。我们不一定希望WNDCLASS一起被销毁。那么在此之前。我们就先要把这个“脐带”剪断,以免“城门失火。殃及池鱼”。

    一个代码演示样例:

     1void CMyCapView::OnLButtonUp(UINT nFlags, CPoint point)
     2{
     3    // TODO: 在此加入消息处理程序代码和/或调用默认值
     4    
     5    CMyCapDoc* pDoc=(CMyCapDoc*)GetDocument();
     6    if (m_nStatus==1)
     7    {
     8        CWnd srcWnd;
     9        //将一个窗体attach到一个CWnd对象上。原因例如以下句凝视。详细见CSDN笔记
    10        //MSDN原文:Attaches a Windows window to a CWnd object.
    11        srcWnd.Attach(hwndCapture);
    12        //CaptureDIB函数參数要求是CWnd类的指针。所以不能直接用hwndCapture,而要将hwndCapture跟一个
    13        //CWnd对象结合起来,使这个CWnd对象就是hwndCapture指向的窗体。
    14        pDoc->m_dib.CaptureDIB(&srcWnd,rectCapture);
    15        //终止鼠标捕获
    16        ReleaseCapture();
    17        //恢复窗体显示模式
    18        CMyCapApp* theApp=(CMyCapApp*)AfxGetApp();
    19        theApp->m_pMainWnd->ShowWindow(SW_NORMAL);
    20        m_nStatus=0;
    21        srcWnd.Detach();
    22        Invalidate(false);
    23    }

    24    CView::OnLButtonUp(nFlags, point);
    25}

     
    总结:由于Detach()函数是CWnd对象来调用的,所以能够理解为一个内部空虚的C++对象在呼唤一个有模有样的Windows窗体。

  • 相关阅读:
    SQLiteStudio免费的mac下的sqlite 客户端工具
    pip python mysqlclient 报各种错
    python Flask安装
    配置本地资源映射路径 addResourceHandlers
    微信支付 统一下单 签名错误
    utf8mb4_bin与utf8_genera_ci
    Java中Synchronized的用法(简单介绍)
    javaScript正则表达式的使用
    中文正则表达式匹配-正则中文匹配
    excel之CMMI-FP功能点估算辅助生成DET、RET、FTR、FP
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10466156.html
  • Copyright © 2020-2023  润新知