Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

winapi - Detect external display being connected or removed under Windows 7

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...