[SOLVED] Laptop lid close lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2021-01-31 00:30 Hi All, how can I detect when the lid of my laptop closes? I know I have to use the RegisterPowerSettingNotification API, but how? Windows 10. Thanks in advance. ---------------------------------------------------------------------- jamie · 2021-01-31 00:33 I believe that is a device change message or Power message.. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i             IntPtr handle = new WindowInteropHelper(Application.Current.Windows[0]).Handle;          IntPtr hLIDSWITCHSTATECHANGE = RegisterPowerSettingNotification(handle,                 ref GUID_LIDSWITCH_STATE_CHANGE,                 DEVICE_NOTIFY_WINDOW_HANDLE);   Something like that …. then you need to process the WM_POWERBROADCAST message in the form ---------------------------------------------------------------------- pcurtis · 2021-01-31 01:01 Hi there - It's all 'C' to me  I don't understand. I've found this :- Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Windows, Messages; type   { TForm1 }   TForm1 = class(TForm)    Memo1: TMemo;    procedure FormCreate(Sender: TObject);  private     FHPOWERNOTIFY: THandle;   protected     procedure WM_POWERBROADCAST(var Msg: TMessage); message WM_POWERBROADCAST;   public   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; const  GUID_MONITOR_POWER_ON: TGUID =     '{02731015-4510-4526-99E6-E5A17EBD1AEA}'; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  FHPOWERNOTIFY := RegisterPowerSettingNotification(Handle,    @GUID_MONITOR_POWER_ON, DEVICE_NOTIFY_WINDOW_HANDLE);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.  but it won't compile. ---------------------------------------------------------------------- jamie · 2021-01-31 01:19 What part isn't compiling ? Btw, you shouldn't use the procedure name the same as the message name WMPOWERBROADCAST would be good, take the - out of it. btw, a list of the GUID strings you need to use … Take your pick https://docs.microsoft.com/en-us/windows/win32/power/power-setting- guids ---------------------------------------------------------------------- jamie · 2021-01-31 02:01 I wanted to mention that although you may get that to compile it may not notify you.. I am not so sure if the LCL is allowing that message to be processed through the form's message handler. you may need to hook the Window Procedure ---------------------------------------------------------------------- pcurtis · 2021-01-31 07:50 Your right, I use MsgHook that allows me to do that. The final program is :- Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, {Controls, Graphics, Dialogs, StdCtrls,} Windows, Messages, msghook; type  PPowerbroadcastSetting = ^TPowerbroadcastSetting;  TPowerBroadcastSetting = record   PowerSetting: TGUID;   DataLength: DWORD;   Data: array[0..0] of UCHAR;  end;  { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private     FHPOWERNOTIFY: THandle;   protected  public    procedure WMPOWERBROADCAST(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';  function LockWorkStation: boolean; stdcall; external 'user32.dll' name 'LockWorkStation';var  Form1: TForm1;  bOpen : boolean = false; const  GUID_LIDSWITCH_STATE_CHANGE: TGUID  = '{BA3E0F4D-B817-4094-A2D1-D56379E6A0F3}';  DEVICE_NOTIFY_WINDOW_HANDLE: DWORD  = 0;  PBT_POWERSETTINGCHANGE            = $8013; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  AddMsg(self, WM_POWERBROADCAST);  FHPOWERNOTIFY := RegisterPowerSettingNotification(Handle, @GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);end; procedure TForm1.WMPOWERBROADCAST(var Msg: TMessage);var  PB_SETTING : TPowerBroadcastSetting;  Buf : dword;begin  if Msg.wParam = PBT_POWERSETTINGCHANGE then    begin      Buf := 0;      Move(PB_SETTING.Data, Buf, SizeOf(dword));      if Buf = 0 then bOpen := not bOpen;       if not bOpen then LockWorkStation;  end;end; end.