Sign in to follow this  

WndProc as a member function

This topic is 3574 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've made a window class and I'm trying to get WndProc to be a member function so it can access private members of the window class (specifically, keys, lmb, rmb, and active):
class Window
{
private:
	HGLRC hRC;
	HDC hDC;
	HWND hWnd;
	HINSTANCE hInstance;

	bool keys[256];
	bool lmb, rmb;
	bool active, fullscreen;

	int width;
	int height;
	int numbofbits;
	std::string title;

	static LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); // < this line
	bool InitOpenGL();
	void ResizeScene( int w, int h );

public:
	Window();
	bool MakeWindow( std::string text, int w, int h, int bits, bool full );
	void KillWindow();

	int GetWidth();
	int GetHeight();
	std::string GetTitle();
	void SetTitle( std::string text );

	bool GetKey( BYTE key );
	bool GetLMB();
	bool GetRMB();
	int GetMouseX();
	int GetMouseY();
	void SetMousePosition( int x, int y );

	bool GetActive();
	bool GetFullscreen();
	bool SetFullscreen( bool flag );
};

If I don't make it static, I get this error:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'WNDPROC'
        None of the functions with this name in scope match the target type
in this line when I'm setting up the window (in the MakeWindow function): wc.lpfnWndProc = (WNDPROC)WndProc; If I do make it static, I get errors like this:
error C2597: illegal reference to non-static member 'Window::active'
Is it possible to make WndProc a member of the window class and have be able to access other members of the class?

Share this post


Link to post
Share on other sites
WndProc must be a non-member or static function, so it cannot have this. It's a sane behaviour, since you only pass a pointer to a function and not to the object.
You could try something like this:


static LRESULT CALLBACK wndproc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
Window *window;

if (umsg == WM_CREATE) {

window = (Window *) ((LPCREATESTRUCT) lparam)->lpCreateParams;

SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) window);
}
else {

window = (Window *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
}

if (window != NULL) {

// do something here...
}

return DefWindowProc(hwnd, umsg, wparam, lparam);
}


Share this post


Link to post
Share on other sites

This topic is 3574 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this