Previously, the principle of mfc was to map the class pointer to the window handle;
After several versions are implemented, I always think that it is a waste of elegance to search for ing tables, especially when there are many windows, for example, a large number of subclass win32 controls are often used;
Therefore, the window USERDATA has the following implementation versions:
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 createmediawex (dwExStyle,
LpszClass,
LpszName,
DwStyle,
X, y,
NWidth, nHeight,
HParent,
HMenu,
HInst,
& Mdic );
}
Private:
HWND m_hWnd;
};
In this way, message ing is so easy. You can add ing in the static window or ing in the member window;