Mega Code Archive

 
Categories / Delphi / System
 

How to control the behaviour of the PC PowerOnOff button (APM)

Title: How to control the behaviour of the PC PowerOn/Off button (APM) 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 Version, Hersteller: string; IResultHi, IResultLo, ILParamHi, ILParamLo, IWParamHi, ILParam, IWParamLo, IWparam, Imsg, IResult: Integer; 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 = 536; {Request for permission to suspend.} procedure Register; begin RegisterComponents('Zus?tzlich', [TPowerButton]); end; constructor TPowerButton.Create(AOwner: TComponent); begin inherited Create(AOwner); Version := '1.0.0.0'; Hersteller := 'CTVNet.ch'; 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 IResultHi := Msg.ResultHi; IResultLo := Msg.ResultLo; ILParamHi := Msg.LParamHi; ILParamLo := Msg.LParamLo; IWParamHi := Msg.WParamHi; ILParam := Msg.lParam; IWParamLo := Msg.WParamLo; Imsg := Msg.Msg; IResult := Msg.Result; IWparam := Msg.wParam; if (Msg.Msg = PBT_APMQUERYSUSPEND) and (Msg.wParam = 0) then //win95/98 begin if PPowerOffEnable = False then begin Msg.Result := PWR_FAIL; end; end; if (Msg.Msg = PBT_APMQUERYSUSPEND) and (Msg.wParam = 0) then //winNT,2k,XP begin if PPowerOffEnable = False then begin Msg.Result := BROADCAST_QUERY_DENY; end; end; if (Msg.Msg = PBT_APMQUERYSUSPEND) and (Msg.wParam = 0) then //excute Event begin DoPowerbuttonPress; end; end; destructor TPowerButton.Destroy; begin if FHooked then Application.UnhookMainWindow(MessageHook); inherited Destroy; end; end.