Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- const
- WM_POWERBROADCAST = 536;
- PBT_POWERSETTINGCHANGE = $8013;
- DEVICE_NOTIFY_WINDOW_HANDLE = 0;
- GUID_MONITOR_POWER_ON: TGUID = '{02731015-4510-4526-99E6-E5A17EBD1AEA}';
- type
- HPOWERNOTIFY = Pointer;
- PHPOWERNOTIFY = ^HPOWERNOTIFY;
- PPowerBroadcastSetting = ^TPowerBroadcastSetting;
- TPowerBroadcastSetting = record
- PowerSetting: TGUID;
- DataLength: DWORD;
- Data: array[0..0] of UCHAR;
- end;
- function RegisterPowerSettingNotification(const hRecipient: THandle;
- const PowerSettingGuid: TGUID; const Flags: DWORD): HPOWERNOTIFY; stdcall;
- external user32 name 'RegisterPowerSettingNotification';
- function UnregisterPowerSettingNotification(Handle: HPOWERNOTIFY): BOOL; stdcall;
- external user32 name 'UnregisterPowerSettingNotification';
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Button2: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- NotifyHandle: HPOWERNOTIFY;
- procedure WMPowerBroadcast(var AMessage: TMessage); message WM_POWERBROADCAST;
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- NotifyHandle := RegisterPowerSettingNotification(Handle, GUID_MONITOR_POWER_ON,
- DEVICE_NOTIFY_WINDOW_HANDLE);
- if not Assigned(NotifyHandle) then
- RaiseLastOSError;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- if Assigned(NotifyHandle) then
- if not UnregisterPowerSettingNotification(NotifyHandle) then
- RaiseLastOSError;
- end;
- procedure TForm1.WMPowerBroadcast(var AMessage: TMessage);
- var
- Setting: TPowerBroadcastSetting;
- begin
- if AMessage.WParam = PBT_POWERSETTINGCHANGE then
- begin
- Setting := PPowerBroadcastSetting(AMessage.LParam)^;
- if IsEqualGUID(Setting.PowerSetting, GUID_MONITOR_POWER_ON) then
- if DWORD(Setting.Data[0]) = 1 then
- ShowMessage('The monitor is on.')
- else
- ShowMessage('The monitor is off.');
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement