Closing the Window

When the user closes a window, that action triggers a sequence of window letters.

The user can shut an awarding window by clicking the Close button, or by using a keyboard shortcut such equally ALT+F4. Any of these deportment causes the window to receive a WM_CLOSE message. The WM_CLOSE message gives you an opportunity to prompt the user before closing the window. If you actually practice desire to close the window, call the DestroyWindow function. Otherwise, simply render zero from the WM_CLOSE bulletin, and the operating organisation will ignore the message and non destroy the window.

Here is an example of how a programme might handle WM_CLOSE.

              case WM_CLOSE:     if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK)     {         DestroyWindow(hwnd);     }     // Else: User canceled. Do nil.     return 0;                          

In this example, the MessageBox role shows a modal dialog that contains OK and Cancel buttons. If the user clicks OK, the programme calls DestroyWindow. Otherwise, if the user clicks Cancel, the phone call to DestroyWindow is skipped, and the window remains open up. In either case, return zero to signal that you lot handled the message.

If you want to close the window without prompting the user, you could merely phone call DestroyWindow without the call to MessageBox. All the same, at that place is a shortcut in this example. Call back that DefWindowProc executes the default action for any window message. In the case of WM_CLOSE, DefWindowProc automatically calls DestroyWindow. That ways if you ignore the WM_CLOSE message in your switch statement, the window is destroyed by default.

When a window is about to be destroyed, it receives a WM_DESTROY message. This message is sent after the window is removed from the screen, only before the devastation occurs (in particular, before any child windows are destroyed).

In your main application window, you will typically respond to WM_DESTROY by calling PostQuitMessage.

              instance WM_DESTROY:     PostQuitMessage(0);     render 0;                          

We saw in the Window Messages section that PostQuitMessage puts a WM_QUIT message on the message queue, causing the bulletin loop to end.

Here is a menses chart showing the typical fashion to process WM_CLOSE and WM_DESTROY messages:

flow chart showing how to handle wm-close and wm-destroy messages

Next

Managing Application Country