whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

17 Visitors Online


 
...control the behaviour of the PC PowerOn/Off button (APM)?
Autor: Cyrus
Homepage: http://www.ctvnet.ch
[ Print tip ]  

Tip Rating (5):  
     


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 => www.ctvnet.ch
//
//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, 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.


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners