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.
newindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
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.

0
intheCommented:
Listening..
0
newindAuthor Commented:
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?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rwilson032697Commented:
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.

0
intheCommented:
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
0
rwilson032697Commented:
Barry, does SPI_SETSCREENSAVEACTIVE work in NT?

Cheers,

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

www.drdelphi.com.


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

see win32.hlp file under SystemParametersInfo for details ..
Regards
0
intheCommented:
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;
0
intheCommented:
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.
0
DrDelphiCommented:
inthe,
  I leeched it from a JavaScript website. There is no CGI used in it at all. I like it because it is fast.
0
newindAuthor Commented:
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?
0
craig_capelCommented:
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.
0
craig_capelCommented:
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?
0
intheCommented:
hi newwind,
no your correct application.handle wont do it.

also ..sc_screensave,0) is fine for starting it but not for stopping it.
i tried ..sc_screensave,1)  and it brought up the shutdown screen :o]

to stop it its better to use something like:

keybd_Event(ord('X'), 0, 0, 0);{key down}
keybd_Event(ord('X'), 0, KEYEVENTF_KEYUP, 0);{key up}

this just sends a keypress which stops it same as sending a mousemove message..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newindAuthor Commented:
Thanks Barry,
keybd_Event(ord('X'), 0, 0, 0) did work.
Wishing you all the best in your endeavours.
- newind
0
newindAuthor Commented:
Thank you to all the experts who tried to help me out.  Good luck to all of you guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.