Extending the desktop (via a script)
Published August 26, 2006 by Robbie
Over the last few months, several articles have been written about the productivity boost associated with having two (or more) monitors (e.g., NYTimes). I’ve had dual monitors at work and home for two months and I love it. Extending the desktop environment over much larger screen real estate means you don’t have to constantly switch between various applications. I can have my email, IM, or browsers up in one monitor while I read a document or code in an IDE in another.
One pain associated with doing this on Windows XP is the fact that you have to go into the Display Properties, select the second montior, and check “Extend my Windows desktop onto this monitor.”. This is a pain because sometimes when I undock my laptop, it won’t recognize that the second monitor isn’t attached. And even if it does recognize that it is disconnected, when I hook up to the docking station again I have to go back into the properties and re-check the “extend my desktop” box.
I figured there had to be a way to script this setting. I found the ChangeDisplaySettingsEx API, but it wasn’t easily scriptable. Next I looked at simulating GUI movements. A quick Google search led me to AutoIt, which looked like exactly what I needed. Plus it is free!
AutoIt was very easy to install and use. It only took me a couple minutes to create this script which will toggle the ‘extend my desktop’ box:
Run("C:\WINDOWS\system32\control.exe desk.cpl,@0,3")
WinWaitActive(”Display Properties”)
Send(”{TAB}”)
Send(”{DOWN}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{SPACE}”)
Send(”{ENTER}”)
It’s a hack, but it works great. Even better than that, AutoIt has an option to compile scripts to executables. This won’t work for everyone, but here is my compiled script.
This post has been viewed 5,427 times
Save to del.icio.us
|
Digg it
|
Reddit

You rule!
I was searching on the web for the same thing, found AutoIt, but couldn’t figure out how to run the control panel. Thanks!
quick. easy. thanks
That’s good stuff right there — exactly what I needed for my new MacBook Pro.
For the easily confused, save what Robbie described in a file on your desktop with a name like DualScreen.au3. If you have autoit installed, all you need to do to switch monitor mode is to double click on this file.
Wonderful tip, thank you. I couldn’t get the script working on Vista, although it gave me a great head start, thank you! Apparently the display settings tab sequence has changed on Vista, however the below script will enable dual-monitors just fine. I’ve one problem left to solve, and that’s using the same script to *toggle* the monitors; I want to click on the same script to both enable and disable dual screens. Ideas appreciated!
;;;;;;;;;;;;;;;;;
Run(”C:\WINDOWS\system32\control.exe desk.cpl,@0,3″)
; Wait for the window to appear and become active
WinWaitActive(”Display Properties”, “”, 2)
Send(”{TAB}”)
Send(”{DOWN}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{SPACE}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{ENTER}”)
; Now confirm the dialog box
Send(”{TAB}”)
Send(”{ENTER}”)
; Close the script
Exit
;;;;;;;;;;;;;;;;;
OMG! OMG! OMG!
much love to you! no, seriously..i`ve been looking for ways to do this for weeks. i initially though i`d be able to do it via WSH/JScript/VBScript..but after many, may tries it was having none of it!
so yeh…for some reason some games i play need me to disable my 2nd screen….and it`s a major pain doing the GUI all the time, so i`m just gonna pop off and try this :)
thanks!
OMG!^5
just played about for like 2mins and i got a toggle-ing script, i done it ‘backwards’ as if it`s disabled to start with then the ‘make primary’ is disabled..so working backwards will (well..it should nearly always) work :)
;
; — toggle-screen.au3
;
; exec cpanel app `display settings`
Run(”C:\WINDOWS\system32\control.exe desk.cpl,@0,3″)
; wait for window to be active
WinWaitActive(”Display Properties”)
; select 2nd display
Send(”{TAB}”)
Send(”{DOWN}”)
; work back to the ‘extend desktop’ control
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
; toggle ‘extend desktop’ control and apply
Send(”{SPACE}”)
Send(”{ENTER}”)
;
; — E.O.F.
;
Thanks!!
I was doing this manually every day…
until I Googled it 2 minutes ago!
Great work,
Nico.
Wonderful! That sure saves me a huge pain in the ass!
Had to post thanks also - good hack to a pain in the ar*e procedure just to get another monitor on….
u rock
Fantastic! Exactly what I was looking for. I had been manually doing this. Total time (including downloading AutoIT was 15 minutes).
I did have problems with cutting and paste on the scripts. The double quotes were being read wrong. I re-moved and re-added them to the scripts and everything started working.
Eureka! I finally found the answer! (And the correct Google seach terms - ’script extend desktop’). I can’t tell you how much time I’ve wasted trying to learn windows scripting or some other batch language to get my computer to automate that one simple task. My thinking was, if I am repeating the same steps to do the same thing every day, then I should be able to automate it. Thank you very much!
P.S. Your script worked on my PC straight away - but I’m downloading AutoIt so I can lean how to write my own scripts.
Thanks… rewrote it in java.
Awesome. Call me lazy but this is exactly what I wanted.
Very helpful.
And if you need to move your secondary display to another position (mine is on the left) you can do it like that:
; set mouse coords to relative coords for the active window
Opt(”MouseCoordMode”, 0)
; drag 2nd screen to the left
MouseClickDrag(”left”, 250, 150, 50, 150)
You might need to adapt the coordinates to your needs.
If you have an nvidia card(s) installed, this single command will extend your desktop to two screens: (you can put it in a cmd file and stick that on your desktop if you want to)
rundll32.exe nvcpl.dll,dtcfg setview 1 dualview
If your start bar shows up on the wrong screen, run these two commands to move it to the other screen:
rundll32.exe nvcpl.dll,dtcfg setview 2 normal
rundll32.exe nvcpl.dll,dtcfg setview 2 dualview
You can also change resolutions with nvcpl.dll,dtcfg - just search for “nvcpl.dll,dtcfg setwiew” on google.
Hi,
well, it seems like you guys talk about and have exactly what I need ! A little script to TURN ON / TURN OFF a secondary monitor. However, the .exe I downloaded from the to of this pages doesn’t work in Vista and I am not a scripter, so I don’t know how to make this work.
I got Vista Ultimate on my Notebook and have a second monitor attached. I’d like to be able to enable/disable the secondary mnitor with ONE cick ! Is this possible and what do I need to do ?
Thank you.
PS: Besides posting here, please send me also an e-mail to jce2005@gmail.com letting me know. Thanks,
Tom
Tweaked the previous posts for Vista, tested on Vista Ultimate x64 SP1 and AutoIT 3 but should work with all versions of Vista. Same command toggles Dualview on and off. :)
Thanks to the previous posters, hope damn nVIDIA fixes the stupid nvcpl.dll for vista so that we can switch with a simple command line.
;
; — toggle-screen.au3
;
; exec cpanel app `display settings`
Run(”C:\WINDOWS\system32\control.exe desk.cpl,@0,3?”)
; wait for window to be active
WinWaitActive(”Display Settings”)
; select 2nd display
Send(”{TAB}”)
Send(”{DOWN}”)
; work back to the ‘extend desktop’ control
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
Send(”+{TAB}”)
; toggle ‘extend desktop’ control and apply
Send(”{SPACE}”)
Send(”{ENTER}”)
; wait for window to be active
WinWaitActive(”Display Settings”)
; accept
Send(”{TAB}”)
Send(”{ENTER}”)
;
; — E.O.F.
;