这个是网上找的代码,做了一点小修改
using System; using System.Runtime.InteropServices; namespace monitorOff { class Program { private const uint WM_SYSCOMMAND = 0x112; //系统消息 private const int SC_MONITORPOWER = 0xF170; //关闭显示器的系统命令 private const int MonitorPowerOff = 2; //2为PowerOff, 1为省电状态,-1为开机 private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);//广播消息,所有顶级窗体都会接收 [DllImport("user32.dll")] // 引入 API 函数 private static extern bool LockWorkStation();// 这个是调用 windows 的系统锁定 [DllImport("user32.dll")] // 引入 API 函数 private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);// 这个是调用 windows 的关闭显示器 static void Main(string[] args) { LockWorkStation(); SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff); } } }