Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

ChangeDisplaySettingsEx (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);
static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam);

VB.NET

<DllImport("user32.dll")> _
Private Shared Function ChangeDisplaySettingsEx(ByVal lpszDeviceName As String, ByRef lpDevMode As DEVMODE, ByVal hwnd As IntPtr, ByVal dwflags As UInteger, ByVal lParam As IntPtr) As DISP_CHANGE
End Function

User-Defined Types:

DISP_CHANGE

DEVMODE

ChangeDisplaySettingsFlags

Notes:

See also ChangeDisplaySettings

Tips & Tricks:

To change the postion of a secondary device it is very important to use dmPositionX and dmPositionY from DEVMODE (see structs).

First i tried it with a PointL struct dmPosition.x and dmPosition.y until i luckily found this page:

http://msdn2.microsoft.com/en-us/library/aa719104(vs.71).aspx

Sample Code:

You can not enable and set size in one call. If the second monitor is not enabled you must first call ChangeDisplaySettingsEx with dmFields = DM.Position, with dmPosition set something other than (0,0). Once enabled you can call ChangeDisplaySettingsEx again with dmFields = DM.PelsHeight | DM.PelsWidth | DM.Position to set position and size. To disable the monitor call ChangeDisplaySettingsEx with dmFields=DM.Position, with dmPosition set to (0,0). Disabling will not work if dmFields = DM.PelsHeight | DM.PelsWidth | DM.Position.

DISPLAY_DEVICE d = new DISPLAY_DEVICE();
DEVMODE dm = new DEVMODE();
d.cb = Marshal.SizeOf(d);
int deviceID=1; // This is only for my device setting. Go through the whole EnumDisplayDevices to find your device  
EnumDisplayDevices(null,deviceID, ref  d, 0); //
EnumDisplaySettings(d.DeviceName, 0, ref dm);
dm.dmPelsWidth = 1024;
dm.dmPelsHeight = 768;
dm.dmPositionX = Screen.PrimaryScreen.Bounds.Right;
dm.dmFields = DM.Position | DM.PelsWidth | DM.PelsHeight;
ChangeDisplaySettingsEx(d.DeviceName, ref dm, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero);

Alternative Managed API:

Do you know one? Please contribute it!

Sample Code:

DISPLAY_DEVICE d = new DISPLAY_DEVICE();
DEVMODE dm = new DEVMODE();
d.cb = Marshal.SizeOf(d);
int deviceID=1; // This is only for my device setting. Go through the whole EnumDisplayDevices to find your device  
EnumDisplayDevices(null,deviceID, ref  d, 0); //
EnumDisplaySettings(d.DeviceName, 0, ref dm);
dm.dmPelsWidth = 1024;
dm.dmPelsHeight = 768;
dm.dmPositionX = Screen.PrimaryScreen.Bounds.Right;
dm.dmFields = DM.Position | DM.PelsWidth | DM.PelsHeight;
ChangeDisplaySettingsEx(d.DeviceName, ref dm, IntPtr.Zero, CDS_UPDATEREGISTRY, IntPtr.Zero);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions