Problem with WindProc and PostMessage()

ok, i have this piece of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void main(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int iCmdShow){


            ................................

if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
		{
			
			// handle or dispatch messages
			if ( msg.message == WM_QUIT ) 
			{
				quit = TRUE;
			}
			else if (msg.lParam == WM_USER)
				glColor3f(0.0f,0.0f,1.0f);//MessageBox(NULL,"Malaka1","ERROR",MB_OK|MB_ICONSTOP);//refresh(LOWORD(msg.lParam), HIWORD(msg.lParam));
			else if (msg.message == WM_SIZE)
				MessageBox(NULL,"Malaka2","ERROR",MB_OK|MB_ICONSTOP);
			else 
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			
		} 
		else 
                    .............
}
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	
	switch (message)
	{
          ...........
          case WM_SIZE:								// Resize The OpenGL Window
		{
			//MessageBox(NULL,"Malaka","ERROR",MB_OK|MB_ICONSTOP);
                        //PostMessage(hWnd,WM_SIZE,0,0);
			PostMessage(hWnd,WM_USER,0,0);
			return 0;								// Jump Back
		}


now, you see what i'm doing.
every time the window change size(case WM_SIZE) i send a message(WM_USER) that supposed to be handled by the main function.
Now, the very first time the program runs, it does catches it but only the first time.
If i uncomment the line 38 it displays both messages normally
If i comment line 38 and line 40 and uncomment line 39, it enters in an endless loop and crashes of course

what i have to do to catches the message WM_USER every time without displaying the 2 messages.

why without the command in line 38, PostMessage in line 40 seems like dead.Is there anything to do with the MessageBox command in line 38 that invokes and the PostMessage command?
Last edited on
If you're not calling DefWindowProc for every message, you can expect your program to behave weirdly / unpredictably.

By side-stepping the message pump, you're sidestepping the way Windows wants and expects to handle these messages.

The first thing you should do is put ALL message handling code in your WndProc. Don't split it up between the WndProc and the message pump -- do it all from the WndProc. There's pretty much no reason to ever check msg.message like you're doing.

Your pump should look like this:

1
2
3
4
5
6
7
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
{
	TranslateMessage( &msg );
	DispatchMessage( &msg );
} 
else
     ...


That's it. Put all that other stuff in WndProc. Also make sure your WndProc is calling DefWindowProc for all messages (unless you specifically don't want normal behavior to occur for a specific message)

After you fix that, see if the problem disappears. If not, repost your fixed code and I'll have another look.


EDIT:

also you probably meant to check msg.message == WM_USER on line 15 and not msg.lParam. But do the above fix anyway.
Last edited on
Ok problem solved.

@Disch I know the best way i the way you're describing but the think was:
i made a class that creates a window.For this i was needed a function to pass its pointer to the wc.lpfnWndProc parameter in WNDCLASS struct. Now in the wndproc function i had to do things inside the class. For that, or i was gonna pass the pointer of the class in the WndProc make a pointer to the class global. I don't want to do either of these. The other option was to make WndProc member of my class. But then the compiler complaining about converting an over-loaded function to WNDPROC type. So i made WindProc, which is member of the class now, static.
But now this function is shared between all hte copies of the class. Not that i'm gonna make a lot of copies. But anyway.

Thanks for the help.much appreciated
Topic archived. No new replies allowed.