Advertisement

encapsulating wndproc

Started by January 13, 2001 06:23 PM
3 comments, last by ECKILLER 24 years ago
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
You can''t
The "best" way is to make it a static function , but that''s clearly not what you want ..
however you can go the other way ,
you create a Controller class, pass it to windows, then figure out with object windows returned to you (and if it''s a derived class of Controller) and go from there
for more info check the Win32 Tutorial at: www.relisoft.com
(very nice OOP-based wrapper of Win32 w/o using MFC there )

-vat
machinshin@onebox.com
Advertisement
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);
}
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);
}
Oops, sorry for the double post...

This topic is closed to new replies.

Advertisement