think window procedure

Lucky启航

Lucky启航

2016-01-29 11:55

think window procedure,think window procedure

think window procedure
作者:温昱

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/cyuyanjiaocheng/)

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 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      
展开更多 50%)
分享

猜你喜欢

think window procedure

C语言教程 C语言函数
think window procedure

think in recent things

Linux Linux命令 Linux安装 Linux编程 Linux桌面 Linux软件 Linux内核 Linux管理
think in recent things

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

librarycachepin与PROCEDURE的重建

电脑网络
librarycachepin与PROCEDURE的重建

建立Floating Window(Top Most的window)

编程语言 网络编程
建立Floating Window(Top Most的window)

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

"Think For Yourself "创意广告欣赏

平面设计 海报设计 广告设计 画报设计 签名设计 服装设计 名片设计 画册设计 版式设计 商标设计

General Window

编程语言 网络编程
General Window

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

三维建模教程--工艺壸的制作

三维建模教程--工艺壸的制作

HTML之字体标记

HTML之字体标记
下拉加载更多内容 ↓