2009. augusztus 29., szombat

How to check when a screen saver has been invoked


Problem/Question/Abstract:

I want my application to go into 'sleep mode' when the screen saver has been started.

Answer:

unit U1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure AppMessage(var AMessage: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{Get screen saver messages}

procedure TForm1.AppMessage(var AMessage: TMsg; var Handled: Boolean);
begin
  if ((WM_SYSCOMMAND = AMessage.Message) and (SC_SCREENSAVE = AMessage.wParam)) then
  begin
    Handled := True;
    Label1.Caption := 'Warning! Screen saver detected. Top clearance required.';
    Handled := False;
  end;
end;

{On form create}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := AppMessage;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése