Your basic idea that you showed in this example is correct.
You create a static function WndProc and a mapping that maps HWND to your classes.
When you create a new instance of the widget, you add it to the mapping. When destroyed, you delete it from the mapping.
In your WndProc function, you take an instance of your class from the mapping and call the event handler function of this instance:
class WidgetBase { public: WidgetBase() { _handle = CreateWindow(, &WidgetBase::MainProc, ); _widgets.insert(std::make_pair(handle, this); } virtual ~WidgetsBase() { _widgets.remove(handle); } protected: HWND _handle; virtual LRESULT handleEvents(UINT msg,WPARAM wParam,LPARAM lParam) { return DefWindowProc(_handle, hWnd,msg,wParam,lParam); } private: static std::map<HWND, WidgetBase*> _widgets; static WidgetBase* GetWidgetPointerFromHWND(HWND handle) {
Then in your derived class you only need to override the handleEvents function:
class Derived: public WidgetBase { protected: virtual LRESULT handleEvents(UINT msg,WPARAM wParam,LPARAM lParam) { // This is your event handler, that is memeber function //... return WidgetBase::handleEvents(msg, wParam, lParam); } };
source share