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?

3

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
4
+50

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.

1 Comment

For completeness, it is possible to find the source code of devcon github.com/Microsoft/Windows-driver-samples in order to include only the needed functionality to the project without using .bat or .cmd file
0

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

3 Comments

How is that relevant?
Because when a new display connected/removed, Windows sends WM_DISPLAYCHANGE message to app; I remember.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.