How to call a screen saver in Windows in C #?

I would like to call the user's screen saver, if one is defined in the Windows environment. I know that this can be done using pure C ++ code (and then wrapping in C # is pretty simple) as suggested here .
However, for curiosity, I would like to know whether such a task can be performed using purely managed code using the dot net grid (version 2.0 and higher), without p / invoke and without visiting the C ++ side (which, in in turn, can use the windows API quite easily).

+2
c # windows managed screensaver
source share
5 answers

I have an idea, I'm not sure how consistent this will work, so you need to learn a little, but I hope this is enough to get you started.

A screensaver is just an executable file, and the registry stores the location of this executable file in HKCU\Control Panel\Desktop\SCRNSAVE.EXE

On my copy of Vista, this worked for me:

 RegistryKey screenSaverKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop"); if (screenSaverKey != null) { string screenSaverFilePath = screenSaverKey.GetValue("SCRNSAVE.EXE", string.Empty).ToString(); if (!string.IsNullOrEmpty(screenSaverFilePath) && File.Exists(screenSaverFilePath)) { Process screenSaverProcess = Process.Start(new ProcessStartInfo(screenSaverFilePath, "/s")); // "/s" for full-screen mode screenSaverProcess.WaitForExit(); // Wait for the screensaver to be dismissed by the user } } 
+3
source share
+1
source share

I think having a .Net library function that does this is unlikely - I don't know anyone. A quick search returned this draft code tutorial , which contains an example of the managed wrapper that you mentioned in your question.

P / invoke exists, so you can access OS-specific functions, such as screen savers.

+1
source share

I'm not sure if you can use fully managed code for this.

This uses the Windows API, but still very simple: Launch a system splash screen from C # Windows Form

0
source share

Work with any version of windows ...

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace HQ.Util.Unmanaged { public class ScreenSaverHelper { [DllImport("User32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] private static extern IntPtr GetDesktopWindow(); // Signatures for unmanaged calls [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool SystemParametersInfo(int uAction, int uParam, ref int lpvParam, int flags); // Constants private const int SPI_GETSCREENSAVERACTIVE = 16; private const int SPI_SETSCREENSAVERACTIVE = 17; private const int SPI_GETSCREENSAVERTIMEOUT = 14; private const int SPI_SETSCREENSAVERTIMEOUT = 15; private const int SPI_GETSCREENSAVERRUNNING = 114; private const int SPIF_SENDWININICHANGE = 2; private const uint DESKTOP_WRITEOBJECTS = 0x0080; private const uint DESKTOP_READOBJECTS = 0x0001; private const int WM_CLOSE = 16; public const uint WM_SYSCOMMAND = 0x112; public const uint SC_SCREENSAVE = 0xF140; public enum SpecialHandles { HWND_DESKTOP = 0x0, HWND_BROADCAST = 0xFFFF } public static void TurnScreenSaver(bool turnOn = true) { // Does not work on Windows 7 // int nullVar = 0; // SystemParametersInfo(SPI_SETSCREENSAVERACTIVE, 1, ref nullVar, SPIF_SENDWININICHANGE); // Does not work on Windows 7, can't broadcast. Also not needed. // SendMessage(new IntPtr((int) SpecialHandles.HWND_BROADCAST), WM_SYSCOMMAND, SC_SCREENSAVE, 0); SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, (IntPtr)SC_SCREENSAVE, (IntPtr)0); } } } 
0
source share

All Articles