Skip to main content
GameDev.net gamedev.net
🔒 Locked

encapsulating wndproc

Started by ECKILLER Jan 13, 2001 at 6:23 PM 3 replies 2.1k views
Original Post
ECKILLER
ECKILLER
Anyone know how to put the msg proc function in a class? If i make it static then it doesnt know which window object the msg proc belongs to. ECKILLER
ECKILLER
Shukaido
Shukaido
Create a class for a window, and in its constructor, or wherever you call CreateWindow(), use SetWindowLong() and GWL_USERDATA to store the "this" pointer for this object. Then, keep your WndProc a global function. The first thing your WndProc will do is get the "this" ptr for the hWnd it is given with GetWindowLong(). Then call a member function of that object. In other words, don''t do your message processing from WndProc - just use WndProc to hand off to the method in the appropriate object. You could try something like this:

class CWindow {

public:
CWindow(void) {

/* Register the WNDCLASS here */

m_hWnd = CreateWindow( /* Whatever you want in here */)

SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this);
}

virtual LRESULT wndProc(UINT uiMsg, WPARAM wParam, LPARAM lParam) {

switch(uiMsg) {

/* Message handling goes here */
}
}

protected:
HWND m_hWnd;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
CWindow * pWindow;

pWindow = (CWindow *)GetWindowLong(hWnd, GWL_USERDATA);

return pWindow->wndProc(uiMsg, wParam, lParam);
}
Shukaido
Shukaido
Oops, sorry for the double post...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.