1.用Win32 API編程時,window procedure比較明顯,那就是程序員自定義window procedure,但Win32提供了一個API函數DefWindowProc(),缺省的處理要交給它。
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wcex;
wcex.lpszClassName = "MyClass";
wcex.lpfnWndProc = (WNDPROC)MyWndProc;
...
RegisterClassEx(&wcex);
HWND hWnd;
hWnd = CreateWindow("MyClass", szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MyWndProc(HWND hWnd,
UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
...
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
2.用MFC,window procedure會復雜一些,先看靜態的,就是MFC預注冊過的那些類,一句話,MFC替你打點好了window procedure的事。
2.1 最抽象的,MFC把window procedure封裝了起來,程序員只需"programming by difference",你對哪個消息感興趣,就建立哪個消息的響應函數。(當然還有虛函數override...)
void CMyClass::OnLButtonDown(UINT nFlags, CPoint pt)
{
...
}
2.2 往底層一點,我們可以說CWnd::WindowProc()是現在的window procedure,它是一個template method,被你"programming by difference"的消息,會被它交給CWnd::OnWndMsg()處理,缺省的,會被它交給CWnd::DefWindowProc()處理。當然,上面說的沒有考慮多態的情況,其實CWnd::OnWndMsg()和CWnd::DefWindowProc()都是虛函數。我們也注意到CWnd::DefWindowProc()中調用了::DefWindowProc(),也就是Win32 API的DefWindowProc()。
class CWnd : public CCmdTarget
{
...
protected:
// for processing Windows messages
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
virtual BOOL OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult);
...
};
///template method
LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = 0;
if (!OnWndMsg(message, wParam, lParam, &lResult))
lResult = DefWindowProc(message, wParam, lParam);
return lResult;
}
//primitive method
LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
if (m_pfnSuper != NULL)
return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);
...
}
2.3 往更底層,來看看MFC預注冊的那些類,window procedure是誰。注意,Pre-Registers Window Classes沒有什麼神秘的,因為Window Classes就是一個struct,而當你想用某個Pre-Registers Window Classes時,無非是傳一個parameter過去,某段程序一判斷,給wc結構賦值,調用AfxRegisterClass( & wc),OK。哈哈,我看到了,用的還是Win32 API的::DefWindowProc()。
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)
{
CREATESTRUCT cs;
cs.lpszClass = lpszClassName;
...
PreCreateWindow(cs); //########pass a cs with lpszClass null
...
}
BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs) //########pass a cs with lpszClass NULL
{
if (cs.lpszClass == NULL) //########pass a cs with lpszClass NULL
{
// make sure the default window class is registered
VERIFY(AfxDeferRegisterClass(AFX_WND_REG));//########pass a para AFX_WND_REG
// no WNDCLASS provided - use child window default
ASSERT(cs.style & WS_CHILD);
cs.lpszClass = _afxWnd;
}
return TRUE;
}
#define AfxDeferRegisterClass(fClass) AfxEndDeferRegisterClass(fClass)
BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)//########pass a para AFX_WND_REG
{
...
// common initialization
WNDCLASS wndcls;
memset( & wndcls, 0, sizeof(WNDCLASS));
wndcls.lpfnWndProc = DefWindowProc; //########## here,Win32 API ::DefWindowProc()
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hCursor = afxData.hcurArrow;
...
if (fToRegister & AFX_WND_REG) //########pass a para AFX_WND_REG
{
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpszClassName = _afxWnd; //########pass a para _afxWnd
AfxRegisterClass( & wndcls);
...
}
...
}
const TCHAR _afxWnd[] = AFX_WND;
#define AFX_WND AFX_WNDCLASS("Wnd")
#define AFX_WNDCLASS(s) \
_T("Afx") _T(s) _T("42") _STATIC_SUFFIX _UNICODE_SUFFIX _DEBUG_SUFFIX
2.4 好,總結一下。
class CWnd : public CCmdTarget
{
...
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
virtual BOOL OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult);
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
...
}
LRESULT CALLBACK
AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
}
LRESULT AFXAPI
AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
WPARAM wParam = 0, LPARAM lParam = 0)
{
LRESULT lResult;
lResult = pWnd->WindowProc(nMsg, wParam, lParam);
return lResult;
}
LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = 0;
if (!OnWndMsg(message, wParam, lParam, & lResult))
lResult = DefWindowProc(message, wParam, lParam);
return lResult;
}
BOOL CWnd::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
if (message == WM_COMMAND)
OnCommand(wParam, lParam);
else if (message == WM_NOTIFY)
OnNotify(wParam, lParam, & lResult);
else
...// msg map related
}
LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
if (m_pfnSuper != NULL)
return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);
}