■ 화면 보호기/절전 모드를 방지하거나 허용하는 방법을 보여준다.
▶ ExecutionState.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 |
using System; namespace TestProject { /// <summary> /// 실행 상태 /// </summary> [FlagsAttribute] public enum ExecutionState : uint { /// <summary> /// ES_AWAYMODE_REQUIRED /// </summary> ES_AWAYMODE_REQUIRED = 0x00000040, /// <summary> /// ES_CONTINUOUS /// </summary> ES_CONTINUOUS = 0x80000000, /// <summary> /// ES_DISPLAY_REQUIRED /// </summary> ES_DISPLAY_REQUIRED = 0x00000002, /// <summary> /// ES_SYSTEM_REQUIRED /// </summary> ES_SYSTEM_REQUIRED = 0x00000001 } } |
▶ SleepModeHelper.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 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 절전 모드 헬퍼 /// </summary> public static class SleepModeHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 스레드 실행 상태 설정하기 - SetThreadExecutionState(state) /// <summary> /// 스레드 실행 상태 설정하기 /// </summary> /// <param name="state">실행 상태</param> /// <returns>실행 상태</returns> [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern ExecutionState SetThreadExecutionState(ExecutionState state); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 절전 모드 방지하기 - Prevent() /// <summary> /// 절전 모드 방지하기 /// </summary> public static void Prevent() { SetThreadExecutionState ( ExecutionState.ES_CONTINUOUS | ExecutionState.ES_SYSTEM_REQUIRED | ExecutionState.ES_AWAYMODE_REQUIRED | ExecutionState.ES_DISPLAY_REQUIRED ); } #endregion #region 절전 모드 허용하기 - Allow() /// <summary> /// 절전 모드 허용하기 /// </summary> public static void Allow() { SetThreadExecutionState(ExecutionState.ES_CONTINUOUS); } #endregion } } |
▶ MainForm.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 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.button.Click += button_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, EventArgs e) { if(this.button.Text == "방지하기") { SleepModeHelper.Prevent(); this.button.Text = "허용하기"; this.messageLabel.Text = "화면 보호기/절전 모드 방지중"; } else { SleepModeHelper.Allow(); this.button.Text = "방지하기"; this.messageLabel.Text = "화면 보호기/절전 모드 허용중"; } } #endregion } } |