Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WM_CLOSE / YesNo Dialog on closing program.

22 views
Skip to first unread message

Sinead Bunyan

unread,
Jan 10, 2005, 10:33:09 AM1/10/05
to
Hi,

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.

Bob Gonder

unread,
Jan 10, 2005, 1:34:49 PM1/10/05
to
Sinead Bunyan wrote:

>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(

Remy Lebeau (TeamB)

unread,
Jan 10, 2005, 1:33:12 PM1/10/05
to

"Sinead Bunyan" <nospam...@DAIRYMASTER.COM> wrote in message
news:41e2...@newsgroups.borland.com...

> 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


0 new messages