A general technique for allowing a window instance to be represented as an instance of a class is to use SetWindowLongPtr and GetWindowLongPtr to bind a class instance pointer to a window handle. Below is a sample code to get started. It cannot compile without a few tweaks. This meant only a link.
Personally, I stopped rolling back my own window classes a few years ago when I discovered the ATL template class CWindow and CWindowImpl. They will take care of doing all this mundane coding for you, so you can only focus on writing methods that process window messages. See the sample code I wrote here .
Hope this helps.
class CYourWindowClass { private: HWND m_hwnd; public: LRESULT WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: return OnCreate(wParam, lParam); case wM_PAINT: return OnPaint(wParam, lParam); case WM_DESTROY: { SetWindowLongPtr(m_hwnd, GWLP_USERDATA, NULL); m_hwnd = NULL; return 0; } } return DefWindowProc(m_hwnd, uMsg, wParam, lParam); } CYourWindowClass() { m_hwnd = NULL; } ~CYourWindowClass() { ASSERT(m_hwnd == NULL && "You forgot to destroy your window!"); if (m_hwnd) { SetWindowLong(m_hwnd, GWLP_USERDATA, 0); } } bool Create(...)
selbie
source share