Detect external display being connected or removed under Windows 7
Asked Answered
P

3

10

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?

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

(I prefer programming C + Win32 API)

UPDATE

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

UPDATE 2

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

Pervasive answered 12/5, 2011 at 16:47 Comment(2)
I was thinking I would use AutoHotkey for Windows, to maybe detect resolution change. Or something like this script - autohotkey.com/board/topic/59846-detect-resolution-change - which I think detects general display change.Mel
there is code here how to use WM_DEVICECHANGE and registerdevicenotification silabs.com/community/interface/knowledge-base.entry.html/2013/…Peoples
G
6

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

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.

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.

Guss answered 20/8, 2015 at 9:35 Comment(1)
I've just tested WM_DISPLAYCHANGE with adding and removing a monitor, and it works fine on Windows 10 (though the original question was asking about Windows 7).Equidistant
H
3

You can try WM_DEVICECHANGE. If that doesn't do the trick, run your window and attach Spy++ to it which will log all the window messages it receives. Then plug your monitor in and check if you received any messages.

Alternatively you can poll GetSystemMetrics() with SM_CMONITORS.

Husha answered 12/5, 2011 at 16:50 Comment(2)
Thanks Mike. WM_DEVICECHANGE can't do it on its own since it only tells you that something was added or removed and nothing else. But its docs directed me to RegisterDeviceNotification() which seems to be the one. Not easy to understand though and all the examples I can find are for USB but still trying...Pervasive
This is a really old answer but I'll comment anyway - you could just listen for this message to indicate what had changed, then use EnumDisplayMonitors (or something similar, it's not the only tool for this job) to loop over every available monitor.Devries
C
0

As said here:

You will see registered messages "UxdDisplayChangeMessage" and "HotplugDetected" (second one only when adding monitor). You can use RegisterWindowMessage to get the identifier for these messages.

There are also other messages you can check, just see the linked answer.

Consalve answered 17/11, 2015 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.