我想在Windows环境中调用用户的屏幕保护程序(如果已定义)。 我知道这可以使用纯C ++代码(然后在C#中的包装非常简单),如这里所build议的。
不过,为了好奇,我想知道这样的任务是否可以通过纯粹的托pipe代码使用点networking框架(版本2.0及以上),没有p / invoke和没有访问C + +端(反过来,可以很容易使用Windows API)。
我有一个想法,我不确定这是多么一贯的工作,所以你需要研究一下我想,但希望这足以让你开始。
屏幕保护程序只是一个可执行文件,并且注册表存储此可执行文件的位置在HKCU\Control Panel\Desktop\SCRNSAVE.EXE
在我的Vista副本,这对我工作:
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 } }
用C#控制屏幕保护程序 。
我想有一个.Net库函数,这是不太可能的 – 我不知道有任何。 快速搜索返回此代码项目教程 ,其中包含您在问题中提到的托管包装的示例。
存在P / invoke,以便您可以访问特定于操作系统的功能,其中屏幕保护程序就是一个示例。
我不确定你可以使用完全托管的代码来做到这一点。
这使用Windows API,但仍然非常简单: 从C#Windows窗体启动系统屏幕保护程序
在任何版本的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); } } }