Attach函数的讲解

Attach函数的讲解

http://www.cppblog.com/lateCpp/articles/140631.html

MSDN上的函数说明:

 

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 CWnd object 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对象关联起来怎么办?很简单,用Attach:其实就是让一个C++对象——CWnd对象的HWND成员等于这个窗口句柄。这就是Attach主要完成的任务。 

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

一个代码示例:

 1 void  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窗口。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值