Advertisement
TLama

Untitled

Jul 28th, 2013
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.17 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. const
  10.   WM_POWERBROADCAST = 536;
  11.   PBT_POWERSETTINGCHANGE = $8013;
  12.   DEVICE_NOTIFY_WINDOW_HANDLE = 0;
  13.   GUID_MONITOR_POWER_ON: TGUID = '{02731015-4510-4526-99E6-E5A17EBD1AEA}';
  14.  
  15. type
  16.   HPOWERNOTIFY = Pointer;
  17.   PHPOWERNOTIFY = ^HPOWERNOTIFY;
  18.  
  19.   PPowerBroadcastSetting = ^TPowerBroadcastSetting;
  20.   TPowerBroadcastSetting = record
  21.     PowerSetting: TGUID;
  22.     DataLength: DWORD;
  23.     Data: array[0..0] of UCHAR;
  24.   end;
  25.  
  26. function RegisterPowerSettingNotification(const hRecipient: THandle;
  27.   const PowerSettingGuid: TGUID; const Flags: DWORD): HPOWERNOTIFY; stdcall;
  28.   external user32 name 'RegisterPowerSettingNotification';
  29. function UnregisterPowerSettingNotification(Handle: HPOWERNOTIFY): BOOL; stdcall;
  30.   external user32 name 'UnregisterPowerSettingNotification';
  31.  
  32. type
  33.   TForm1 = class(TForm)
  34.     Button1: TButton;
  35.     Button2: TButton;
  36.     procedure Button1Click(Sender: TObject);
  37.     procedure Button2Click(Sender: TObject);
  38.   private
  39.     NotifyHandle: HPOWERNOTIFY;
  40.     procedure WMPowerBroadcast(var AMessage: TMessage); message WM_POWERBROADCAST;
  41.   public
  42.     { Public declarations }
  43.   end;
  44.  
  45. var
  46.   Form1: TForm1;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. procedure TForm1.Button1Click(Sender: TObject);
  53. begin
  54.   NotifyHandle := RegisterPowerSettingNotification(Handle, GUID_MONITOR_POWER_ON,
  55.     DEVICE_NOTIFY_WINDOW_HANDLE);
  56.   if not Assigned(NotifyHandle) then
  57.     RaiseLastOSError;
  58. end;
  59.  
  60. procedure TForm1.Button2Click(Sender: TObject);
  61. begin
  62.   if Assigned(NotifyHandle) then
  63.     if not UnregisterPowerSettingNotification(NotifyHandle) then
  64.       RaiseLastOSError;
  65. end;
  66.  
  67. procedure TForm1.WMPowerBroadcast(var AMessage: TMessage);
  68. var
  69.   Setting: TPowerBroadcastSetting;
  70. begin
  71.   if AMessage.WParam = PBT_POWERSETTINGCHANGE then
  72.   begin
  73.     Setting := PPowerBroadcastSetting(AMessage.LParam)^;
  74.     if IsEqualGUID(Setting.PowerSetting, GUID_MONITOR_POWER_ON) then
  75.       if DWORD(Setting.Data[0]) = 1 then
  76.         ShowMessage('The monitor is on.')
  77.       else
  78.         ShowMessage('The monitor is off.');
  79.   end;
  80. end;
  81.  
  82. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement