Link to home
Start Free TrialLog in
Avatar of newind
newindFlag for Philippines

asked on

Disabling ScreenSaver

How to I disable a screensaver?  I want to turn off the screen saver while a portion of my program is running.  
Thanks.
Avatar of rwilson032697
rwilson032697

You certainly can prevent the computer's screen-saver from starting.
Declare a message handler for your form like this -

procedure WMSysCommand (var Msg: TWMSysCommand); message WM_SYSCOMMAND;

  And then, implement the following in the body of your code -

procedure TTraceMeFrm.WMSysCommand (var Msg: TWMSysCommand);
begin
  if Msg.cmdType = SC_ScreenSave then
    Msg.Result := -1
  else
    inherited
end;

  This will prevent the Windows screen saver from running while your application is running.

Cheers,

Raymond.

Listening..
Avatar of newind

ASKER

Thanks Raymond.  However, I don't want to turn off the screen saver while my app is running.  In fact my app runs on a server, thus it has to be running all the time.  What I want is to disable the screensaver while a portion of my app is running.  How do I do this?
Perhaps this change might help...

procedure TTraceMeFrm.WMSysCommand (var Msg: TWMSysCommand);
begin
  if (Msg.cmdType = SC_ScreenSave) AND
      SuppressTheScreenSaver then    
    Msg.Result := -1
  else
    inherited
end;

Where SuppressTheScreenSaver is a boolean you set to true while the portion of your program is running that shoudl stop the screensaver from running...

Cheers,

Raymond.

Hi
You could temporarily turn off screen saving like this:

  {Turn screen saving off}
  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,0,nil,0);
  try
    {Do the job where screen saving not required}
     
  finally
    {Turn screen saving on again}
    SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,1,nil,0);
  end;


Other ideas Would be :
this would force the default screensaver to start:

  SendMessage(Application.Handle,WM_SYSCOMMAND,SC_SCREENSAVE,0);
 
And this will cause the screensaver which has started to stop:

  SendMessage(Application.Handle,WM_SYSCOMMAND,SC_SCREENSAVE,1);

Regards Barry
Barry, does SPI_SETSCREENSAVEACTIVE work in NT?

Cheers,

Raymond.
I have a nifty little examples of two ways of disabling the screensaver on my website:

www.drdelphi.com.


Good luck!!
Hi Raymond
yes about half the SPI_* messages work in nt including SPI_SETSCREENSAVEACTIVE

see win32.hlp file under SystemParametersInfo for details ..
Regards
tried and tested:

  {Temporarily turn screen saver off}
  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,0,nil,0);
  try

    {Do the code here}
     
  finally
    {Turn screen saver on again}
    SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,1,nil,0);
  end;
ps dr delphi
did you make the search for your website?
looking for one myself ,i dont think the new server im moving to supports frontpage extensions which i used for my search. suppose i need a cgi one.
inthe,
  I leeched it from a JavaScript website. There is no CGI used in it at all. I like it because it is fast.
Avatar of newind

ASKER

Thanks Barry.  I'm already inclined to give you the points. I  think the one of SystemParametersInfo will work indeed.   But one last question.  To stop the currently running screensaver you said I can use SendMessage(Application.Handle,WM_SYSCOMMAND,SC_SCREENSAVE,0);
I tried it and it didn't work.  Is Application.Handle MY App's handle?
unit Unit1;

 interface

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

 type
   TForm1 = class(TForm)
   private
     procedure WMSysCommand(var Msg : TWMSysCommand);
               message WM_SYSCOMMAND;
   public
     { Public declarations }
   end;

 var
   Form1: TForm1;

 implementation

 {$R *.DFM}

 procedure TForm1.WMSysCommand(var Msg : TWMSysCommand);
 begin
   //trap the message and set its result to -1
   if (Msg.CmdType = SC_SCREENSAVE) then
     Msg.Result := -1
   else
     inherited;
 end;

 end.


IT may work in NT? try it....

Craig C.
ah, sorry never read as to why this would not work..... how about a little if statement?

unit Unit1;

 interface

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

 type
   TForm1 = class(TForm)
   private
     procedure WMSysCommand(var Msg : TWMSysCommand);
               message WM_SYSCOMMAND;
   public
     { Public declarations }
   end;

 var
   Form1: TForm1;

 implementation

 {$R *.DFM}

 procedure TForm1.WMSysCommand(var Msg : TWMSysCommand);
 begin
   //trap the message and set its result to -1
if   part_of_program_running=false then
  begin
   if (Msg.CmdType = SC_SCREENSAVE) then
     Msg.Result := -1
   else
     inherited;
   end;
  end;
end.

What would be wrong with something like that?
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newind

ASKER

Thanks Barry,
keybd_Event(ord('X'), 0, 0, 0) did work.
Wishing you all the best in your endeavours.
- newind
Avatar of newind

ASKER

Thank you to all the experts who tried to help me out.  Good luck to all of you guys!