Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-07-2014, 08:51 AM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default How to use and implement this Unit (PowerButton)

Hi and thanks in advanced.

I need to implement that when the computer power button is pressed a message is popup.

I found this unit which I compiled and included in a test app under the uses clause.

My question is: How do I get this unit to fire up (Popup a message or something that tells me that the computer power button was pressed)

Here is the Unit

Code:
unit UnitPowerButton;

///////////////////////////////////////////////////////////////////////////////////////
//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.
Reply With Quote
  #2  
Old 02-07-2014, 09:37 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

1. Create a new package
2. Add unitpowerbutton.pas
3. Build and install
4. Create a new application
5. On the tool palette search the TPoweButton component(under Zusätzlich)
6. Set PowerOffEnable to True, OnPowerButtonPress event:
ShowMessage('power button pressed')
Reply With Quote
  #3  
Old 02-07-2014, 12:49 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Great Thanks

I got all done, however is not firing when the power button in computer is pressed at least in Windows 7 nothing happens.

Any ideas,?

Thanks for the help
Reply With Quote
  #4  
Old 02-07-2014, 02:26 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Try this:
Code:
type
  TForm1 = class(TForm)
  private
    procedure WMQueryEndSession(var Msg : TWMQueryEndSession);
      message WM_QueryEndSession;  
  public
  end;

procedure TForm1.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
  Msg.Result := 0;
  ShowMessage('power button pressed');
end;
Reply With Quote
  #5  
Old 02-07-2014, 03:02 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Hi Delphimpd,

Long time no see my firend. Jut use this small code:

Code:
type
   TForm1 = class(TForm)
   private
     procedure WMEndSession(var Msg: TWMEndSession); message WM_ENDSESSION;
   public
   end;

implementation

{$R *.DFM}

procedure TForm1.WMEndSession(var Msg: TWMEndSession);
begin
   if Msg.EndSession = True then
     ShowMessage(Power button in computer is pressed .');
   inherited;
end;
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #6  
Old 02-07-2014, 03:03 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Oooopss...I just saw kolbasz last post.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #7  
Old 02-07-2014, 03:33 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hey Jasser is always good to see you. Here, in a little busy in a million things to do.

Thank you both kolbasz and you for the help.

Let me try all these.

Hope that family are all good!

Thanks

Best regards,
Reply With Quote
  #8  
Old 02-07-2014, 03:52 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Great !

It worked

Thanks to both
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:47 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.