We help IT Professionals succeed at work.

How to stop a running screen-saver?

cjclayton
cjclayton asked
on
I know you can use the following code to disable and enable a screen-saver, but how do you stop a screen-saver that is currently running?

I got the following code from another question is this topic area:

procedure TForm1.Scr_Disable;
begin
 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,0,nil,0);
end;

procedure TForm1.Scr_Enable;
begin
 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,1,nil,0);
end;

Thanks
Comment
Watch Question

Commented:
This is a tip found by me some time ago.

8<---------

How to kill the windows Screen Saver

SK Computer Solutions

Here is a little tip i wrote that shows how to close a screen saver.
Keep in mind, there might be better ways of doing this - if you find any, please let the webmaster or myself know.

    // the callback function used when killing an NT screen saver.

  function KillScreenSaverFunc(hwnd: HWND; lParam: LPARAM): Boolean; stdcall;
  begin
     // post WM_CLOSE command in msg queus of all windows
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    Result := True;
  end;


  procedure CScrnDesktop.KillScreenSaver;
  var
    osversioninfo: TOSVersionInfo;
    hsswnd: HWND;
    hDesk: THandle;
  begin
     // first we get the windows version

    osversioninfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);    

    if not GetVersionEx(osversioninfo) then
      Exit;

     // if the command was successful, then we decide
     // what course of action we wish to take:

    case osversioninfo.dwPlatformId of

       // windows 98 and 95
      VER_PLATFORM_WIN32_WINDOWS:
        begin
           // this is easy, just find the window with the
           // screen saver class and kill it
          hsswnd := FindWindow('WindowsScreenSaverClass', nil);
          if hsswnd <> 0 then
            PostMessage(hsswnd, WM_CLOSE, 0, 0);
        end;

       // windows NT
      VER_PLATFORM_WIN32_NT:
        begin
           // screen saver acts as another desktop on NT
          hDesk := OpenDesktop('Screen-saver',
                               0,
                               False,
                               DESKTOP_READOBJECTS or DESKTOP_WRITEOBJECTS);
           // if one exists, then kill all the windows on that desktop
           // using an enumeration function
          if hDesk <> 0 then
          begin
            EnumDesktopWindows(hDesk, @KillScreenSaverFunc, 0);
             // then close the desktop
            CloseDesktop(hDesk);
             // on slow computers, we assume it might take a while,
             // so we just sit there until it closes
            Sleep(2000);
             // then let the system know that the screen saver
             // is no longer active.
            SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
                                 Cardinal(True),
                                 nil,
                                 SPIF_SENDWININICHANGE);
          end;
        end;
    end;
  end;

Commented:
Or just simulate a mouse click or a mouse movement:

// Set the mouse cursor to position x,y:

SetCursorPos(x, y);

// Simulate the left mouse button down
   
mouse_event(MOUSEEVENTF_LEFTDOWN,0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP,0, 0, 0, 0);

// Simulate the right mouse button down

mouse_event(MOUSEEVENTF_RIGHTDOWN,0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP,0, 0, 0, 0);
Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
ginsonic, the first tip is the same link i posted in the question, and this is duplicate from it

Commented:
Maybe but I don't copy your link . This was from my tips&tricks database . I have type here a comment before to see that cjclayton put twice questions.
And I still think that is better to simulate a press key or a mouse movement .

Commented:
BTW I used the original article not a copy ( see the author : SK Computer Solutions )

Commented:
I reckon mouse_event and keybd_event would be the best/easiest way to go.
How 'bout screen savers with passwords? You might need to
actually kill the process.

Commented:
How you can see rondi I suggest two way ... OK, mnasman proposed one of them, too (into a twin question). There I come just with the second solution . The answer depend just by cjclayton needs .

See you guys on net.
Nick

Commented:
Disabling screen saver temporarily
If you want to turn off the screen saver while your program is running, you don't have to disable the Windows screen saver at all. Just define the following method to handle the appropriate Windows message:

procedure TForm1.AppMessage (var Msg: TMsg; var Handled: boolean);
begin
  if (Msg.Message = WM_SYSCOMMAND) and (Msg.wParam = SC_SCREENSAVE) then
    Handled := true;
end;

On the form's OnCreate event, assign
Application.OnMessage := AppMessage;

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.12007619.html
http://www.experts-exchange.com/questions/Q.20074880.html
http://www.experts-exchange.com/questions/Q.20090157.html
http://www.experts-exchange.com/questions/Q.20107509.html
http://www.experts-exchange.com/questions/Q.20110418.html
http://www.experts-exchange.com/questions/Q.20153985.html
http://www.experts-exchange.com/questions/Q.20162557.html
http://www.experts-exchange.com/questions/Q.20186199.html
http://www.experts-exchange.com/questions/Q.20257200.html
http://www.experts-exchange.com/questions/Q.20291160.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20186210.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
cjclayton:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
CERTIFIED EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQed without refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
This question is PAQed and no points refunded (of 50)

YensidMod
Expert Exchange Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.