PreCreateWindow、PreSubclassWindow、SubclassWin MFC

通常情况下窗口是由用户创建的 CWnd::Create(…) ,在此流程中,MFC提供一个机会"PreCreateWindow()供用户在创建前修改窗口的样式。

BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,LPCTSTR lpszWindowName, DWORD dwStyle,int x, int y, int nWidth, 
                   int nHeight,HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam){ 
     // allow modification of several common create parameters
     CREATESTRUCT cs;
     cs.dwExStyle = dwExStyle;
     cs.lpszClass = lpszClassName;
     cs.lpszName = lpszWindowName;
     cs.style = dwStyle;
     cs.x = x;
     cs.y = y;
     cs.cx = nWidth;
     cs.cy = nHeight;
     cs.hwndParent = hWndParent;
     cs.hMenu = nIDorHMenu;
     cs.hInstance = AfxGetInstanceHandle();
     cs.lpCreateParams = lpParam;
     if (!PreCreateWindow(cs)){
         PostNcDestroy();
         return FALSE;
     }
     AfxHookWindowCreate(this);
     HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,
         	 cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
        	 cs.hwndParent, cs.hMenu, cs.hInstance,
        	 cs.lpCreateParams);	   
     return TRUE;
}

PreCreateWindow的定义

// for child windows
BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs)
{
     if (cs.lpszClass == NULL){
         // make sure the default window class is registered
         VERIFY(AfxDeferRegisterClass(AFX_WND_REG));
         // no WNDCLASS provided - use child window default
         ASSERT(cs.style & WS_CHILD);
         cs.lpszClass = _afxWnd;
     }
     return TRUE;
}  

然而对于自定义的子窗口(对话框里通过dx数据绑定的窗口,也即在资源文件里定义的控件),MFC提供一个"PreSubclassWindow()“的虚函数供用户重载,用来实现在子窗口显示之前修改子窗口的样式。它们是通过subclass方式交给用户的 ,系统首先读入对话框模板,建立其中各个子窗口 ,然后将各子窗口的消息处理函数替换成 对应的C++对象 的消息处理函数 (Subclass:子类化,或"接管”) ,子窗口就会按类中定义的方式来动作了。在此过程中,调用的是CWnd:SubclassWindow( HWND hWnd )。

BOOL CWnd::SubclassWindow(HWND hWnd)
{
     if (!Attach(hWnd))
         return FALSE;
     // allow any other subclassing to occur
     PreSubclassWindow();    
     // now hook into the AFX WndProc
     WNDPROC* lplpfn = GetSuperWndProcAddr();
     WNDPROC oldWndProc = (WNDPROC)::SetWindowLong(hWnd, GWL_WNDPROC,(DWORD)AfxGetAfxWndProc());
     ASSERT(oldWndProc != (WNDPROC)AfxGetAfxWndProc());    
     if (*lplpfn == NULL)
         *lplpfn = oldWndProc; // the first control of that type created
     #ifdef _DEBUG
     	else if (*lplpfn != oldWndProc){        
             ::SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);
    	 }  
     #endif    
     return TRUE;
}
void CWnd::PreSubclassWindow()
{
     // no default processing
}   

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值