非静态成员函数做窗口过程(非映射表实现),成员函数做消息映射
之前看mfc的原理是做类指针对窗口句柄的映射表;
实现了几个版本,总觉得查找映射表是件很浪费不优雅的事情,尤其在窗口很多的时候,比如大量使用了子类化的win32控件这种常出现的情况;
于是,利用窗口的USERDATA,有如下版本的实现,大概如下:
class XWindow
{
protected:
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
XWindow* pWindow;
if (WM_NCCREATE == uMsg)
{
MDICREATESTRUCT* pMDIC = (MDICREATESTRUCT*)((LPCREATESTRUCT)lParam)->lpCreateParams;
pWindow = (XWindow*)(pMDIC->lParam);
::SetWindowLong(hWnd, GWL_USERDATA, (LONG) pWindow);
}
else
{
pWindow=(XWindow*)::GetWindowLong(hWnd, GWL_USERDATA);
}
if (NULL != pWindow)
{
return pWindow->WndProc(hWnd, uMsg, wParam, lParam);
}
else
{
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
virtual LRESULT WndProcSelf(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//this is self wndproc
}
virtual HWND CreateEx(DWORD dwExStyle,
LPCTSTR lpszClass,
LPCTSTR lpszName,
DWORD dwStyle,
int x, int y,
int nWidth, int nHeight,
HWND hParent,
HMENU hMenu,
HINSTANCE hInst)
{
MDICREATESTRUCT mdic;
memset(& mdic, 0, sizeof(mdic));
mdic.lParam = (LPARAM) this;
return CreateWindowEx(dwExStyle,
lpszClass,
lpszName,
dwStyle,
x, y,
nWidth, nHeight,
hParent,
hMenu,
hInst,
& mdic);
}
private:
HWND m_hWnd;
};
这样,想做消息映射就so easy了,可以在静态窗口过程中加映射或者在成员窗口过程中加映射;
补充:软件开发 , C++ ,