分类: C/C++
2008-08-05 13:55:12
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的事。
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_SUFFIX2.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); }(完) 下载本文示例代码