Thread: Windows message loop variations

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Windows message loop variations

    I'm having some trouble with the windows message loop. The trouble is, when I use GetMessage I have to wait for a windows message for it to return, which means I can't update graphics on the screen in the main message loop unless there is a windows message. Then I have to use WM_TIMER which doesn't go fast enough.

    If I use PeekMessage the program runs fine except it hardly ever gets windows messages. Meaning it doesn't react well at all. If I try and click on the file menu on my prog, it takes like 10 seconds to come up. I have to use ctrl-alt-del to close it. I'll post the two I've tried:
    Code:
    /*while( (bRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
        { 
            if (bRet == -1)
            {
                PostQuitMessage(0);
            }
            else 
            {
                TranslateMessage(&Msg); 
                DispatchMessage(&Msg);
            }
    		MainFunc(hWnd);
        }*/
    Code:
    while(!done)
    	{
    		if(PeekMessage(&Msg, hWnd, 0, 0, PM_REMOVE))
    		{
    			if(Msg.message == WM_QUIT)
    			    done = true;
    
    			TranslateMessage(&Msg);
    		    DispatchMessage(&Msg);
    		}
    		
    		MainFunc(hWnd);
    	}

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Just as with GetMessage, PeekMessage should specify a HWND of NULL. That could very well be the problem there.

    One very important point about GetMessage is that it can return one of three values, positive on success, zero upon completion, and -1 on error. In other words, to avoid an out of control loop, you should use something like:

    Code:
    while(GetMessage(&msg, 0,0,0) > 0)
    {
     //...
    }

    Otherwise, everything looks kosher (though I never use PeekMessage() myself, perhaps someone might spot something else there...).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. Message loop?
    By Shadowwoelf in forum Windows Programming
    Replies: 2
    Last Post: 07-21-2007, 09:58 PM
  3. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 01:09 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. Replies: 1
    Last Post: 01-10-2003, 10:08 PM