Issues Wrapping Win32 API - static message handler

Started by
2 comments, last by Endurion 16 years, 11 months ago
Hi everyone. I've been modifing and splicing bits of code I've found together in order to create a wrapped win32 api solution. However, I'm having a bit of troubles. The first symptoms were that escape would close the window and properly terminate the program, but clicking the "x" in the corner would only close the window, leaving the app running in the background (taking up almost 50% CPU usage due to the PeekMessage loop I imagine). After a bit of debugging, it seems there's an issue in my static message handler. It seems that whether we are receiving the WM_NCCREATE message or not, I can never manage to get a pointer to my window, so if(pWnd) ends up being false every time. Thus my custom message handler (non-static) never gets called, and instead DefWindowProc is called every time. Any ideas as to what might be wrong? Here's the code for the static (stWinMsgHandler) and non-static (msgHandler) functions.

LRESULT CALLBACK Win32Frame::msgHandler(HWND hWnd, UINT Msg, 
						  WPARAM wParam, LPARAM lParam)
{
	switch (Msg){
		case WM_CLOSE:		// When the user want's to close the aplication
			//PostMessage(hWnd,WM_DESTROY,0,0); 
			PostQuitMessage(0);		// The user "wants" to quit
			break;
		case WM_DESTROY:	// The main window was destroyed
			PostQuitMessage(0);
			break;
		default:	// Other messages
			return DefWindowProc(hWnd, Msg, wParam, lParam);
	}
	
	return 0;	// Message handled succesful
}

LRESULT CALLBACK Win32Frame::stWinMsgHandler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	Win32Frame* pWnd = NULL;

	if (Msg == WM_NCCREATE)
	{
		// get the pointer to the window from lpCreateParams which was set in CreateWindow
		pWnd = reinterpret_cast<Win32Frame *>((LPCREATESTRUCT(lParam))->lpCreateParams);
		SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<long>(pWnd));
	}
	else
	{
	// get the pointer to the window
		pWnd = reinterpret_cast<Win32Frame *>(GetWindowLong(hWnd, GWL_USERDATA));
	} 

	// if we have the pointer, go to the message handler of the window
	// else, use DefWindowProc
	if (pWnd) // ** Here is the issue - this ALWAYS returns false
		return pWnd->msgHandler(hWnd, Msg, wParam, lParam);
	else
		return DefWindowProc(hWnd, Msg, wParam, lParam); 
}
ßαδ Hαm
Advertisement
My guess is then that you are setting lpCreateParams correctly when you are creating the window.

As a separate nit, your WM_CLOSE handler should call DestroyWindow() rather than PostQuitMessage().
Ah, that did it! Thank you! :)
ßαδ Hαm
Arghh!!!!

Move the return DefWindowProc call outside the switch case.

There is NO general return value if you handled a message. Returning 0 may be ok for a few messages but not by all.

Also you will run intro trouble later if you want to handle a message AND pass it on to DefWindowProc.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement