Click here to Skip to main content
Licence CPOL
First Posted 11 Aug 2009
Views 15,067
Downloads 455
Bookmarked 12 times

Set Primary Display (ChangeDisplaySettingsEx)

By | 11 Aug 2009 | Article
Simplified code for setting the primary display on a multi-monitor PC.

Introduction

I've seen plenty of "complicated" code for setting the "primary" display in a multiple display PC environment, but never a "simple" C# version. (This code is based off plenty of samples I've seen on the web.)

Background

This code originated out of the need to pop applications to multiple monitors during an automated start-up script (i.e., set a designated monitor as primary, start apps, then revert back to original monitor).

Using the code

The code is left raw for addition of "bells and whistles".

//manual gather - NewPrimary name ----------------------------------
WinApi.DISPLAY_DEVICE ddOne = new WinApi.DISPLAY_DEVICE();

ddOne.cb = Marshal.SizeOf(ddOne);
deviceID = 1;
WinApi.User_32.EnumDisplayDevices(null, deviceID, ref ddOne, 0);
string NewPrimary = ddOne.DeviceName;

WinApi.DEVMODE ndm6 = NewDevMode();
result = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(NewPrimary, 
          ref ndm6, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_SET_PRIMARY | 
          (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
Console.WriteLine("Action 3.2 result:" + result.ToString());

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

beyonder422

Systems Engineer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDoes not work for me. PinmemberBill Langlais13:02 2 Nov '09  
GeneralManually set monitor\display name Pinmemberbeyonder4225:48 11 Aug '09  
GeneralRe: Manually set monitor\display name PinmemberThe_Mega_ZZTer6:51 11 Aug '09  
GeneralRe: Manually set monitor\display name Pinmemberdalek92:23 25 Aug '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 11 Aug 2009
Article Copyright 2009 by beyonder422
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid