{"id":36,"date":"2007-11-17T11:11:14","date_gmt":"2007-11-17T13:11:14","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=36"},"modified":"2007-11-17T11:13:10","modified_gmt":"2007-11-17T13:13:10","slug":"delegators-2-c-win32","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=36","title":{"rendered":"Delegators #2 &#8211; C++ &#038; Win32"},"content":{"rendered":"<p>Last post I was talking about using SetProp\/GetProp to link between an HWND and an instance to a class that encapsulates the window widget. There&#8217;s another way to do it.<\/p>\n<p>The first time you are taught how to create a window in Win32, they tell you that you have to fill in all fields in the WNDCLASS structure, but they always keep on saying to ignore some of the advanced fields. One of the fields inside that structure is cbWndExtra, this one says to the windows manager how many bytes to allocate\u00a0after the original windows manager&#8217;s structure. So suppose all we need is to save the &#8216;this&#8217; pointer, it means we will need\u00a04 bytes, in 32bits environment system, of course. So we can set cbWndExtra to 4, and then we have a DWORD we can access to as much as we like with the SetWindowLong\/GetWindowLong API.<\/p>\n<p>Something like this would suffice:<\/p>\n<p>WNDCLASS wc;<br \/>\nwc. fields = &#8230;<br \/>\nwc.lpszClassName = &#8220;myclass&#8221;;<br \/>\nwc.cbWndExtra = 4;<br \/>\nRegisterClass(&amp;wc);<br \/>\nHWND hWnd = CreateWindow(&#8220;myclass&#8221;, &#8230;)<br \/>\nSetWindowLong(hWnd, 0, this);<br \/>\nShowWindow(&#8230;);<br \/>\n&#8230;<br \/>\n..<\/p>\n<p>And later on in the window-procedure, you can do this:<\/p>\n<p>LRESULT WINAPI MyWindow::WndProc(HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam)<br \/>\n{<br \/>\n\u00a0MyWindow* pThis = (MyWindow*)GetWindowLong(hWnd, 0);<br \/>\n\u00a0if (pThis != 0) return pThis-&gt;WndProc(message, wparam, lparam);<br \/>\n\u00a0return DefWindowProc(hWnd, message, wparam, lparam);<br \/>\n}<\/p>\n<p>\u00a0And there you go. There are a few problems with this technique though. You can&#8217;t handle the WM_CREATE message inside the instance&#8217;s WndProc, that&#8217;s because <em>before<\/em> CreateWindow returns, the global WndProc will get called with this message, and you didn&#8217;t have the chance to set the &#8216;this&#8217; pointer&#8230;<\/p>\n<p>The second problem is that this technique is good only for windows you create yourself, because you control the WNDCLASS&#8230; You can use pre-created window-classes of the system by changing their fields in your application only, I think this will create a copy of the pre-created classes only for your process.<\/p>\n<p>Anyhow, to solve the first problem, I saw implementation that create the instance of the object when they receive the WM_CREATE inside the global WndProc. This is really a matter of design, but this way you can call an initializer of the instance once WM_CREATE was received. I can&#8217;t come up with another way at the moment if the instance is already created. Maybe just call the initializer on the first message the window gets? Well it&#8217;s a bit cracky.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last post I was talking about using SetProp\/GetProp to link between an HWND and an instance to a class that encapsulates the window widget. There&#8217;s another way to do it. The first time you are taught how to create a window in Win32, they tell you that you have to fill in all fields in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[20,7],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-A","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}