我想要援引的用户的屏幕保护程序如果是这样的定义,在Windows环境。

我知道这可以通过使用纯C++代码(然后缠绕在C#是很简单的),作为建议 在这里,.

仍然,出于好奇,我想知道,如果这样的任务,可以通过纯粹的管理的码的使用点净框架(2.0版本和以上),而不p/调用并没有访问的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
    }
}

其他提示

我觉得有NET库函数,它这是极不可能的 - 我不知道有任何。快速搜索返回此代码项目教程其中包含托管包装的例子你在你的问题中提到。

P /调用的存在使得你能够访问特定于操作系统的功能,其中的屏幕保护程序是一个例子。

我不知道,你可以完全使用托管代码来做到这一点。

本使用Windows API,但还是很简单的:从C#启动系统屏幕保护程序视窗表单

在任何版本的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);
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top