I'll suggest you to use SetProp and GetProp functions from Windows API.
Once the window is created, you can set the pointer to your class as the property of the window using SetProp.
Then in window procedure you can get the pointer to your class from HWND with GetProp function.
Also the property should be removed in WM_DESTROY or WM_NCDESTROY with RemoveProp.
Then you can access the instance of your class from window procedure:Code:class MyWindow { public: // Attributes int num_squares; // Functions MyWindow() { num_squares = 0; // Other stuff like registering the window class // Creating a window. HWND hWnd = CreateWindow(...); SetProp(hWnd, "MyWindow", (HANDLE)this); }; static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param); };
For second parameter of SetProp/GetProp/RemoveProp to be unique GlobalAddAtom function can be used or some GUID string.Code:LRESULT CALLBACK MyWindow::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) { switch(msg) { case WM_LBUTTONDOWN: { MyWindow* pMyWindow = (MyWindow*)GetProp(hwnd, "MyWindow"); if(NULL != pMyWindow) { // Increment num_squares. ++pMyWindow->num_squares; } break; } case WM_DESTROY: { RemoveProp(hwnd, "MyWindow"); break; } } return DefWindowProc(hwnd, msg, w_param, l_param); }




Reply With Quote