Page 1 of 1

MainLoop and events

Posted: Wed Jun 28, 2006 11:09 am
by smetana
I created MyApp class (like in tutorial) and virtual int MainLoop(). But now events handling is not working. What should I change? I think I must use ProcessMessage, Pending and Dispatch, but I don't know how.

Code: Select all

int MyApp::MainLoop()
{
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
	if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
	        TranslateMessage( &msg );
                DispatchMessage( &msg );
        }
	else
	{
		framework->Update();
	}
    }

    return static_cast<int>(msg.wParam);
}

Posted: Wed Sep 27, 2006 9:18 pm
by tdctaz
I have the same problem, I tried something like the following, but it didnt work.

Code: Select all

while( wxTheApp->Pending() )
{
    if( !wxTheApp->Dispatch() )
        return false;
}
Hoping someone knows what to do - thanks.

Posted: Thu Sep 28, 2006 7:16 pm
by tdctaz
ok, I fanaly got it to work - I just created my own message pump on my wxApp class. Im not really sure if this is the correct way to do it but it works well in my application.

Destructor:

Code: Select all

myWxApp::~myWxApp()
{
    delete m_mainLoop; m_mainLoop = NULL;
}
Constructor:

Code: Select all

bool myWxApp::OnInit()
{
    m_mainLoop = new wxEventLoop;
    return true;
}
And then my message pump that are called from my own main loop:

Code: Select all

bool CGgeWxApp::messagePump()
{
    MSG msg;
    while( ::PeekMessage( &msg, (HWND)0, 0, 0, PM_REMOVE ) )
    {
        // On quit messages just quit.
        if( msg.message == WM_QUIT )
            return false;

        if( !m_mainLoop->PreProcessMessage( &msg ) )
        {
            // wx did not handel it then windows should...
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
    }
    return true;
}