G
G
Governor2018-07-07 21:12:30
C++ / C#
Governor, 2018-07-07 21:12:30

WinAPI :: What happens when you close a program in Windows?

Welcome all!
I am studying WinAPI, the moment of closing the program became interesting, that is, the release of a message WM_DESTROY. I can not understand the next point, I will try to explain:
When I click on the cross in the upper area of ​​\u200b\u200bthe window (or Alt + F4), Windows, my program sends a message WM_DESTROYwhile itself (Windows) hides my window.
I thought, what if by rejecting the message WM_DESTROY, do not exit the program and display the window again.
When processing a message, the following code stands:

case WM_DESTROY :
  if (MessageBox(hwnd, "Пришла просьба закрыться!","!!!", MB_OKCANCEL) == IDOK )
  {
    PostQuitMessage(0) ;
  }
  else
  {
    ShowWindow(hwnd, SW_NORMAL);
  }			   
return 0 ;

When I click "Cancel", the window is not displayed and the program process is not closed!
Can anyone explain what exactly happens when the program is closed?
And why does Windows hide my client area when closing, ahead of time, while WinMain is still running?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2018-07-08
@Mr-Governor

Use the WM_CLOSE event handling to prevent the window from closing.

R
rPman, 2018-07-07
@rPman

wm_destroy is just a request to the application to close the window (not the application)
according to the unwritten ui rules, in windows, after closing the last application window (there may be several), the application itself must be closed.
In your case, when canceling, it is necessary to return in return it seems true (look at the documentation) by this you will tell the calling function that the process has changed its mind about closing the window and will not delete it. I vaguely remember that you had to call some other method before return - the window's event handler by default, just when canceling the closing it will not need to be called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question