Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • How to detect hot plugging of monitor in a win32 application?
  • Getting an event on monitor hotplug for windows
like image 981
hippietrail Avatar asked May 12 '11 16:05

hippietrail


People also ask

How do I detect an external monitor on Windows 11?

To detect an external monitor manually on Windows 11, use these steps: Open Settings. Click on System. Click the Display page on the right side. Click the Multiple displays setting. Click the Detect button. Once you complete the steps, Windows 11 should now detect the external display.

How to troubleshoot external monitor connections in Windows 10?

Troubleshoot external monitor connections in Windows 10. Before considering troubleshooting options, make sure your device is up to date. Select Start > Settings > Update & Security > Windows Update , then select Check for updates. If you need help setting up your external monitors, see How to use multiple monitors in Windows 10.

How do I set up an external display on Windows 7?

These instructions show you how to configure your external display on your Windows 7 computer. Make sure the projector is turned on, and then plug the projector cable into a video port on your computer. All projector stations in classrooms are equipped with the proper cables. Select how you want your desktop to be displayed:

How do I get my computer to detect my monitor?

How to Get My Computer to Detect My Monitor 1 Open Settings . 2 Click on System . 3 Click on Display . 4 Under the "Rearrange your displays" section, click the Detect button in the bottom-right corner (if applicable). Source: Windows Central. See More....


1 Answers

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.

like image 168
Martin Kutny Avatar answered Sep 30 '22 16:09

Martin Kutny