MainLoop and events

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
smetana
In need of some credit
In need of some credit
Posts: 1
Joined: Wed Jun 28, 2006 10:59 am

MainLoop and events

Post 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);
}
tdctaz
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Sep 27, 2006 9:13 pm
Location: Copenhagen, Denmark

Post 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.
tdctaz
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Sep 27, 2006 9:13 pm
Location: Copenhagen, Denmark

Post 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;
}
Post Reply