Sign in to follow this  
AtsusaKai

How to cancel WM_CLOSE message?

Recommended Posts

From the docs:
Quote:

If an application processes this message, it should return zero.

In other words, return zero in your message handling callback.

Share this post


Link to post
Share on other sites
Quote:
Original post by eq
From the docs:
Quote:

If an application processes this message, it should return zero.

In other words, return zero in your message handling callback.


Hold a sec, I'm a little confused. Okay, here's the situation.. Say I have created a window, then I click the close button of the window, true enough, the window will send a message which is I think WM_CLOSE..and will pass through a MsgProc... now, what should I do or write to cancel the closing.

Did you mean that I should return zero so it will be cancelled?

[source language=cpp]

LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if ( uMsg == WM_CLOSE )
{
if ( cancelClose )
{
// code to cancel.. how?
}
}

return 0;
}




Thanks for the time.

Share this post


Link to post
Share on other sites
Again, read the docs:

Quote:

An application can prompt the user for confirmation, prior to destroying a window, by processing the WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice.

By default, the DefWindowProc function calls the DestroyWindow function to destroy the window.

If you don't return 0; in your WM_CLOSE handler and call DefWindowProc instead, it will call DestroyWindow(). So, by returning 0 in your handler, you can bypass the default handler and choose whether or not to destroy the window yourself:


case WM_CLOSE:
// ...
if (!cancelClose)
{
DestroyWindow(hWnd)
}
return 0;




Edit: I noticed that you don't return DefWindowProc. This is how a Message Proc typically looks:


LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch (uMsg)
{
case WM_CREATE:
break;
// etc...
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this