14

I am using Windows 7. When I plug a new monitor when the PC is running, the monitor is not automatically detected. I must go to the Display->Screen Resolution and click on the Detect button in order to detect the monitor with the registry configuration.

I am searching for a way to do the same programmatically using the Windows API. So I would like to write a program that detects all connected monitors reproducing the Detect button. I tried using ChangeDisplaySettingEx but with bad results. Any suggestions?

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
DOFHandler
  • 225
  • 2
  • 12
  • Maybe http://stackoverflow.com/questions/5981520/detect-external-display-being-connected-or-removed-under-windows-7 can help? – Lyubomyr Shaydariv Jun 11 '15 at 18:51
  • 2
    You already wasted too much time on this. Just replace the video adapter. – Hans Passant Jun 11 '15 at 18:53
  • This Code Project article may be of help: http://www.codeproject.com/Articles/119168/Hardware-Change-Detection Although I'd agree with @HansPassant that it sounds like a hardware problem that it's not worth trying to work around with a software fix! – beercohol Jun 18 '15 at 13:20

3 Answers3

5

Not really a WinApi but try this utility:

C:\Windows\System32\DisplaySwitch.exe

DisplaySwitch.exe /internal - Switch to Primary only
DisplaySwitch.exe /external - Switch to Secondary only
DisplaySwitch.exe /clone - Clone desktop on both screens (Not HDCP compliant!)
DisplaySwitch.exe /extend - Extend desktop to both screens

In particular, try /extend switch, it should perform detection internally.

Eldarien
  • 813
  • 6
  • 10
  • 1
    I had a similar issue - when my laptop wakes, sometimes only the connected monitors turn on but my laptop display stays black. "Detect" in the display properties brings the laptop screen back on, but the Windows taskbar glitches. So I also need to restart the explorer.exe process to fix that. BUT, this approach fixes both issues. I had to run "/internal" to get the laptop screen to be recognized first, and then "/extend" to bring the connected monitors back on. But this is script-able so I won't have to do all that other nonsense. Thanks! – Kory Feb 07 '20 at 16:22
4

Not an API solution, but you can use MS's DevCon utility on the command line to scan for new hardware.

https://support.microsoft.com/en-us/kb/311272

Download it from that page, then just execute this from a cmd window:

devcon.exe rescan

You can easily create a .bat or .cmd file to run this from a link.

beercohol
  • 2,577
  • 13
  • 26
  • For completeness, it is possible to find the source code of devcon https://github.com/Microsoft/Windows-driver-samples in order to include only the needed functionality to the project without using .bat or .cmd file – DOFHandler Sep 18 '15 at 08:10
0

You should read this if you want to learn about display-aware apps.

  • How is that relevant? – CodeCaster Jun 18 '15 at 11:18
  • Because when a new display connected/removed, Windows sends WM_DISPLAYCHANGE message to app; I remember. – AraneaSerket6848 Jun 18 '15 at 12:14
  • If that were so, you should include that in your answer. Link-only answers are bad answers. Anyway OP's problem is that newly connected displays aren't detected, so most likely a `WM_DISPLAYCHANGE` won't be sent too, until OP clicks "Identify" in the Display control panel. – CodeCaster Jun 18 '15 at 12:18