Jejeje, pues he encontrado este componente, como no, en otra gran página sobre el fascinante mundo de Delphi, despues de probar montones de formas, con privilegiios, mensajes, capturas, etc, etc. Y lo peor es que cada vez que se pulsa el maldito botón hay que arrancar el sistema.
Sin más rollos ni dilaciones os presento a PowerButton (Lo publico tal como lo he encontrado):
Código Delphi
[-]
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
Version, Hersteller: string;
IResultHi, IResultLo, ILParamHi, ILParamLo, IWParamHi, ILParam,
IWParamLo, IWparam, Imsg, IResult: Integer;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property OnPowerbuttonPress: TNotifyEvent
read FOnPowerbuttonPress write FOnPowerbuttonPress;
property PowerOffEnable: Boolean read PPowerOffEnable write PPowerOffEnable;
end;
procedure Register;
implementation
const
PBT_APMQUERYSUSPEND = 536;
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 begin
if PPowerOffEnable = False then
begin
Msg.Result := PWR_FAIL;
end;
end;
if (Msg.Msg = PBT_APMQUERYSUSPEND) and (Msg.wParam = 0) then begin
if PPowerOffEnable = False then
begin
Msg.Result := BROADCAST_QUERY_DENY;
end;
end;
if (Msg.Msg = PBT_APMQUERYSUSPEND) and (Msg.wParam = 0) then begin
DoPowerbuttonPress;
end;
end;
destructor TPowerButton.Destroy;
begin
if FHooked then Application.UnhookMainWindow(MessageHook);
inherited Destroy;
end;
end.