🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2381

Re: HELP: My app closes, but cancels Windows shutdown?

#2381
From: Dennis Jones
Date: Thu, 08 Mar 2007 22:53
120 lines
5233 bytes
"A Nonymous" <anonymous@hotmail.com> wrote in message 
news:Xns98EDAA07AFDF0anonymoushotmailcom@207.105.83.66...

>> Why don't you simply use the form's OnCloseQuery and OnClose events
>> instead?  The VCL already handles the WM_QUERYENDSESSION and
>> WM_ENDSESSION messages.  OnCloseQuery is triggered when
>> WM_QUERYENDSESSION is received.
>>
>
> I just tried that.  I commented out the message map line so as not to
> interfeer with the normal processing of the end session messages.  I
> then modified the OnQueryClose to not ask about saving, just save and
> exit.
>

Yeah, that's why I handle both the VCL events and the Windows messages, and 
then also some of my own messages, because I have control over when the user 
is prompted for confirmation.  I ask the user for confirmation only if the 
user is shutting down the application, but not if Windows is shutting down:

MESSAGE void __fastcall TMainForm::WM_Service_Stop(TMessage &Message)
{
   bool CanStop = true;
   if ( Message.WParam != NoPrompt )
   {
      StopQuery( CanStop );
   }
   if ( !CanStop )
   {
      return;
   }

   LogMsg("Application is stopping the service - Received service stop 
notification");
   Service->Stop();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormCloseQuery(TObject */*Sender*/, bool 
&CanClose)
{
   LogMsg("Application wants to shut down - Received close window request");
   if ( Service->IsRunning() )
   {
      StopQuery( CanClose );
      if ( !CanClose )
      {
         LogMsg("Application shut down cancelled");
      }
   }
}
//---------------------------------------------------------------------------

void TMainForm::StopQuery( bool &CanStop )
{
   LogMsg("Application wants to stop the service - Received service stop 
request");
   AnsiString StopQueryMsg = "You are about to deactivate the service...";

   if ( IDCANCEL == Application->MessageBox( StopQueryMsg.c_str(),
                                             "Confirm",
                                             MB_OKCANCEL | 
MB_ICONWARNING ) )
   {
      LogMsg("Service stop cancelled");
      CanStop = false;
   }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormClose(TObject */*Sender*/,
      TCloseAction &/*Action*/)
{
   LogMsg("Application is shutting down - Received close window 
notification");
   SendMessage( this->Handle, WM_SERVICE_STOP, NoPrompt, 0 );
}
//---------------------------------------------------------------------------

MESSAGE void __fastcall TMainForm::WMEndSession(TMessage &msg)
{
   // if WM_QUERYENDSESSION returned false, it's not okay to shutdown the 
program
   if ( msg.WParam == false ) return;

   // if we get this far, it's okay to shut down the interface
   LogMsg("System is shutting down - Received end of session notification");

   // if Windows is shutting down, we want to shut down the application 
without user intervention;
   // to accomplish this, we must stop the interface service first
   SendMessage( this->Handle, WM_SERVICE_STOP, NoPrompt, 0 );

   // now we know the interface service is not running; we can close the 
application and we
   // won't be queried about shutting down
   Close();
}
//---------------------------------------------------------------------------

// handle WM_QUERYENDSESSION -- this isn't strictly necessary since we 
always want to allow
// Windows to shut down, but it's here for completeness and for the sake of 
logging
MESSAGE void __fastcall TMainForm::WMQueryEndSession(TMessage &msg)
{
   LogMsg("System wants to shut down - Received end of session query 
notification");

   // when an application receives the WM_QUERYENDSESSION message, it must 
tell Windows
   // whether or not it is really okay to shut down;
   // setting the message result to 'true' means it is okay to shut down 
Windows
   msg.Result = true;
}
//---------------------------------------------------------------------------

- Dennis 
Technical Details
Message-ID: <45f093cb$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <Xns98ED90968E734anonymoushotmailcom@207.105.83.66> <Xns98ED941B2E2C8anonymoushotmailcom@207.105.83.66> <45f08683$1@newsgroups.borland.com> <Xns98EDAA07AFDF0anonymoushotmailcom@207.105.83.66>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,346f60f70c29a156
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Dennis Jones" <nospam@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <Xns98ED90968E734anonymoushotmailcom@207.105.83.66> <Xns98ED941B2E2C8anonymoushotmailcom@207.105.83.66> <45f08683$1@newsgroups.borland.com> <Xns98EDAA07AFDF0anonymoushotmailcom@207.105.83.66>
Subject: Re: HELP: My app closes, but cancels Windows shutdown?
Date: Thu, 8 Mar 2007 14:53:31 -0800
Lines: 122
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 71.245.109.58
Message-ID: <45f093cb$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1173394379 71.245.109.58 (8 Mar 2007 14:52:59 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail