How to change your screen resolution using C#
21st August 2008
My laptop gets confused when it goes in or comes out of the docking station and always picks the wrong resolution for the monitor. Part of this might be due to a difference in aspect ratios: the laptop screen is a widescreen model (width:height is 4:3) while the monitor connected to the docking station is 5:4.
I have gotten pretty good at Win-R, desk.cpl<enter>, Alt-R, <right> (or <right><right><right> as the case may be), <enter>, <left><enter> but I would still rather do something quicker.
So this morning I decided to write a bit of C# to handle it. I didn’t know of anything that ships in the .NET Framework to handle this directly so I went off to pinvoke.net and found what I was looking for.
The code example is formatted strangely but they link to a sample on CodeProject that presents it a bit more clearly. It is a bit strange that the class he created changes the resolution in its constructor rather than through a method. It is just a sample, after all.