Posted on 2007-03-17
Last Modified: 2008-01-09
Hi experts.
I need to intercept the power button press. Searching in msdn...I found that WM_POWERBROADCAST is responsable for this shutdown operation.
Can anyone help me use it?
Thanks a lot.
Question by:A. Cristian Csiki
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Accepted Solution

2266180 earned 500 total points
ID: 18742997
it's actually pretty straitforward:

unit Unit1;


  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    procedure WMPowerBroadcast(var Msg: TMessage); message WM_POWERBROADCAST;
    { Public declarations }

  Form1: TForm1;


{$R *.dfm}

procedure TForm1.WMPowerBroadcast(var Msg: TMessage);
  showmessage('good night ');


with this code, you will get a "good night" message before the system goes into suspend and anotehr "good night" message when it returns.
LVL 28

Expert Comment

ID: 18743008
remove the
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
(it was a rip from another project and I forgot to remove those lines :D )
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 18744229
Hi ciuly.
Glad once again you help me. I tried checking your code, but nothing happens. I tried even to put a memo and save the WMPowerBroadcast msg in a log file from that procedure but still nothing.
One more thing. If I press the power button or I go Start-SHutdown-SHutdown is the same msg that receives WMPowerBroadcast?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 18746869
LVL 28

Expert Comment

ID: 18746911
if you don't get that message, the first thing that gets into my mind is the following quote from MSDN: "Client      Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0." (from here:
are you running from one of these?

"If I press the power button or I go Start-SHutdown-SHutdown is the same msg that receives WMPowerBroadcast?"
no. that message is WM_EndSession.
LVL 21

Expert Comment

ID: 18747288
>>WM_POWERBROADCAST is responsable for this shutdown operation

WM_QUERYENDSESSION and WM_ENDSESSION are sent when shutting down
according to MSDN:
"The WM_POWERBROADCAST message is broadcast to an application to notify it of power-management events" if you look at possible wParam values you'll see that this message is sent when computer is about to resume/suspend or is switching from battery power to A/C.

LVL 11

Author Comment

by:A. Cristian Csiki
ID: 18750861
HI all.
Got the point with  WM_QUERYENDSESSION and WM_POWERBROADCAST. I'm running win xp sp2 with delphi 7 pro. Still can't trap the power button press, or the shutdown caused by it.

I even trieded a component found on, but not working. I don't know what I miss....

Any ideas?

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question