Object oriented c ++ win32?

I want to create my own class for window processing and window procedure, but I noticed that the window procedure must be static! Now I wonder, is it possible to make the object orientation of a window procedure? I read several guides on object-oriented windows, but they always make the procedure static -.- what is its point?: /

Any links or information on how to get around this issue will be appreciated.

thanks

+7
c ++ winapi
source share
5 answers

You can get around this by making the WndProc static delegate to all members:

// Forward declarations class MyWindowClass; LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) std::map<HWND, MyWindowClass *> windowMap; // Your class class MyWindowClass { private: HWND m_handle; // The member WndProc LRESULT MyWndProc(UINT message, WPARAM wParam, LPARAM lParam) { /* ... */ } public: MyWindowClass() { /* TODO: Create the window here and assign its handle to m_handle */ /* Pass &WndProc as the pointer to the Window procedure */ // Register the window windowMap[m_handle] = this; } }; // The delegating WndProc LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { std::map<HWND, MyWindowClass *>::iterator it = windowMap.find(hWnd); if (it != windowMap.end()) return it->second->MyWndProc(message, wParam, lParam); return 0; } 
+11
source share

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(...) // add whatever parameters you want { HWND hwnd = CreateWindow("Your Window Class Name", "Your Window title", dwStyle, x, y, width, height, NULL, hMenu, g_hInstance, (LPARAM)this); if (hwnd == NULL) return false; ASSERT(m_hwnd == hwnd); return true; } static LRESULT __stdcall StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { CYourWindowClass* pWindow = (CYourWindowClass*)GetWindowLongPtr(hwnd, GWLP_USERDATA); if (uMsg == WM_CREATE) { pWindow = ((CREATESTRUCT*)lParam)->lpCreateParams; SetWindowLongPtr(hwnd, GWLP_USERDATA, (void*)pWindow); m_hWnd = hwnd; } if (pWindow != NULL) { return pWindow->WndProc(uMsg, wParam, lParam); } return DefWindowProc(hwnd, uMsg, wParam, lParam); }; }; 
+6
source share

If you are looking for an object-oriented Win32 API, then you should look at MFC and / or WTL .

+3
source share

Just add the answer to Brian, but for a win32 newbie friendly framework, look at Win32 ++ . The library itself is not so comprehensive compared to MFC or QT, but it is a compromise that the developer made at the beginning to make the library understandable and easy to use.

If you are still interested in this topic, I highly recommend that you take a look at it, since it uses another way to store the 'this' pointer using local thread storage.

+2
source share

You can use the window handler passed to WindowProc to capture the object that you created for that particular window and delegate event processing to that object.

eg.

 IMyWindowInterface* pWnd = getMyWindowObject(hWnd); pWnd->ProcessMessage(uMsg, wParam, lParam); 
+1
source share

All Articles