Jump to content
  • Advertisement
Sign in to follow this  
Tera_Dragon

WinProc() as a memeber function

This topic is 4961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have made a class to handle the general things for a Win32 window, but I am having problems with making the WinProc() a method of the class. The only error I get if for this line of code: wndclass.lpfnWndProc = WinProc; The error is: error C2440: '=' : cannot convert from 'long (__stdcall OGLWindow::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned i Is it possible to make the WinProc() a method or not?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
error C2440: '=' : cannot convert from 'long (__stdcall OGLWindow::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned i

All member functions have an implicit first parameter, the 'this' pointer, because member functions act on objects. What you're doing here is giving the wndclass a pointer to a member function, which won't cut it. It needs a 'vanilla' function. This is possible by making the member function static, in which case there's no implicit 'this' pointer, but you can't access non-static class data from it either. i.e.

class OGLWindow
{
public:
...
static LRESULT window_procedure(HWND window, UINT message, WPARAM wparam, LPARAM lparam);
};

Share this post


Link to post
Share on other sites
One way I saw in a book to do this was to have a global WndProc, and a WndProc local to the class and public. After creating the window, set it's user data like so:


SetWindowLong( m_hWnd, GWL_USERDATA, (long) this );




This will set the window's "user data" to a pointer to the viewport class, from there you can call the WndProc local to the class and handle it there like this:


LRESULT MsgProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
CViewport *pViewport = (CViewport *) GetWindowLong( hWnd, GWL_USERDATA );
return pViewport->MsgProc( hWnd, message, wParam, lParam );
}




When registering the window, use ::WndProc, not WndProc, it'll confuse the one you want with the one in the class and give you the same error.

Share this post


Link to post
Share on other sites
the last parameter of CreateWindow can be used aswell:


// Create a window.
m_hwnd = CreateWindowEx(
0, // ExStyle
window_name, // Window class name
window_name, // Title
windowstyle, // Style
xpos, // X coordinate of window
ypos, // Y coordinate of window
windowrect.right + abs(windowrect.left), // Window Width
windowrect.bottom + abs(windowrect.top), // Window Height
0, // Parent window
0, // Menu
GetModuleHandle(NULL), // Instance handle
(void*)this); // Additional arguments




LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static window_manager_t* window_manager = NULL;

switch(msg)
{
case WM_CREATE:
window_manager = (window_manager_t*)((LPCREATESTRUCT)lParam)->lpCreateParams;
break;

default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!