Article View: borland.public.cppbuilder.nativeapi
Article #6709Re: Before Shutting Down the PC
From: Michel Corbin
Date: Fri, 15 Apr 2005 20:09
Date: Fri, 15 Apr 2005 20:09
95 lines
4716 bytes
4716 bytes
Hi,
A small explanation and the things I'd try.
When the user decide to stop Windows, Windows sends two messages to the
main form of your application
WM_QUERYENDSESSION in order to ask the application if it agrees to end.
and then
WM_ENDSESSION to indicate whether the session is (or is not) going to end
So in the header file of the main form , you must declare :
private: // user declarations
.......
MESSAGE void __fastcall HandleQueryEndSession(TMessage&) ;
MESSAGE void __fastcall HandleEndSession(TMessage&) ;
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_QUERYENDSESSION,TMessage,HandleQueryEndSession) ;
MESSAGE_HANDLER(WM_ENDSESSION,TMessage,HandleEndSession) ;
END_MESSAGE_MAP(TForm) ;
bool YesITerminate ; // that is one possible way to manage, you
// must put it to false in the constructor.
And in the cpp file :
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
YesITerminate=false ;
.....
}
//---------------------------------------------------------------------------
MESSAGE void __fastcall TForm1::HandleQueryEndSession(TMessage &Msg)
{
if(....)
Msg.Result=1 ; // in case the application agrees to stop
else
Msg.Result=0 ; // in case the application does not agree to stop
}
//---------------------------------------------------------------------------
MESSAGE void __fastcall TForm1::HandleEndSession(TMessage & Msg)
{
if(Msg.WParam==TRUE) // Windows is going to stop (all the running
{ // processes answered Msg.Result=1 to the first message)
YesITerminate=true ;
// here you close open files etc ... you can, for example,
// remove the icon of your application from the tray icon
}
else // Windows won't stop
{
..... // you can tell the user want you want, perhaps there is
nothing to do
}
Msg.Result=0 ; // I think that's mandatory, I'm not sure
}
//---------------------------------------------------------------------------
you also write an OnCloseQuery event manager :
/---------------------------------------------------------------------------
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if(!YesITerminate)
CanClose=false ; // the choice may be more complicated ...
}
/---------------------------------------------------------------------------
I hope that is understandable
Michel.
Grety wrote:
> I found the text below but I couldn't understand it
> Someone please tell me. thx
> //-----------------------------------------
> Respond correctly to Windows shutdown WM_QUERYENDSESSION
> Posted: 21 Jan 2005 06:48 AM
> I have an application which minimizes to systemtray when the close button is
> pressed. How ever when shutting down windows the application keeps windows
> from doing so.
> I have made the following implementation i order to catch the windows
> message WM_QUERYENDSESSION
>
> protected override void WndProc(ref Message m)
> {
> base.WndProc(ref m);
> // WM_QUERYENDSESSION
> if (m.Msg == 0x11) {
> //close up
> Application.Exit();
> }
> }
>
> Now my application shutsdown (I have checked in taskmanager) but still
> windows keeps running. Second time I ask for a shutdown it works. So my
> question is how do I respond correctly to the windows shutdown request?
> //-----------------------------------------
>
>
Technical Details
Message-ID:
<42601f81@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!mpls-transit-01.news.qwest.net!38.119.100.82.MISMATCH!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.nuthinbutnews.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<425ebafb@newsgroups.borland.com> <425ebef8@newsgroups.borland.com> <425fe54e@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,92564fdeda5ee096 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 15 Apr 2005 22:09:29 +0200 From: "Michel Corbin" <"Michel Corbin"> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr-FR; rv:1.0.2) Gecko/20030208 Netscape/7.02 X-Accept-Language: fr-fr, fr, en, en-us, es, it, p MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Before Shutting Down the PC References: <425ebafb@newsgroups.borland.com> <425ebef8@newsgroups.borland.com> <425fe54e@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 80.125.199.12 Message-ID: <42601f81@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1113595777 80.125.199.12 (15 Apr 2005 13:09:37 -0800) Lines: 96 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!mpls-transit-01.news.qwest.net!38.119.100.82.MISMATCH!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.nuthinbutnews.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail