Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

sleep mode in delphi

sleep mode in delphi

sleep mode in delphi

Hi all,

is there any way to catch the computer sleep mode event with an application created in Delphi 7 ?



RE: sleep mode in delphi

Look up "Power Messages" in the Windows SDK (under the help menu).

"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: sleep mode in delphi


I didn't found documentation about power messages in my windows help...

I tried to do something with this procedure :

    procedure WMSysCommand (var Msg: TWMSysCommand);
                            message WM_SYSCOMMAND;

procedure TfrmLoc.WMSysCommand(var Msg: TWMSysCommand);
  if Msg.cmdType = SC_ScreenSave then
    Msg.Result := -1;

And nothing works...the event isn't catch.
The problem is that I have to close 2 ADO connections before the sleep mode event.

Anybody has a clue?


RE: sleep mode in delphi

You can go to http://msdn.microsoft.com and search or go  to:




From Delphi Help:

The system sends power messages to all applications and installable drivers whenever a power management event occurs or whenever an application calls the SetSystemPowerState function to suspend operation. The system sends these messages through the WM_POWERBROADCAST message, setting the wParam
 parameter to the message type. For example, the message type, PBT_APMPOWERSTATUSCHANGE, indicates a system power status change message.

The system sends a PBT_APMQUERYSUSPEND message to request permission to suspend system operation. The system expects each application and driver to determine whether the requested event should occur and to return TRUE or BROADCAST_QUERY_DENY indicating this decision. Any application or driver can deny the request and prevent it from occurring.
The system sends a PBT_APMSUSPEND message immediately before suspending operation. This gives applications and drivers one last chance to prepare for the event before it occurs. In many cases, the system sends these messages without requesting permission to do so. This happens, for example, if an application forces suspension with the SetSystemPowerState function.

The system sends the PBT_APMQUERYSUSPENDFAILED message whenever a requested event is denied. These messages are intended to notify applications and drivers to continue operation as usual.
The system sends the PBT_APMRESUMESUSPEND or PBT_APMRESUMECRITICAL message when system operation has been restored.


Note  The WM_POWER message, previously available to applications and drivers for use with power management, is maintained for backward compatibility. All current applications and installable drivers should use and process the WM_POWERBROADCAST message instead.

WM_PowerBroadcast Help

The WM_POWERBROADCAST message is sent to an application to notify it of power-management events.

dwPowerEvent = (DWORD) wParam;
dwData = (DWORD) lParam;



Event notification message. This parameter can be one of the following values:

Value    Meaning
PBT_APMBATTERYLOW    Battery power is low.
PBT_APMOEMEVENT    OEM-defined event occurred.
PBT_APMPOWERSTATUSCHANGE    Power status has changed.
PBT_APMQUERYSUSPEND    Request for permission to suspend.
PBT_APMQUERYSUSPENDFAILED    Suspension request denied.
PBT_APMRESUMECRITICAL    Operation resuming after critical suspension.
PBT_APMRESUMESUSPEND    Operation resuming after suspension.
PBT_APMSUSPEND    System is suspending operation.


Function-specific data. For most messages, this parameter is reserved and not used.
However, if wParam is one of the resume notifications (PBT_APMRESUME*), the lParam parameter can specify the PBTF_APMRESUMEFROMFAILURE flag. This flag indicates that a suspend operation failed after the PBT_APMSUSPEND message was sent.


Return Values

Return TRUE to grant a request.
Return BROADCAST_QUERY_DENY to deny a request.


"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: sleep mode in delphi

Thanx man, really helpful..

although, with which procedure can I catch this message?

I tried with :

procedure TfrmLoc.FormMessage(var Msg: TMsg; var Handled: Boolean);
  Case msg.Message of
      Timer.Enabled := False;


but nothing happened...



RE: sleep mode in delphi

oups sorry

without the "msg."  :)

RE: sleep mode in delphi

Are you trying to catch this on the main form, or a secondary form?

What if you try using the TApplicationEvents component (on the "additional" tab of the palette)?

"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: sleep mode in delphi

I want to catch the event on my main form.

The component TApplicationEvents acts exactly like my procedure, so there is no changes.

My procedure catches other events like mouse clicks or activation for example, but not the system events.

Thanx for your help!


RE: sleep mode in delphi

There's the message number 512 that is catch before the sleep mode event but I can't find it's constant name (like WM_SYSCOMMAND for example).

I don't know if this could be the message...



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close