---
title: "CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别"
author: "swimmer2000"
date: "2007-10-30T11:26:00Z"
tags: []
slug: "cwnd-precreatewindow-presubclasswindow-subclasswindow"
draft: false
lastmod: "2007-10-30T11:26:00Z"
id: 441520
url: "https://swimmer2000.iteye.com/blog/441520"
---

MFC(VC6.0)的CWnd及其子类中，有如下三个函数：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//FromVSInstallPathVC98MFCIncludeAFXWIN.H  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)classCWnd:publicCCmdTarget  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)public:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)virtualBOOLPreCreateWindow(CREATESTRUCT&cs);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)virtualvoidPreSubclassWindow();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BOOLSubclassWindow(HWNDhWnd);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

让人很不容易区分，不知道它们究竟干了些什么，在什么情况下要改写哪个函数？

想知道改写函数？让我先告诉你哪个不能改写，那就是SubclassWindow。Scott Meyers的杰作&lt;&lt;Effective C++&gt;&gt;的第36条是这样的:Differentiate between inheritance of interface and inheritance of implementation. 看了后你马上就知道，父类中的非虚拟函数是设计成不被子类改写的。根据有无virtual关键字，我们在排除了SubclassWindow后，也就知道PreCreateWindow和PreSubClassWindow是被设计成可改写的。接着的问题便是该在什么时候该写了。要知道什么时候该写，必须知道函数是在什么时候被调用，还有执行函数的想要达到的目的。我们先看看对这三个函数，MSDN给的解释:

PreCreateWindow:

Called by the framework before the creation of the Windows window  
attached to this CWnd object.  
(译:在窗口被创建并attach到this指针所指的CWnd对象之前，被framework调用)

PreSubclassWindow:

This member function is called by the framework to allow other necessary  
subclassing to occur before the window is subclassed.  
(译:在window被subclassed之前被framework调用，用来允许其它必要的  
subclassing发生)

虽然我已有译文，但还是让我对CWnd的attach和窗口的subclass作简单的解释吧！要理解attach，我们必须要知道一个C++的CWnd对象和窗口(window)的区别：window就是实在的窗口，而CWnd就是MFC用类对window所进行C++封装。attach，就是把窗口附加到CWnd对象上操作。附加(attach)完成后，CWnd对象才和窗口发生了联系。窗口的subclass是指修改窗口过程的操作，而不是面向对象中的派生子类。

好了，PreCreateWindow由framework在窗口创建前被调用，函数名也说明了这一点，Pre应该是previous的缩写，PreSubclassWindow由framework在subclass窗口前调用。 这段话说了等于没说，你可能还是不知道，什么时候该改写哪个函数。罗罗嗦嗦的作者，还是用代码说话吧！源码之前，了无秘密(候捷语)。我们就看看MFC中的这三个函数都是这样实现的吧！

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//FromVSInstallPathVC98MFCSRCWINCORE.CPP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)BOOLCWnd::CreateEx(DWORDdwExStyle,LPCTSTRlpszClassName,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)LPCTSTRlpszWindowName,DWORDdwStyle,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)intx,inty,intnWidth,intnHeight,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)HWNDhWndParent,HMENUnIDorHMenu,LPVOIDlpParam)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//allowmodificationofseveralcommoncreateparameters  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)CREATESTRUCTcs;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.dwExStyle=dwExStyle;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.lpszClass=lpszClassName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.lpszName=lpszWindowName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.style=dwStyle;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.x=x;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.y=y;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.cx=nWidth;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.cy=nHeight;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.hwndParent=hWndParent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.hMenu=nIDorHMenu;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.hInstance=AfxGetInstanceHandle();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.lpCreateParams=lpParam;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!PreCreateWindow(cs))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PostNcDestroy();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnFALSE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)AfxHookWindowCreate(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HWNDhWnd=::CreateWindowEx(cs.dwExStyle,cs.lpszClass,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.lpszName,cs.style,cs.x,cs.y,cs.cx,cs.cy,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.hwndParent,cs.hMenu,cs.hInstance,cs.lpCreateParams);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnTRUE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//forchildwindows  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)BOOLCWnd::PreCreateWindow(CREATESTRUCT&cs)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(cs.lpszClass==NULL)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//makesurethedefaultwindowclassisregistered  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)VERIFY(AfxDeferRegisterClass(AFX\_WND\_REG));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//noWNDCLASSprovided-usechildwindowdefault  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ASSERT(cs.style&WS\_CHILD);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.lpszClass=\_afxWnd;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnTRUE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

