C#에서 Windows의 화면 보호기를 호출하는 방법은 무엇입니까?
-
06-09-2019 - |
문제
Windows 환경에서 정의된 경우 사용자의 화면 보호기를 호출하고 싶습니다.
제안된 대로 순수 C++ 코드를 사용하여 수행할 수 있다는 것을 알고 있습니다(C#의 래핑은 매우 간단합니다). 여기.
그래도 호기심을 위해 p/invoke나 C++ 측 방문 없이 닷넷 프레임워크(버전 2.0 이상)를 사용하여 순수하게 관리되는 코드로 이러한 작업을 수행할 수 있는지 알고 싶습니다. 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/invoke가 존재하여 어떤 화면 보호기가 예를 들어 어떤 스크린 보호기에 액세스 할 수 있도록 존재합니다.
완전히 관리 된 코드를 사용하여이를 수행 할 수 있는지 확실하지 않습니다.
이것은 Windows API를 사용하지만 여전히 매우 간단합니다. C# Windows 양식에서 System Screensaver를 시작합니다
모든 버전의 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);
}
}
}