RegisterPowerSettingNotification在Delphi中使用

时间:2013-07-28 18:53:07

标签: delphi winapi delphi-xe2

如何在Delphi XE2中将RegisterPowerSettingNotificationGUID_MONITOR_POWER_ON结合使用?

1 个答案:

答案 0 :(得分:4)

您必须使用所需的GUID RegisterPowerSettingNotification致电Power Setting GUIDs以注册应用以接收针对特定电源设置事件的电源设置通知,如果不再需要电话UnregisterPowerSettingNotification。< / p>

delphi示例可能如下所示:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  GUID_MONITOR_POWER_ON: TGUID = '{02731015-4510-4526-99e6-e5a17ebd1aea}';

type

  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FHPOWERNOTIFY: THandle;
  protected
    procedure WM_POWERBROADCAST(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';

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FHPOWERNOTIFY := RegisterPowerSettingNotification(Handle,
    @GUID_MONITOR_POWER_ON, DEVICE_NOTIFY_WINDOW_HANDLE);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnregisterPowerSettingNotification(FHPOWERNOTIFY);
end;

procedure TForm1.WM_POWERBROADCAST(var Msg: TMessage);
begin
  if PPOWERBROADCAST_SETTING(Msg.LParam)^.Data[0] = 0 then
    Memo1.Lines.Add('Off')
  else
    Memo1.Lines.Add('ON')
end;

end.