图片

 

//关闭显示器类...
    class CloseMonitor
    {
        //系统消息
        private const uint WM_SYSCOMMAND = 0x112;

 

        //关闭显示器的系统命令  
        private const int SC_MONITORPOWER = 0xF170;

 

        //2为PowerOff, 1为省电状态,-1为开机
        private const int MonitorPowerOff = 2;

 

        //广播消息,所有顶级窗体都会接收
        private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);

 

        //导入API函数库,以下用到的函数SendMessage(...)就在user32.dll文件中
        [System.Runtime.InteropServices.DllImport("user32.dll")]

 

        //此方法的各个参数类型有一些重载,一般还是建议用IntPtr,否则,在64位平台OR其它情况下可能会崩溃
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
  
        //关闭显示器函数
        public static void closeMonitor()  
        {
           
            //SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff);    //GUI程序用这行
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff);   //CUI程序用这行 
        }
    }


本文转载:CSDN博客