C#实现打开或关闭电脑显示器

感觉目前C#是一个为PC机量身定做的一门语言,写PC软件实在是太好用,以下是C#实现打开或关闭电脑显示器
       //引用using System.Runtime.InteropServices;和继承Form这个类
        private const uint WM_SYSCOMMAND = 0x0112;
        private const uint SC_MONITORPOWER = 0xF170;

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, int lParam);

        /// <summary>
        /// 是否打开显示器
        /// </summary>
        /// <param name="isDisplay"></param>
        public void IsDisplay(bool isDisplay)
        {
            if (!isDisplay)
            {
                SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2); //关闭显示器;
                TopMost = true;
                WindowState = FormWindowState.Maximized;
            }

            if (isDisplay)
            {
                SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1); //打开显示器;
            }
        }

评论