think window procedure,think window procedure
【 tulaoshi.com - C语言心得技巧 】
think window procedure
作者:温昱
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的事。
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 messagesvirtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);virtual BOOL OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult);...};///template methodLRESULT 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 methodLRESULT 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 registeredVERIFY(AfxDeferRegisterClass(AFX_WND_REG));//########pass a para AFX_WND_REG// no WNDCLASS provided - use child window defaultASSERT(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 initializationWNDCLASS 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.st
来源:http://www.tulaoshi.com/n/20160129/1483896.html
看过《think window procedure》的人还看了以下文章 更多>>