Jump to content
  • Advertisement
Sign in to follow this  
Gumgo

WndProc as a member function

This topic is 3790 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
Advertisement
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
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!