23

Is there a way to programmatically change the screen resolution or enable/disable multiple monitors in Windows XP? For example to change from 1024x768 with one monitor to 1280x1024 on two monitors? I would be most interested in a win32 function to do this but anything that can be tied to a windows shortcut would suffice.

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
jacobsee
  • 1,438
  • 4
  • 18
  • 34
  • 8
    I trust you would not even think of doing that without permission from the user. If you fiddled with my screen resolution, I'd be entirely pissed off with you - and would probably not use your program a second time. – Jonathan Leffler Oct 19 '08 at 06:27
  • I totality agree with the previous commenter. Unless this was some sort of utility for managing powerpoint presentations, it is hard to imagine an app where this would be a useful function. – David L Morris Oct 19 '08 at 06:54
  • That was "I totally agree... " – David L Morris Oct 19 '08 at 08:14
  • 8
    I'm looking to do this for myself -- as a convenience for when I remote into a system – jacobsee Oct 20 '08 at 22:45
  • 1
    That was **exactly** the reason why I was looking for this. – Juan Mar 14 '11 at 00:13

4 Answers4

33

You can use EnumDisplayDevices to figure out what displays you have available and EnumDisplaySettings to get a list of available resolutions for your displays. Use ChangeDisplaySettings to set the resolution you need.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • 2
    I found a great [working implementation of these APIs using C#](https://www.codeproject.com/Tips/702664/Change-Resolution-Before-Starting-Application). Easy to create a shortcut for it. If you don't want to run a program after changing the resolution, simply remove the few lines of code that the author included to start a program. In my case, I use it to lower my 4k screen resolution down to 1920x1080 before sharing my screen on WebEx. After I'm done, I have a different shortcut to reset back to max resolution. – Bill Aug 15 '19 at 05:07
3

Yes, but its not part of .NET. You will need to use, invoke or write a wrapper to access the Win32 API.

See ChangeDisplaySettings and related function.

Here you can find a basic example.

learnAsWeGo
  • 2,252
  • 2
  • 13
  • 19
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
0

To change the display resolution for the primary display:

import win32api
import win32con
import pywintypes

devmode = pywintypes.DEVMODEType()
devmode.PelsWidth = 1920
devmode.PelsHeight = 1080

devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettings(devmode, 0)

For a python script offering selection of different resolutions, see https://github.com/randyramsaywack/changeResolution.

koppor
  • 19,079
  • 15
  • 119
  • 161
-3

You can easily script this with http://www.autohotkey.com

Here's a script for swapping between one monitor and two monitors with Windows+1 and Windows+2

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Recommended for catching common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#1::
Send {LWin}
WinWaitActive Start menu
Send Adjust Screen Resolution
Send {enter}
WinWaitActive Screen Resolution
ControlClick ComboBox3
Send {PgDn}
Send {Up} ; Select "Show desktop only on 1"
Send {enter}
Sleep 3000 ; workaround - cannot select accept/revert window?
Send {left}
Send {enter} ; accept changes
Return
#2::
Send {LWin}
WinWaitActive Start menu
Send Adjust Screen Resolution
Send {enter}
WinWaitActive Screen Resolution
ControlClick ComboBox3
Send {PgDn}
Send {Up}
Send {Up} ; Select "Extend these displays"
Send {enter}
Sleep 3000 ; workaround - cannot select accept/revert window?
Send {left}
Send {enter} ; accept changes
Return
a7drew
  • 7,801
  • 6
  • 38
  • 39