2009-05-29 143 views
2

我想在Windows環境中調用用戶的屏幕保護程序(如果已定義)。 我知道它可以使用純C++代碼完成(然後在C#中打包非常簡單),如建議here
不過,爲了好奇,我想知道這樣的任務是否可以通過使用dot net framework(2.0及以上版本)的純粹託管代碼完成,不需要p/invoke,也不需要訪問C++端(反過來,可以很容易地使用Windows API)。如何在C#中調用Windows中的屏幕保護程序?

+0

你也可以很容易地從C#中調用Windows API。 – Nate 2009-05-29 20:02:38

+0

重複(有你正在尋找的答案):http://stackoverflow.com/questions/267728/launch-system-screensaver-from-c-windows-form – mockobject 2009-05-29 20:05:19

回答

3

我的想法,我不知道怎麼一直這樣的工作,所以你就需要研究了一下,我認爲,但希望這足以讓你開始。

屏幕保護程序僅僅是一個可執行文件,並存儲在註冊表中該可執行文件的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 
    } 
} 
1

我想有一個.Net庫函數,這是不太可能的 - 我不知道任何。快速搜索返回此代碼項目tutorial其中包含您在問題中提到的託管包裝的示例。

P/invoke存在,因此您可以訪問操作系統特有的功能,其屏幕保護程序就是一個示例。

0

上的任何版本的工作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); 
     } 
    } 
} 
相關問題