home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d456 / PWRSAVE.ZIP / PwrSave.pas < prev    next >
Pascal/Delphi Source File  |  2002-08-22  |  11KB  |  305 lines

  1. unit PwrSave;
  2. //==============================================================================
  3. // PwrSave: Delphi Component
  4. // Version 1.1
  5. // Date 22 August 2002
  6. // Copyright Jan Mitrovics 2002
  7. //
  8. // Send comments to: Mitrovics@web.de
  9. //
  10. // This is a freeware component! Use at own risk!
  11. //
  12. // COPYRIGHT:
  13. // Copyright Jan Mitrovics 2002.
  14. //
  15. // DISCLAIMER:
  16. // THIS COMPONENT IS PROVIDED AS IS! THE AUTHOR WILL TAKE NO RESPONSIBILITY
  17. // FOR ANY DAMAGES RESULTING FORM THE USE OF THIS COMPONENT!
  18. // The author disclaims any and all warranties, express or implied,
  19. // oral or written, including any implied warranties of merchantability or
  20. // fitness for a particular purpose.
  21. // The author does not and cannot warrant the performance or results you may
  22. // obtain by using the software or documentation.
  23. //
  24. // DISTRIBUTION:
  25. // This component maybe freely distributed under following conditions:
  26. //   Charging for the component is prohibited.
  27. //   Distribution must be complete with all original files unchanged.
  28. //   If you add own files, clearly identify that you are providing them.
  29. //
  30. // -----------------------------------------------------------------------------
  31. //
  32. // Purpose:
  33. // Windows has the ability to shutdown / hibernate / standby when no user
  34. // interaction takes place for a certain time. When writing programs for
  35. // data aqcuisition, control systems or servers this might be very undesired.
  36. // This component will prevent these events and/or gives you the opportunity
  37. // to take necessary action when such events occur.
  38. // In addition this program can as well prevent shutdown / log off initiated
  39. // by the user.
  40. //
  41. // Function:
  42. // This little component will catch the listed Windows messages that might
  43. // intercept program execution.
  44. //   WM_POWERBROADCAST with
  45. //     PBT_APMQUERYSUSPEND   -> OnQuerySuspend
  46. //     PBT_APMQUERYSTANDBY   -> OnQueryStandby
  47. //   WM_ENDSESSION  -> OnQueryEndSession
  48. //
  49. // It will fire the corresponding events, if an eventhandler has been assigned.
  50. // If no eventhandler is assigned or user interaction is not allowed by Windows,
  51. // then it will respond to the messages as defined by the properties:
  52. //   AllowSuspend, AllowStandby, AllowEndSession
  53. //
  54. // In addition to this, events for the other APM messages are included.
  55. //
  56. // Usage:
  57. // Drop the component on your main form.
  58. // If you do not assign any event handlers and do not change the default values,
  59. // then Windows will not shutdown / hibernate / standby (unless this is forced
  60. // by a program / driver) and the user can not log off / shutdown while the
  61. // program is running.
  62. // Use the Events and the proporties to fine tune the behaviour.
  63. // The Query events will give you a chance to e.g. display a dialog to ask the
  64. // user what to do.
  65. //
  66. // I have tested the component with Delphi 4, 5, 6 and Windows 2000 and 98
  67. // However, testing has not been extensive. If you experience any problems,
  68. // please let me know. (email: Mitrovics@web.de)
  69. // If you test this component in other versions of Windows / Delphi, please
  70. // send me a short report, so I can include it with the documentation.
  71. //
  72. // Check the Windows SDK Help for more information
  73. //
  74. // History:
  75. //
  76. // Version 1.0: First official release only for Delphi 6
  77. //
  78. // Version 1.1: Support for Delphi 5 and Delhi 4 added
  79. //   Remarks: This was simple, just added Forms to the uses statement.
  80. //==============================================================================
  81.  
  82. interface
  83.  
  84. uses
  85.   Windows, Messages, SysUtils, Classes, Forms;
  86.  
  87. const
  88.   // APM messages
  89.   PBT_APMQUERYSUSPEND       = $0000;
  90.   PBT_APMQUERYSTANDBY       = $0001;
  91.   PBT_APMQUERYSUSPENDFAILED = $0002;
  92.   PBT_APMQUERYSTANDBYFAILED = $0003;
  93.   PBT_APMSUSPEND            = $0004;
  94.   PBT_APMSTANDBY            = $0005;
  95.   PBT_APMRESUMECRITICAL     = $0006;
  96.   PBT_APMRESUMESUSPEND      = $0007;
  97.   PBT_APMRESUMESTANDBY      = $0008;
  98.   PBT_APMBATTERYLOW         = $0009;
  99.   PBT_APMPOWERSTATUSCHANGE  = $000A;
  100.   PBT_APMOEMEVENT           = $000B;
  101.  
  102.   // ACLineStatus values
  103.   AC_Offline =   0;
  104.   AC_Online  =   1;
  105.   AC_Unkown  = 255;
  106.  
  107.   // BatteryFlag values
  108.   BF_High      =   1;
  109.   BF_Low       =   2;
  110.   BF_Critical  =   4;
  111.   BF_Charging  =   8;
  112.   BF_NoBattery = 128;
  113.   BF_Unkown    = 255;
  114.  
  115. type
  116.  
  117.   TQueryEvent  = procedure(Sender: TObject; var Allow: boolean) of object;
  118.   TPowerEvent  = procedure(Sender: TObject) of object;
  119.   TOEMEvent    = procedure(Sender: TObject; dwEventCode: integer) of object;
  120.                // dwEventCode depends on the hardware manufacturer
  121.   TStatusEvent = procedure(Sender: TObject;
  122.                            ACLineStatus, BatteryFlag, BatteryLifePercent: byte;
  123.                            BatteryLifeTime, BatteryFullLifeTime: longword)
  124.                            of object;
  125.                // BatteryLifeTime and BatteryFullLifeTime is given in seconds
  126.  
  127.   TPwrSave = class(TComponent)
  128.   private
  129.     { Private declarations }
  130.     FHWnd: THandle;
  131.     FAllowSuspend: boolean;
  132.     FAllowStandby: boolean;
  133.     FAllowEndSession: boolean;
  134.     FOnQuerySuspend: TQueryEvent;
  135.     FOnQueryStandby: TQueryEvent;
  136.     FOnQueryEndSession: TQueryEvent;
  137.     FOnBatteryLow: TPowerEvent;
  138.     FOnPowerStatusChange: TStatusEvent;
  139.     FOnOemEvent: TOEMEvent;
  140.     FOnQuerySuspendFailed: TPowerEvent;
  141.     FOnQueryStandbyFailed: TPowerEvent;
  142.     FOnResumeCritical: TPowerEvent;
  143.     FOnResumeSuspend: TPowerEvent;
  144.     FOnResumeStandby: TPowerEvent;
  145.     FOnSuspend: TPowerEvent;
  146.     FOnStandby: TPowerEvent;
  147.     procedure WndProc(var Msg: TMessage);
  148.   protected
  149.     { Protected declarations }
  150.     function QuerySuspend: boolean;
  151.     function QueryStandby: boolean;
  152.     function QueryEndSession: boolean;
  153.     procedure PowerStatusChange;
  154.   public
  155.     { Public declarations }
  156.     constructor Create(AOwner: TComponent); override;
  157.     destructor Destroy; override;
  158.   published
  159.     { Published declarations }
  160.     property AllowSuspend: boolean read FAllowSuspend write FAllowSuspend;
  161.     property AllowStandby: boolean read FAllowStandby write FAllowStandby;
  162.     property AllowEndSession: boolean read FAllowEndSession write FAllowEndSession;
  163.     property OnQuerySuspend: TQueryEvent read FOnQuerySuspend write FOnQuerySuspend;
  164.     property OnQueryStandby: TQueryEvent read FOnQueryStandby write FOnQueryStandby;
  165.     property OnQueryEndSession: TQueryEvent read FOnQueryEndSession write FOnQueryEndSession;
  166.     property OnBatteryLow: TPowerEvent read FOnBatteryLow write FOnBatteryLow;
  167.     property OnPowerStatusChange: TStatusEvent read FOnPowerStatusChange write FOnPowerStatusChange;
  168.     property OnOemEvent: TOEMEvent read FOnOemEvent write FOnOemEvent;
  169.     property OnQuerySuspendFailed: TPowerEvent read FOnQuerySuspendFailed write FOnQuerySuspendFailed;
  170.     property OnQueryStandbyFailed: TPowerEvent read FOnQueryStandbyFailed write FOnQueryStandbyFailed;
  171.     property OnResumeCritical: TPowerEvent read FOnResumeCritical write FOnResumeCritical;
  172.     property OnResumeSuspend: TPowerEvent read FOnResumeSuspend write FOnResumeSuspend;
  173.     property OnResumeStandby: TPowerEvent read FOnResumeStandby write FOnResumeStandby;
  174.     property OnSuspend: TPowerEvent read FOnSuspend write FOnSuspend;
  175.     property OnStandby: TPowerEvent read FOnStandby write FOnStandby;
  176.   end;
  177.  
  178. procedure Register;
  179.  
  180. implementation
  181.  
  182. procedure Register;
  183. begin
  184.   RegisterComponents('System', [TPwrSave]);
  185. end;
  186.  
  187. { TPwrSave }
  188.  
  189. procedure TPwrSave.WndProc(var Msg: TMessage);
  190. var Allow: boolean;
  191. begin
  192.   inherited;
  193.   if Msg.Msg = WM_POWERBROADCAST then
  194.     case Msg.WParam of
  195.       PBT_APMQUERYSUSPEND:
  196.        begin
  197.         if (Msg.LParam and 1) = 1 then
  198.           Allow := QuerySuspend // Check for User Interaction
  199.         else
  200.           Allow := AllowSuspend;
  201.         if Allow then
  202.           Msg.Result := integer(true)
  203.         else
  204.           Msg.Result := BROADCAST_QUERY_DENY;
  205.        end;
  206.       PBT_APMQUERYSTANDBY:
  207.        begin
  208.         if (Msg.LParam and 1) = 1 then
  209.           Allow := QueryStandby // Check for User Interaction
  210.         else
  211.           Allow := AllowStandby;
  212.         if Allow then
  213.           Msg.Result := integer(true)
  214.         else
  215.           Msg.Result := BROADCAST_QUERY_DENY;
  216.        end;
  217.       PBT_APMBATTERYLOW:
  218.         if Assigned(FOnBatteryLow) then
  219.           FOnBatteryLow(Self);
  220.       PBT_APMPOWERSTATUSCHANGE:
  221.         PowerStatusChange;
  222.       PBT_APMOEMEVENT:
  223.         if Assigned(FOnOemEvent) then
  224.           FOnOemEvent(Self, Msg.LParam);
  225.       PBT_APMQUERYSUSPENDFAILED:
  226.         if Assigned(FOnQuerySuspendFailed) then
  227.           FOnQuerySuspendFailed(Self);
  228.       PBT_APMQUERYSTANDBYFAILED:
  229.         if Assigned(FOnQueryStandbyFailed) then
  230.           FOnQueryStandbyFailed(Self);
  231.       PBT_APMRESUMECRITICAL:
  232.         if Assigned(FOnResumeCritical) then
  233.           FOnResumeCritical(Self);
  234.       PBT_APMRESUMESUSPEND:
  235.         if Assigned(FOnResumeSuspend) then
  236.           FOnResumeSuspend(Self);
  237.       PBT_APMRESUMESTANDBY:
  238.         if Assigned(FOnResumeStandby) then
  239.           FOnResumeStandby(Self);
  240.       PBT_APMSUSPEND:
  241.         if Assigned(FOnSuspend) then
  242.           FOnSuspend(Self);
  243.       PBT_APMSTANDBY:
  244.         if Assigned(FOnStandby) then
  245.           FOnStandby(Self);
  246.     end
  247.   else
  248.     if Msg.Msg = WM_ENDSESSION then
  249.      begin
  250.        Msg.Result := integer(QueryEndSession);
  251.      end;
  252. end;
  253.  
  254. function TPwrSave.QuerySuspend: boolean;
  255. begin
  256.   if Assigned(FOnQuerySuspend) then
  257.     FOnQuerySuspend(Self, Result)
  258.   else
  259.     Result := AllowSuspend;
  260. end;
  261.  
  262. function TPwrSave.QueryStandby: boolean;
  263. begin
  264.   if Assigned(FOnQueryStandby) then
  265.     FOnQueryStandby(Self, Result)
  266.   else
  267.     Result := AllowStandby;
  268. end;
  269.  
  270. function TPwrSave.QueryEndSession: boolean;
  271. begin
  272.   if Assigned(FOnQueryEndSession) then
  273.     FOnQueryEndSession(Self, Result)
  274.   else
  275.     Result := AllowEndSession;
  276. end;
  277.  
  278. procedure TPwrSave.PowerStatusChange;
  279. var Status: _SYSTEM_POWER_STATUS;
  280. begin
  281.   if Assigned(FOnPowerStatusChange) then
  282.     if GetSystemPowerStatus(Status) then
  283.      begin
  284.       FOnPowerStatusChange(Self, Status.ACLineStatus, Status.BatteryFlag,
  285.                            Status.BatteryLifePercent, Status.BatteryLifeTime,
  286.                            Status.BatteryFullLifeTime);
  287.      end;
  288. end;
  289.  
  290. constructor TPwrSave.Create(AOwner: TComponent);
  291. begin
  292.   inherited;
  293.   if not (csDesigning in ComponentState) then
  294.     FHWnd := AllocateHWnd(WndProc);
  295. end;
  296.  
  297. destructor TPwrSave.Destroy;
  298. begin
  299.   if not (csDesigning in ComponentState) then
  300.     DeallocateHWnd(FHwnd);
  301.   inherited;
  302. end;
  303.  
  304. end.
  305.