Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Windows API / MS.NET Framework API (https://www.delphipraxis.net/20-library-windows-api-ms-net-framework-api/)
-   -   Delphi TPowerButton (https://www.delphipraxis.net/15845-tpowerbutton.html)

CTV 5. Feb 2004 09:34


TPowerButton
 
Delphi-Quellcode:
unit PowerButton;

///////////////////////////////////////////////////////////////////////////////////////
//Dies ist eine Komponente um den Powerknopf anzusteuern.
//Eigenschaften:
//PowerOffEnable:boolean
//  >false lässt den Pc nicht mehr herunterfahren
//Ereignisse:
//  OnPowerbuttonpress
//  >Wird ausgeführt wenn der Powerbutton gedrückt wurde
//
//Programmed by CTV => [url]www.ctvnet.ch[/url]
//
//Komponente darf frei für alles verwendet werden copyright darf nicht entfernt werden
///////////////////////////////////////////////////////////////////////////////////////



interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Menus, ShellApi, ExtCtrls;

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.
Greetz Cyrus

Luckie 5. Feb 2004 09:43

Re: TPowerButton
 
Wozu dieser MessageHook?

Warum nicht in der Klasse:
Delphi-Quellcode:
public
  procedure PowerBtnPress(var msg: TMessage); message: PBT_APMQUERYSUSPEND;
Dann behandelt deine Prozedur auch nur genau diese Message.

CTV 5. Feb 2004 10:06

Re: TPowerButton
 
jo das währe noch eine idee :)
das kam eben von da her als ich noch ned wusste welche message übertragen wird. dann hab ich das teil gehookt damit ich alle messages abfagen konnte um so rauszubekommen welche Message es ist. Aber da ich das ja nun weiss währe das glaub besser so wie du bereits gesagt hast.

Greetz Cyrus


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:28 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz