RegisterPowerSettingNotification use in Delphi

假如想象 提交于 2020-02-16 07:01:26

问题


How to use the RegisterPowerSettingNotification in conjuction with GUID_MONITOR_POWER_ON in Delphi XE2?


回答1:


You have to call RegisterPowerSettingNotification with the desired GUID Power Setting GUIDs to registers the application to receive power setting notifications for a specific power setting event, if not needed anymore the call UnregisterPowerSettingNotification.

A delphi example could look like this:

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.


来源:https://stackoverflow.com/questions/60198304/need-to-recognize-sleep-wake-from-my-delphi-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!