Attach函数的讲解

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++对象(即MFCCWnd)实际上并没有把整个Windows对象都包装在其中,它只是有一个窗口句柄。(MSDN上查看CWnd类的成员变量,确实只有一个HWND hWnd,成员函数有Attach()Dettach()Create())。这个窗口句柄如果指向一个实际存在的窗口对象,那么这个C++对象就是有效的,否则这个MFC对象是空的。

如果你还不明白,请回忆一下,当我们使用MFC创建一个窗口时,是分两步进行的:

第一步,new一个CWnd对象,这一步是创建C++对象,但是其中的HWND还是非法的,因为对应的Windows对象还没有被创建出来;

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

而如果你是用SDK方式,那么只要创建一个WNDCLASS结构,然后调用Create或者CreateEx就创建了一 个窗口。

      好,现在回答你的问题,你可以假设,现在你已经有了一个有效窗口句柄(指向(并标识)了一个具体的Windows window,那么你想把这个窗口和一个CWnd对象关联起来怎么办?很简单,用Attach:其实就是让一个C++对象——CWnd对象的HWND成员等于这个窗口句柄。这就是Attach主要完成的任务。 

     第二个,关于Detach。如前所述,WNDCLASS其实和CWnd根本没有什么关系。它们之间只是通过CWnd的成员HWND联系起来的。如果把 Attach看做联姻的话,那么Detach就是离婚了,通俗地说,就是切断一个CWnd对象和一个有效窗口的脐带。为什么要切断呢?因为 CWndC++的对象,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窗口。

posted on 2011-02-25 10:18 lateCpp 阅读(4084) 评论(1)  编辑 收藏 引用 所属分类: MFC

评论

# re: Attach函数的讲解 2012-06-22 02:31 虚空骄阳

非常不错,讲得比较通俗  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

常用链接

留言簿

随笔分类

文章分类

文章档案

搜索

最新评论