I would like to know what is the best way of asking the user for a YES/NO
dialog when closing a program by clicking with the mouse on the X but this
should not be required if windows is shutting down or doing a restart.
An example would be greatly appreciated.
Many thanks,
Sinead.
>I would like to know what is the best way of asking the user for a YES/NO
>dialog when closing a program by clicking with the mouse on the X but this
>should not be required if windows is shutting down or doing a restart.
With a global bool.
static BOOL GlobalEndSession=FALSE;
Then intercept WM_ENDSESSION
case WM_ENDSESSION:
GlobalEndSession=TRUE;
Then in your WM_CLOSE
case WM_CLOSE:
if( ! GlobalEndSession )
{ if( IDYES==MessageBox(whatever) )
DestroyWindow(
> I would like to know what is the best way of asking the user for a
> YES/NO dialog when closing a program by clicking with the mouse
> on the X
Use the form's OnCloseQuery event for that. Set the CanClose parameter
accordingly.
> but this should not be required if windows is shutting down or doing a
restart.
For that, you need to intercept the WM_QUERYENDSESSION message as well. The
best place to do that is to override the form's WndProc() method, ie:
bool ShuttingDown = false;
void __fastcall TForm1::WndProc(TMessage &Message)
{
if( Message.Msg == WM_QUERYENDSESSION )
ShuttingDown = true;
TForm::WndProc(Message);
}
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if( !ShuttingDown )
{
if( MessageDlg("Are you sure you would like to exit?",
mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrNo )
CanClose = false;
}
}
Gambit