Главная страница | назад





Article #18820: Detecting Windows Shutdown Mode

 Question and Answer Database

FAQ3820C.txt   Detecting Windows Shutdown Mode
Category   :C/C++ Language Issues
Platform    :Win95/NT
Product    :   C++Builder1.0   C++Builder3.x

Question:
How can I detect and prevent a windows shutdown in my application? 

Answer:
It is possible for you to detect when windows is being shutdown and
to prevent this shutdown from within a running C++ Builder application.

The simple solution is to add an event handler to the main form's
OnCloseQuery event. This event handler occurs as a result of a
WM_QUERYENDSESSION message which is sent to all running application
in Windows when Windows is about to be shut down.  The boolean
CanClose parameter to this event can be set to True to allow Windows
to shut down or, CanClose can be set to False to prevent Windows
from shutting down.

The code below illustrates using this event.

----- Code Follows -------

void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{  
  // Asks user if shutdown should occur.
  if (MessageDlg("Are you sure?", mtConfirmation, TMsgDlgButtons() << mbOK << mbCancel,0) == mrOk)
    CanClose = true;	// Allows Windows to be shut down
  else
   CanClose = false;	// Prevent Windows from shutting down
}

12/30/99
 

Last Modified: 01-SEP-99