Mega Code Archive

 
Categories / Delphi / System
 

Invoke Windows Screen Saver in Code

Title: Invoke Windows Screen Saver in Code Question: How to display/invoke the Windows screen saver programatically Answer: There are two ways: The first which is the neatest solution is to use the WinAPI call PostMessage(GetDesktopWindow, WM_SYSCOMMAND, SC_SCREENSAVE, 0) The second involves running the program file for the screen saver which has a .SCR extension. See running other applications elsewhere for this. This method however does not invoke any password security if set on the screen saver. I wanted a tiny app that I could click to start my screen saver and invoke password security (effectively locking my desktop [without Ctrl-Alt-Del] with my screen saver running) and thus created the following .dpr file: program SaveScrn; uses Forms, Windows, Messages; {$R *.RES} var b: Bool; begin Application.Initialize; { 1st check that a screen saver exists} if (SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, @b, 0) and (b) then begin { Invoke Screen saver} PostMessage(GetDesktopWindow, WM_SYSCOMMAND, SC_SCREENSAVE, 0); end; Application.Terminate; end. Now I just run the application and my desktop is safe from prying eyes.