Archived

This topic is now archived and is closed to further replies.

Windows message for closing application

This topic is 5010 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, Does anyone know what the windows message is sent when the "x" in the top right of a windowed application is clicked? For some reason, when I click this, my applicaton still runs, although the window itself is closed.

Share this post


Link to post
Share on other sites
I suspect it''s WM_DESTROY that you want to look at. You indicated that the application still runs although the window is closed. The WM_DESTROY message is sent to the window procedure of the window being destroyed after the window is removed from the screen.

The WM_QUIT message indicates a request to terminate an application and is generated when the application calls the PostQuitMessage function.

The WM_CLOSE message is sent as a signal that a window or an application should terminate.

An application (or window) receives all of these messages through its WindowProc function.

I would try intercepting the WM_DESTORY message and making sure you clean up all your resources so that the application can quit. Or... you might just FORCE the application to quit.

Hope this helps!

Share this post


Link to post
Share on other sites
Also make sure that you don''t have a thread left running somewhere. If your app is multi-threaded, you might have a thread hung somewhere that keeps it going as a process even if the window is destroyed...

Rattlehead

Share this post


Link to post
Share on other sites
Clicking the "X" generates the WM_CLOSE message. The default message handling for WM_CLOSE is to call DestroyWindow(), which posts the WM_DESTROY message.

If you want the app to exit when the "X" is clicked add a message handler for WM_DESTROY and call PostQuitMessage() from within it. This will then generate the WM_QUIT message and cause GetMessage() to return.

[corrected spelling]

[edited by - Solo on March 26, 2004 4:04:29 PM]

Share this post


Link to post
Share on other sites
I''m pretty sure the X in the corner of a window generates a WM_SYSCOMMAND message with a wParam of SC_CLOSE. It''s very possible that, whether or not the WM_SYSCOMMAND was handled, other messages such as WM_CLOSE or WM_DESTROY are also sent.

Share this post


Link to post
Share on other sites
Well, when I close using "x", SC_CLOSE of WM_SYSCOMMAND or WM_CLOSE or WM_DESTROY are not called. As far as I know, I only have one thread running. When I use the ESC key, it closes fine, as ESC is set up in my WindowProc message handling function to PostQuitMessage( 0 ); I set up SC_CLOSE, WM_DESTROY and WM_CLOSE messages to do the same, but they did not when I used the "x".

Anyone have any suggestions?

Here is my main loop:


while( true ) {
Sleep( 20 );
PeekMessage( &msg, hWnd, NULL, NULL, PM_REMOVE );

if( msg.message == WM_QUIT ){
break;
}
else if( msg.message == WM_SYSCOMMAND ) {
switch( msg.wParam ) {
case SC_MINIMIZE:
engineManager->Pause( true );
break;
case SC_MAXIMIZE:
engineManager->Pause( false );
break;
case SC_CLOSE:
engineManager->Log( "Window closed" );
PostQuitMessage( 0 );
break;
}
}
else if( msg.message == WM_CLOSE ) {
engineManager->Log( "Close application" );
PostQuitMessage( 0 );
}
else {
engineManager->MainLoop();

TranslateMessage( &msg );
DispatchMessage( &msg );
}
}

engineManager->Log( "Main loop ended, destroying EngineManager" );
delete engineManager;

UnregisterClass( "2DGAMEPROJ", hInstance );
return( msg.wParam );


[edited by - red_sodium on March 26, 2004 7:50:24 PM]

Share this post


Link to post
Share on other sites
I also have a windows procedure, but this is what a lot of tutorials do, so I stuck with it. I don''t think this is the cause of my problem though, neither place produces the messages.

Share this post


Link to post
Share on other sites
quote:
Original post by red_sodium
...this is what a lot of tutorials do...
Another reason why tutorials are a bad idea. It is better to localize your message handling in the window procedure (which is in a parallel thread) rather than right in your main loop. The only "message handling" you should do in the main loop is the minimum necessary to pump messages.

quote:
I don''t think this is the cause of my problem though, neither place produces the messages.
Time to crack out the debugger.

Share this post


Link to post
Share on other sites
Thanks for that, Oluseyi. I must admit I know very little about my debugger that comes with Borland C++ Builder (v3.0). Do you know any good articles for this compiler or for debugging theory in general, as I'm a little rusty and it's hard to find good material on this subject without shelling out £50 on a 500 page hardback.

Thanks

PS: Would that really solve my problem? I'll give it a try anyway...

[edited by - red_sodium on March 26, 2004 8:26:59 PM]

Share this post


Link to post
Share on other sites

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch(iMsg)
{
case WM_DESTROY:
PostQuitMessage(0) ;

return 0 ;
}

return DefWindowProc(hwnd, iMsg, wParam, lParam) ;
}


tehre u go

Share this post


Link to post
Share on other sites