첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

TestProject.zip
다운로드

▶ MonitorHelper.cs

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 모니터 헬퍼
    /// </summary>
    public static class MonitorHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter)

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter);

        #endregion
        #region 마우스 이벤트 발생시키기 - mouse_event(flag, deltaX, deltaY, data, extraInformation)

        /// <summary>
        /// 마우스 이벤트 발생시키기
        /// </summary>
        /// <param name="flag">플래그</param>
        /// <param name="deltaX">델타 X</param>
        /// <param name="deltaY">델타 Y</param>
        /// <param name="data">데이터</param>
        /// <param name="extraInformation">부가 정보</param>
        [DllImport("user32.dll")]
        private static extern void mouse_event(int flag, int deltaX, int deltaY, int data, UIntPtr extraInformation);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// WM_SYS_COMMAND
        /// </summary>
        private const int WM_SYS_COMMAND = 0x0112;

        /// <summary>
        /// SC_MONITOR_POWER
        /// </summary>
        private const int SC_MONITOR_POWER = 0xf170;

        /// <summary>
        /// MONITOR_SHUT_OFF
        /// </summary>
        private const int MONITOR_SHUT_OFF = 2;

        /// <summary>
        /// MOUSE_EVENT_FLAG_MOVE
        /// </summary>
        private const int MOUSE_EVENT_FLAG_MOVE = 0x0001;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 모니터 전원 끄기 - MonitorPowerOff()

        /// <summary>
        /// 모니터 전원 끄기
        /// </summary>
        public static void MonitorPowerOff()
        {
            SendMessage((IntPtr)0xffff, WM_SYS_COMMAND, (IntPtr)SC_MONITOR_POWER, (IntPtr)MONITOR_SHUT_OFF);
        }

        #endregion
        #region 모니터 전원 켜기 - MonitorPowerOn()

        /// <summary>
        /// 모니터 전원 켜기
        /// </summary>
        public static void MonitorPowerOn()
        {
            mouse_event(MOUSE_EVENT_FLAG_MOVE, 0, 1, 0, UIntPtr.Zero);

            Thread.Sleep(40);

            mouse_event(MOUSE_EVENT_FLAG_MOVE, 0, -1, 0, UIntPtr.Zero);
        }

        #endregion
    }
}

 

▶ Program.cs

using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            while(true)
            {
                MonitorHelper.MonitorPowerOff();

                Thread.Sleep(5000);

                MonitorHelper.MonitorPowerOn();

                Thread.Sleep(5000);
            }
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요