You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

编程检测计算机是否连接显示器及相关日志记录方案咨询

编程检测计算机是否连接显示器及相关日志记录方案咨询

Hey there! Let's break down your problem step by step since I've dealt with similar unattended display monitoring scenarios before.

关于Windows事件日志的隐藏记录

First off, you're right that Windows does log display state changes—but they're easy to miss if you don't know where to look. Here's where to find them:

  • Open Event Viewer, navigate to Windows Logs > System
  • Click "Filter Current Log" and set the Event source to Microsoft-Windows-Display
  • Look for event IDs like:
    • 620: A display was connected
    • 621: A display was disconnected
    • 4101: The display driver has successfully recovered from a timeout

Keep in mind though: These events might not trigger for all display loss scenarios (like a sudden power cut to the monitor without unplugging the cable). In those cases, the system might not detect a state change immediately, so you'll need a proactive check.

写脚本定期检测并手动记录日志

If the built-in event logs aren't covering your use case, a script is a solid approach. PowerShell is perfect for this since it's built into Windows and can interact with system APIs easily.

Here's a quick example script that checks connected displays and logs changes to the Event Log:

# Set up a persistent variable to track previous display count
if (-not (Get-Variable -Name PreviousDisplayCount -Scope Global -ErrorAction SilentlyContinue)) {
    $global:PreviousDisplayCount = (Get-CimInstance -ClassName Win32_DesktopMonitor | Where-Object { $_.Availability -eq 3 }).Count
}

# Get current active display count
$currentDisplayCount = (Get-CimInstance -ClassName Win32_DesktopMonitor | Where-Object { $_.Availability -eq 3 }).Count

# Compare and log changes
if ($currentDisplayCount -ne $global:PreviousDisplayCount) {
    $message = if ($currentDisplayCount -gt $global:PreviousDisplayCount) {
        "Display connected: Total active displays now $currentDisplayCount"
    } else {
        "Display disconnected: Total active displays now $currentDisplayCount"
    }

    # Write to Application log (you can create a custom log too)
    if (-not (Get-EventLog -LogName Application -Source "DisplayMonitor" -ErrorAction SilentlyContinue)) {
        New-EventLog -LogName Application -Source "DisplayMonitor"
    }
    Write-EventLog -LogName Application -Source "DisplayMonitor" -EventId 100 -EntryType Information -Message $message

    # Update previous count
    $global:PreviousDisplayCount = $currentDisplayCount
}

You can set this up to run every 5-10 minutes via Task Scheduler:

  • Create a new task with a trigger set to "Daily" and repeat every 5 minutes indefinitely
  • Set the action to run PowerShell with the script path (use -ExecutionPolicy Bypass -File "C:\Path\To\Your\Script.ps1")

其他替代方案

If scripting isn't your thing, here are a few other options:

  • Custom C#/VB.NET App: Use the System.Windows.Forms.Screen.AllScreens property to get active displays, then write status logs to a file or Event Log. This can run as a background service for 24/7 monitoring.
  • Third-Party Monitoring Tools: Tools like Monitorian or DisplayFusion have built-in display status tracking, though you might need to configure them to log changes to a file.
  • Hardware-Level Checks: If your monitor supports it, you could use tools to query the monitor's EDID or power state via DDC/CI commands. This is more advanced but can detect power loss even if the cable is still plugged in.

Hope this helps you get the display monitoring set up smoothly!

备注:内容来源于stack exchange,提问作者nathan lachenmyer

火山引擎 最新活动