伊人博客 一款有技术又有生活的博客

Windows锁定屏幕然后关闭显示器

发布时间:2017-03-31 18:01:48 标签: | 阅读(2754)

有时候我们需要关闭屏幕来休息一下或者在本上写东西,但是屏幕亮着的时候会分心,但是关闭显示器又太麻烦了,所以直接来一个小程序(非微信小程序)。还有一种情况,有时候晚上要离开电脑旁了,但是电脑还在做事情,不想关电脑,又因为晚上黑乎乎的,屏幕亮着会被骂的,综合以上情况,这端代码非常适合你。

如果你不会编译代码,没关系,我这里有编译好的可执行文件,下载链接http://www.yxxrui.cn/article/71.shtml

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace LockAndCloseWindow
{
    class Program
    {
            //系统消息
        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 WND_BROADCAST = new IntPtr(0xffff);
        static void Main(string[] args)
        {
            LockWorkStation();//锁定屏幕
            System.Threading.Thread.Sleep(10);
            SendMessage(HWND_BROADCAST,
                  WM_SYSCOMMAND,
                  SC_MONITORPOWER,
                  MonitorPowerOff);
            //关闭显示器
        }
        //锁定屏幕
        [DllImport("user32.dll")]
        public static extern bool LockWorkStation();
        //关闭显示器
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(
                IntPtr hWnd, 
                uint Msg, 
                int wParam, 
                int lParam);
    }
}
原创文章如转载,请注明出处“伊人博客

站内搜索
关注公众号(随时搜索)
文章归档
热门推荐
随机推荐
冷门晾晒