Nabend.
Funktioniert ab Vista, Stichwort:
RegisterPowerSettingNotification
Delphi-Quellcode:
TForm1 =
class(TForm)
private
FPowerSetting: THandle;
protected
procedure WmPowerBrotkasten(
var Msg: TMessage);
message WM_POWERBROADCAST;
end;
function RegisterPowerSettingNotification(hRecipient: THandle; PowerSettingGuid: PGUID;
Flags: DWORD): THandle;
stdcall;
external '
user32.dll';
function UnregisterPowerSettingNotification(
Handle: THandle): BOOL;
stdcall;
external '
user32.dll';
{...}
// Start monitoring
const
GUID_MONITOR_POWER_ON: TGUID = '
{02731015-4510-4526-99e6-e5a17ebd1aea}';
begin
FPowerSetting := RegisterPowerSettingNotification(
Handle, @GUID_MONITOR_POWER_ON, DEVICE_NOTIFY_WINDOW_HANDLE);
end;
// End monitoring
begin
UnregisterPowerSettingNotification(FPowerSetting);
end;
// Receive notifications
procedure TForm1.WmPowerBrotkasten(
var Msg: TMessage);
var
pbSetting: POWERBROADCAST_SETTING;
begin
pbSetting := PPOWERBROADCAST_SETTING(Msg.LParam)^;
if pbSetting.Data[0] = 0
then
// Monitor turned off
else if pbSetting.Data[0] = 1
then
// Monitor turned on
end;