think window procedure
2010-06-15 20:40:13 来源:WEB开发网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)
{
...
}
更多精彩
赞助商链接