是否可以通过代码(C#)打开/关闭显示器?
你有没有尝试谷歌搜索?
首先访问:http: //www.codeproject.com/KB/cs/Monitor_management_guide.aspx
我不需要使用Windows提供的一些DLL.
(我猜你需要一个C#解决方案,因为那是你应用的唯一标签).
编辑2013年2月8日:
有人提到该解决方案不再适用于Windows 7和8.这里有一个在Windows 7下运行良好的解决方案,还没有尝试过Windows 8.
http://cocoa.ninja/posts/Turn-off-your-monitor-in-Csharp.html
namespace MonitorOff { public enum MonitorState { MonitorStateOn = -1, MonitorStateOff = 2, MonitorStateStandBy = 1 } public partial class Form1 : Form { [DllImport("user32.dll")] private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam); public Form1() { InitializeComponent(); SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; } void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) { SetMonitorInState(MonitorState.MonitorStateOff); } private void button1_Click(object sender, EventArgs e) { SetMonitorInState(MonitorState.MonitorStateOff); } private void SetMonitorInState(MonitorState state) { SendMessage(0xFFFF, 0x112, 0xF170, (int)state); } } }
按开/关按钮
如果你想在代码中这样做,显然这可以在Win32 API中实现:
SendMessage hWnd,WM_SYSCOMMAND,SC_MONITORPOWER,param
其中WM_SYSCOMMAND = 0x112和SC_MONITORPOWER = 0xF170,param表示将监视器置于的模式:-1:开2:关1:节能模式
hWnd可以是任何窗口的句柄 - 所以如果你有一个表单,这样的东西应该有效
int WM_SYSCOMMAND = 0x112; int SC_MONITORPOWER = 0xF170; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); public static void Main(string[] args) { Form f = new Form(); bool turnOff = true; //set true if you want to turn off, false if on SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(turnOff ? 2 : -1)); }
注意我还没有尝试过这个......
上面的答案/sf/ask/17360801/非常适合关闭Windows 7/8显示器但不能唤醒它.在这些系统上你需要像这样做一些hackish(如/sf/ask/17360801/):
[DllImport("user32.dll")] static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo); private const int MOUSEEVENTF_MOVE = 0x0001; private void Wake(){ mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero); Sleep(40); mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, UIntPtr.Zero); }
此代码可用于打开和关闭..它也适用于Windows 7.
private int SC_MONITORPOWER = 0xF170; private uint WM_SYSCOMMAND = 0x0112; [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); enum MonitorState { ON = -1, OFF = 2, STANDBY = 1 } private void SetMonitorState(MonitorState state) { Form frm = new Form(); SendMessage(frm.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)state); }
要调用该函数,您必须执行以下操作:
SetMonitorState(MonitorState.ON);
要么
SetMonitorState(MonitorState.OFF);
注意:此代码在WPF应用程序中测试.使用以下命名空间:
using System.Runtime.InteropServices; using System.Windows.Forms;
对于谁在控制台应用程序上想要此功能:
using System; using System.Runtime.InteropServices; using System.Timers; namespace TurnScreenOFF { class Program { private static int WM_SYSCOMMAND = 0x0112; private static uint SC_MONITORPOWER = 0xF170; public static void Main(string[] args) { SendMessage(GetConsoleWindow(), WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2); } [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); } }
适应和测试.100%在Windows 8上工作.
我找不到复制粘贴示例,因此我自己创建了一个,不要忘记添加对System.Windows.Forms的引用.
using System; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace monitor_on_off { class Program { [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo); private const int WmSyscommand = 0x0112; private const int ScMonitorpower = 0xF170; private const int MonitorShutoff = 2; private const int MouseeventfMove = 0x0001; public static void MonitorOff(IntPtr handle) { SendMessage(handle, WmSyscommand, (IntPtr)ScMonitorpower, (IntPtr)MonitorShutoff); } private static void MonitorOn() { mouse_event(MouseeventfMove, 0, 1, 0, UIntPtr.Zero); Thread.Sleep(40); mouse_event(MouseeventfMove, 0, -1, 0, UIntPtr.Zero); } static void Main() { var form = new Form(); while (true) { MonitorOff(form.Handle); Thread.Sleep(5000); MonitorOn(); Thread.Sleep(5000); } } } }