PreCreateWindow函數, PreSubClassWindow函數

這兩個函數是MFC的基類CWnd兩個很重要的虛函數,萬惡的MFC總是讓人很煩惱。

class CWnd : public CCmdTarget
{
    
public:
    
        virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    virtual void PreSubclassWindow();
    BOOL SubclassWindow(HWND hWnd);
    
}
;
 

 父類中的虛擬函數是可以被子類重新實現的,以實現多態。一般基類會有一個默認的實現。

   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)的區別:窗口句柄是windows的系統級的概念,代表着一個具體的窗口的句柄,可以通過操作這個句柄來修改窗口的屬性已經行爲。對應sdk編程

的HWND。

而CWnd就是MFC用類對window所進行C++封裝。attach,就是在CWnd對象和窗口句柄建立一個關聯。這樣就可以用CWnd對象的接口來修改實際窗口的

屬性和行爲。

  好了,PreCreateWindow由framework在窗口創建前被調用,函數名也說明了這一點,Pre應該是previous的縮寫,PreSubclassWindow由framework在subclass窗口前調用。 這段話說了等於沒說,你可能還是不知道,什麼時候該改寫哪個函數。羅羅嗦嗦的作者,還是用代碼說話吧!源碼之前,了無祕密(候捷語)。我們就看看MFC中的這三個函數都是這樣實現的吧!
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;
}


// 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;
}
  CWnd::CreateEx先設定cs(CREATESTRUCT),在調用真正的窗口創建函數::CreateWindowEx之前,調用了CWnd::PreCreateWindow函數,並把參數cs以引用的方式傳遞了進去。而CWnd的PreCreateWindow函數也只是給cs.lpszClass賦值而已。畢竟,窗口創建函數CWnd::CreateEx的諸多參數中,並沒有哪個指定了所要創建窗口的窗口類,而這又是不可缺少的(請參考<<windows程序設計>>第三章)。所以當你需要修改窗口的大小、風格、窗口所屬的窗口類等cs成員變量時,要改寫PreCreateWindow函數。
// From VS Install PathVC98MFCSRCWINCORE.CPP
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)前,調用了PreSubclassWindow。CWnd::PreSubclassWindow則是什麼都沒有做。
  在CWnd的實現中,除了CWnd::SubclassWindow會調用PreSubclassWindow外,還有一處。上面所列函數CreateEx的代碼,其中調用了一個AfxHookWindowCreate函數,見下面代碼:
// From VS Install PathVC98MFCSRCWINCORE.CPP
BOOL CWnd::CreateEx()
{
    // allow modification of several common create parameters
    
        
        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;
}
  接着察看AfxHookWindowCreate的代碼:

// From VS Install PathVC98MFCSRCWINCORE.CPP
void AFXAPI AfxHookWindowCreate(CWnd* pWnd)
{
    
        
        if (pThreadState->m_hHookOldCbtFilter == NULL)
            {
            pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
                _AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
            if (pThreadState->m_hHookOldCbtFilter == NULL)
                AfxThrowMemoryException();
        }

        
}

  其主要作用的::SetWindowsHookEx函數用於設置一個掛鉤函數(Hook函數)_AfxCbtFilterHook,每當Windows產生一個窗口時(還有許多其它類似,請參考<<深入淺出MFC>>第9章,563頁),就會調用你設定的Hook函數。
  這樣設定完成後,回到CWnd::CreateEx函數中,執行::CreateWindowEx進行窗口創建,窗口一產生,就會調用上面設定的Hook函數_AfxCbtFilterHook。而正是在_AfxCbtFilterHook中對函數PreSubclassWindow進行了第二次調用。見如下代碼:
// From VS Install PathVC98MFCSRCWINCORE.CPP
/**//////////////////////////////////////////////////////////////////////////////
// Window creation hooks

LRESULT CALLBACK
_AfxCbtFilterHook(
int code, WPARAM wParam, LPARAM lParam)
{
           
        
        // connect the HWND to pWndInit
        pWndInit->Attach(hWnd);
    // allow other subclassing to occur first
    pWndInit->PreSubclassWindow();
    
        {
        // subclass the window with standard AfxWndProc
        oldWndProc = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)afxWndProc);
        ASSERT(oldWndProc != NULL);
        *pOldWndProc = oldWndProc;
    }

    
}

  也在調用函數SetWindowLong進行窗口subclass前調用了PreSubclassWindow.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章