encapsulating wndproc
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
January 13, 2001 06:32 PM
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
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
January 13, 2001 06:52 PM
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);
}
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);
}
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);
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement