打开/关闭监视器 [英] Turn on/off monitor

查看:36
本文介绍了打开/关闭监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过代码 (C#) 以编程方式打开/关闭显示器?

Is it programmatically possible to turn a monitor on/off through code (C#)?

推荐答案

你是否尝试过谷歌搜索?

Did you even try googling it?

第一次命中:http://www.codeproject.com/KB/cs/Monitor_management_guide.aspx

您需要使用 Windows 提供的某些 DLL,我并不感到惊讶.

I am not surprised you need to use some DLL's supplied by Windows.

(我猜您需要一个 C# 解决方案,因为这是您应用的唯一标签).

(I guessed you needed a C# solution, because that's the only tag you applied).

编辑 2013 年 2 月 8 日:

有人提到该解决方案在 Windows 7 en 8 下不再有效.这是一个在 Windows 7 下运行良好的解决方案,还没有尝试过 Windows 8.

It was mentioned that the solution no longer worked under Windows 7 en 8. Well here is one that works nicely under Windows 7, haven't tried Windows 8 yet.

http://cocoa.ninja/posts/Turn-关闭你的显示器在 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);
        }
    }
}

这篇关于打开/关闭监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