unit PowerButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Menus, ShellApi, ExtCtrls,dialogs;
type
TPowerButton =
class(TComponent)
private
FHooked: Boolean;
FOnPowerbuttonPress : TNotifyEvent;
PPowerOffEnable:Boolean;
function MessageHook(
var Msg: TMessage): Boolean;
protected
procedure DoPowerbuttonPress;
dynamic;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
published
{events}
property OnPowerbuttonPress: TNotifyEvent
read FOnPowerbuttonPress
write FOnPowerbuttonPress;
{properties}
property PowerOffEnable : boolean
read PPowerOffEnable
write PPowerOffEnable;
end;
procedure Register;
implementation
const
PBT_APMQUERYSUSPEND = 0;
{Request for permission to suspend.}
procedure Register;
begin
RegisterComponents('
Zusätzlich', [TPowerButton]);
end;
constructor TPowerButton.create(AOwner : TComponent);
begin
inherited Create(AOwner);
FHooked := False;
if not (csDesigning
in ComponentState)
then
begin
Application.HookMainWindow(MessageHook);
FHooked := True;
end;
end;
Procedure TPowerButton.DoPowerbuttonPress;
begin
if Assigned(FOnPowerbuttonPress)
then FOnPowerbuttonPress(Self);
end;
function TPowerButton.MessageHook(
var Msg: TMessage): Boolean;
begin
case msg.WParam
of
PWR_SUSPENDREQUEST:
//win2000,XP
begin
if PPowerOffEnable = false
then
begin
msg.Result := PWR_FAIL;
end;
DoPowerbuttonPress;
end;
PBT_APMQUERYSUSPEND:
//win95,98
begin
if PPowerOffEnable = false
then
begin
msg.Result := BROADCAST_QUERY_DENY;
end;
DoPowerbuttonPress;
end;
end;
end;
destructor TPowerButton.Destroy;
begin
if FHooked
then Application.UnhookMainWindow(MessageHook);
inherited Destroy;
end;