Sign in to follow this  

Issues Wrapping Win32 API - static message handler

This topic is 3873 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

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); 
}

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3873 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