【问题标题】:How to detect whether monitor turned off/on? [duplicate]如何检测显示器是否关闭/打开? [复制]
【发布时间】:2015-11-13 12:48:39
【问题描述】:

问题是在文本文件中我看到了所有结果 - TrueNot。 即使我关闭显示器 5-10 秒再打开,文本文件也会显示 True

不知道为什么它没有检测到它。

我试过了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Management;

namespace Detect_Monitor_Settings
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_AWAYMODE_REQUIRED = 0x00000040,
            ES_CONTINUOUS = 0x80000000,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_SYSTEM_REQUIRED = 0x00000001
            // Legacy flag, should not be used.
            // ES_USER_PRESENT = 0x00000004
        }

        StreamWriter w = new StreamWriter(@"e:\monitordetector.txt");

        public Form1()
        {
            InitializeComponent();
        }

        private void DetectScreenName()
        {
           if (stopdetecting == false)
            {
                var query = "select * from WmiMonitorBasicDisplayParams";
                using (var wmiSearcher = new ManagementObjectSearcher("\\root\\wmi", query))
                {
                    var results = wmiSearcher.Get();
                    foreach (ManagementObject wmiObj in results)
                    {
                        // get the "Active" property and cast to a boolean, which should 
                        // tell us if the display is active. I've interpreted this to mean "on"
                        var active = (Boolean)wmiObj["Active"];
                        w.WriteLine(active.ToString());
                    }
                }
            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        bool stopdetecting = false;
        int counttimer1 = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            DetectScreenName();
            counttimer1 += 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            w.Close();
            stopdetecting = true;
            timer1.Stop();
        }
    }
}

【问题讨论】:

  • 我认为你的问题在这里:// tell us if the display is active. I've interpreted this to mean "on"你从哪里得到这个假设?你能提供一些文件吗?
  • SO Q+A 已经很好地介绍了这一点,无需重复。谷歌“wmi detect monitor 打开”找到它们,前五名都是SO问题。足够了解开始寻找另一个项目。

标签: c# .net winforms


【解决方案1】:

根据文档,Active

表示活动监视器。

这并不意味着显示器未处于省电模式。它只是告诉您它是否被禁用 - 通常,这用于检测您是否有多个监视器(例如“活动监视器的数量是否高于 1?”)。

【讨论】:

    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2011-11-10
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    相关资源
    最近更新 更多