Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using WndProc in a class

Started by moucard Apr 27, 2004 at 7:34 AM 1 replies 300+ views
Original Post
moucard
moucard
Hi to everyone. I just created a class CWindow which should handle all the nasty details of creating a window with the win32 api. Looking at a previous post in this forum I found out how to register correctly (or so it would seem) the WndProc needed by the application to proccess messages [url]http://www.gamedev.net/community/forums/topic.asp?topic_id=59171[/url]. I made a static member function inside my class declaration like this (its public):
static LRESULT CALLBACK WndProcStatic(HWND hWnd_, UINT uiMessage_, 
		WPARAM wParam_, LPARAM lParam_)
	{
		CWindow *tmpWindow = (CWindow*)GetWindowLong(hWnd_, GWL_USERDATA);
	
		return tmpWindow->WndProc(hWnd_, uiMessage_, wParam_, lParam_);
	}
 
So when I initialize the WNDCLASS variable I use: m_WindowClass.lpfnWndProc = WndProcStatic; where m_WindowClass is a private member of my class. The program compiles fine and runs up to a point. That point is when I use: m_hWnd = CreateWindowEx(...) at which the WndProcStatic function is called. But the GetWindowLongFunction returns me zero, so I get an exception in WndProc. The hWnd_ variable seems to have a reasonable value ath the time GetWindowLong is called. Does anyone one why?
Endurion
Endurion
Where do you set GWL_USERDATA?

WndProcStatic will be called a few times during CreateWindow. The very first message will be WM_NCREATE. And at that point there can''t be no GWL_USERDATA set to your class, since that''s the very first place where you actually see your new HWND value.

You could pass your class pointer in LPARAM in CreateWindow and in WM_NCCREATE get the class pointer out of the CREATESTRUCT. Then set GWL_USERDATA, and continue WndProcStatic.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
moucard
moucard
Wooohooo. Finally!!!
Man this stuff isn''t easy, is it? I could not understand your post exactly until I saw the help for CreateWindowEx and then I understood what you meant with "pass the class pointer in the lParam of the WM_NCCREATE message". I was ready to ask you to post some code but bang! problem solved. The only change I had to do was to pass that #(*#($* pointer. Thank you very much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.