■ 윈도우즈 화면 잠금 여부와 화면 보호기 실행 여부를 구하는 방법을 보여준다.
▶ WIN32Helper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// WIN32 헬퍼 /// </summary> public static class WIN32Helper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 시스템 매개 변수 정보 처리하기 - SystemParametersInfo(action, parameter1, parameter2, flag) /// <summary> /// 시스템 매개 변수 정보 처리하기 /// </summary> /// <param name="action">액션</param> /// <param name="parameter1">매개 변수 1</param> /// <param name="parameter2">매개 변수 2</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SystemParametersInfo(uint action, uint parameter1, ref bool parameter2, int flag); #endregion #region 데스크톱 열기 - OpenDesktop(desktopName, flag, inherit, desiredAccess) /// <summary> /// 데스크톱 열기 /// </summary> /// <param name="desktopName">데스크톱명</param> /// <param name="flag">플래그</param> /// <param name="inherit">상속 여부</param> /// <param name="desiredAccess">희망 액세스</param> /// <returns>데스크톱 핸들</returns> [DllImport("user32", SetLastError = true)] private static extern IntPtr OpenDesktop(string desktopName, uint flag, bool inherit, uint desiredAccess); #endregion #region 사용자 입력받는 데스크톱 열기 - OpenInputDesktop(flag, inherit, desiredAccess) /// <summary> /// 사용자 입력받는 데스크톱 열기 /// </summary> /// <param name="flag">플래그</param> /// <param name="inherit">상속 여부</param> /// <param name="desiredAccess">희망 액세스</param> /// <returns>데스크톱 핸들</returns> [DllImport("user32", SetLastError = true)] private static extern IntPtr OpenInputDesktop(uint flag, bool inherit, uint desiredAccess); #endregion #region 데스크톱 닫기 - CloseDesktop(desktopHandle) /// <summary> /// 데스크톱 닫기 /// </summary> /// <param name="desktopHandle">데스크톱 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", SetLastError = true)] private static extern IntPtr CloseDesktop(IntPtr desktopHandle); #endregion #region 데스크톱 전환하기 - SwitchDesktop(desktopHandle) /// <summary> /// 데스크톱 전환하기 /// </summary> /// <param name="desktopHandle">데스크톱 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SwitchDesktop(IntPtr desktopHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// DESKTOP_SWITCHDESKTOP /// </summary> private const int DESKTOP_SWITCHDESKTOP = 256; /// <summary> /// SPI_GETSCREENSAVERRUNNING /// </summary> private const int SPI_GETSCREENSAVERRUNNING = 114; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 화면 잠금 여부 구하기 - IsScreenLocked() /// <summary> /// 화면 잠금 여부 구하기 /// </summary> /// <returns>화면 잠금 여부</returns> public static bool IsScreenLocked() { IntPtr desktopHandle = OpenInputDesktop(0, false, DESKTOP_SWITCHDESKTOP); if(desktopHandle == IntPtr.Zero) { desktopHandle = OpenDesktop("Default", 0, false, DESKTOP_SWITCHDESKTOP); } if(desktopHandle != IntPtr.Zero) { if(SwitchDesktop(desktopHandle)) { CloseDesktop(desktopHandle); } else { CloseDesktop(desktopHandle); return true; } } return false; } #endregion #region 화면 보호기 실행 여부 구하기 - IsScreenSaverRunning() /// <summary> /// 화면 보호기 실행 여부 구하기 /// </summary> /// <returns>화면 보호기 실행 여부</returns> public static bool IsScreenSaverRunning() { bool isRunning = false; if(!SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRunning, 0)) { return false; } if(isRunning) { return true; } return false; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Threading; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.Title = "윈도우즈 화면 잠금 여부 구하기/화면 보호기 실행 여부 구하기"; while(true) { Console.WriteLine(); Console.WriteLine($"SCREEN LOCKED : {WIN32Helper.IsScreenLocked() }"); Console.WriteLine($"SCREEN SAVER RUNNING : {WIN32Helper.IsScreenSaverRunning()}"); Thread.Sleep(1000); } } #endregion } } |