CWnd::CreateEx先设定cs(CREATESTRUCT)，在调用真正的窗口创建函数::CreateWindowEx之前，调用了CWnd::PreCreateWindow函数，并把参数cs以引用的方式传递了进去。而CWnd的PreCreateWindow函数也只是给cs.lpszClass赋值而已。毕竟，窗口创建函数CWnd::CreateEx的诸多参数中，并没有哪个指定了所要创建窗口的窗口类，而这又是不可缺少的(请参考&lt;&lt;windows程序设计&gt;&gt;第三章)。所以当你需要修改窗口的大小、风格、窗口所属的窗口类等cs成员变量时，要改写PreCreateWindow函数。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//FromVSInstallPathVC98MFCSRCWINCORE.CPP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)BOOLCWnd::SubclassWindow(HWNDhWnd)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!Attach(hWnd))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnFALSE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//allowanyothersubclassingtooccur  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PreSubclassWindow();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//nowhookintotheAFXWndProc  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)WNDPROC\*lplpfn=GetSuperWndProcAddr();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)WNDPROColdWndProc=(WNDPROC)::SetWindowLong(hWnd,GWL\_WNDPROC,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)(DWORD)AfxGetAfxWndProc());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ASSERT(oldWndProc!=(WNDPROC)AfxGetAfxWndProc());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(\*lplpfn==NULL)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*lplpfn=oldWndProc;//thefirstcontrolofthattypecreated  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)#ifdef\_DEBUG  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(\*lplpfn!=oldWndProc)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)::SetWindowLong(hWnd,GWL\_WNDPROC,(DWORD)oldWndProc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)#endif  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnTRUE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidCWnd::PreSubclassWindow()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//nodefaultprocessing  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

CWnd::SubclassWindow先调用函数Attach(hWnd)让CWnd对象和hWnd所指的窗口发生关联。接着在用::SetWindowLong修改窗口过程(subclass)前，调用了PreSubclassWindow。CWnd::PreSubclassWindow则是什么都没有做。

在CWnd的实现中，除了CWnd::SubclassWindow会调用PreSubclassWindow外，还有一处。上面所列函数CreateEx的代码，其中调用了一个AfxHookWindowCreate函数，见下面代码：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//FromVSInstallPathVC98MFCSRCWINCORE.CPP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)BOOLCWnd::CreateEx(...)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//allowmodificationofseveralcommoncreateparameters  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!PreCreateWindow(cs))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)PostNcDestroy();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnFALSE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)AfxHookWindowCreate(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HWNDhWnd=::CreateWindowEx(cs.dwExStyle,cs.lpszClass,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.lpszName,cs.style,cs.x,cs.y,cs.cx,cs.cy,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cs.hwndParent,cs.hMenu,cs.hInstance,cs.lpCreateParams);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnTRUE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

接着察看AfxHookWindowCreate的代码：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//FromVSInstallPathVC98MFCSRCWINCORE.CPP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidAFXAPIAfxHookWindowCreate(CWnd\*pWnd)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(pThreadState-&gt;m\_hHookOldCbtFilter==NULL)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pThreadState-&gt;m\_hHookOldCbtFilter=::SetWindowsHookEx(WH\_CBT,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\_AfxCbtFilterHook,NULL,::GetCurrentThreadId());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(pThreadState-&gt;m\_hHookOldCbtFilter==NULL)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)AfxThrowMemoryException();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

其主要作用的::SetWindowsHookEx函数用于设置一个挂钩函数(Hook函数)\_AfxCbtFilterHook，每当Windows产生一个窗口时(还有许多其它类似，请参考&lt;&lt;深入浅出MFC&gt;&gt;第9章，563页)，就会调用你设定的Hook函数。

这样设定完成后，回到CWnd::CreateEx函数中，执行::CreateWindowEx进行窗口创建，窗口一产生，就会调用上面设定的Hook函数\_AfxCbtFilterHook。而正是在\_AfxCbtFilterHook中对函数PreSubclassWindow进行了第二次调用。见如下代码：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//FromVSInstallPathVC98MFCSRCWINCORE.CPP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//////////////////////////////////////////////////////////////////////////////  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//Windowcreationhooks  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)LRESULTCALLBACK  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)\_AfxCbtFilterHook(intcode,WPARAMwParam,LPARAMlParam)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//connecttheHWNDtopWndInit...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pWndInit-&gt;Attach(hWnd);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//allowothersubclassingtooccurfirst  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pWndInit-&gt;PreSubclassWindow();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//subclassthewindowwithstandardAfxWndProc  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)oldWndProc=(WNDPROC)SetWindowLong(hWnd,GWL\_WNDPROC,(DWORD)afxWndProc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ASSERT(oldWndProc!=NULL);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*pOldWndProc=oldWndProc;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

也在调用函数SetWindowLong进行窗口subclass前调用了PreSubclassWindow.