检测在 Windows 7 下连接或移除的外接显示器 [英] Detect external display being connected or removed under Windows 7

查看:45
本文介绍了检测在 Windows 7 下连接或移除的外接显示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次在运行 Windows 7 的笔记本电脑上插入或拔出外部 LCD 显示器时,我是否可以接收或挂接某些事件或通知?

Is there some event or notification I can receive or hook each time an external LCD monitor is plugged in or unplugged from a laptop running Windows 7?

笔记本电脑检测到这一点并将我的显示器切换到外部屏幕,然后通过某些类型的调整大小或重新定位,但这是由操作系统公开的,以便应用程序可以提供处理程序、附加脚本等?

The laptop detects this and switches my display to the external screen and back with certain kinds of resizing or repositioning but is this exposed by the operating system so that applications can provide a handler, attach a script, etc?

如果没有,是否有我可以不时轮询的注册表设置或 API?

If not, is there a registry setting or API I could poll from time to time?

(我更喜欢编程 C + Win32 API)

(I prefer programming C + Win32 API)

更新

Mike 的回答如下,WM_DEVICECHANGE 引导我RegisterDeviceNotification(),但到目前为止我正在努力实现它......

Mike's answer below, WM_DEVICECHANGE led me to RegisterDeviceNotification(), but I'm struggling to implement it so far...

更新 2

这个问题已经被问过几次了,但我认为还没有完全回答:

This question has been asked with different wording a couple of times, but not fully answered yet in my opinion:

推荐答案

根据 这篇文章 Windows 会在显示分辨率更改以及添加或删除显示器时发送 WM_DISPLAYCHANGE 消息.

According to this article Windows sends the WM_DISPLAYCHANGE message when display resolution changes and also when a display is added or removed.

如果您需要对由于显示器添加或删除而导致的桌面大小变化做出反应,您可以在此消息的处理程序中执行此操作.LPARAM 为您提供窗口所在显示器的新分辨率.请注意,如果您使用 100% 以外的任何其他内容进行系统 DPI 缩放并且您的程序不是 DPI 感知.

If you need to react to desktop size changes due to monitor addition or removal, you can do so in the handler of this message. The LPARAM gives you the new resolution of the display on which the window is located. Notice that this resolution will be scaled if you use anything else than 100% for system DPI scaling and your program is not DPI-aware.

或者使用EnumDisplayMonitors 函数来获取每个连接的显示器的显示分辨率以及显示器在虚拟桌面中的相对位置.无论 DPI 缩放如何,此函数都使用真实设备像素值.

Alternatively use the EnumDisplayMonitors function to get the display resolution for each connected monitor and the relative positions of the monitors in the virtual desktop. This functions uses the real device pixel values regardless of DPI scaling.

这篇关于检测在 Windows 7 下连接或移除的外接显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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