Board index » cppbuilder » Re: Before Shutting Down the PC

Re: Before Shutting Down the PC


2005-04-15 11:10:30 PM
cppbuilder5
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?
//-----------------------------------------
 
 

Re:Re: Before Shutting Down the PC

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:
Quote
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?
//-----------------------------------------