Home > Software design >  What message is sent when user is trying to close window?
What message is sent when user is trying to close window?

Time:08-19

What message do i need to listen into my window procedure to close the GUI when right-clicking into her taskbar button and clicking on X Close window?

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch (msg)
    {
    case ???:
    {
      //...
    }
    break;
    }

Note: Im not referring to the X into the caption/title bar.

CodePudding user response:

It's the same message you get when the user presses the [X] button in the caption bar, or chooses Close from the system menu: WM_CLOSE.

The documentation contains useful information. In particular:

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.

CodePudding user response:

The first message is WM_CLOSE. If you handle that message, you have a chance to cancel closing the window.

If the WM_CLOSE handler decided to destroy the window in response (the DefWindowProc function does that), more messages are coming: WM_DESTROY, and finally WM_NCDESTROY. Unlike WM_CLOSE, it’s impossible to cancel closing the window in these handlers, too late.

  • Related