DevMode (Structures)
Last changed: shtolliver-47.227.243.115

.
Summary
Contains information about the initialization and environment of a printer or a display device.

C# Definition:

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
     public const int CCHDEVICENAME = 32;
     public const int CCHFORMNAME   = 32;

     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
     public string dmDeviceName;
     public short  dmSpecVersion;
     public short  dmDriverVersion;
     public short  dmSize;
     public short  dmDriverExtra;
     public int    dmFields;

     public short  dmOrientation;
     public short  dmPaperSize;
     public short  dmPaperLength;
     public short  dmPaperWidth;

     public short  dmScale;
     public short  dmCopies;
     public short  dmDefaultSource;
     public short  dmPrintQuality;
     public short  dmColor;
     public short  dmDuplex;
     public short  dmYResolution;
     public short  dmTTOption;
     public short  dmCollate;
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
     public string dmFormName;
     public short  dmLogPixels;
     public int    dmBitsPerPel;    // Declared wrong in the full framework
     public int    dmPelsWidth;
     public int    dmPelsHeight;
     public int    dmDisplayFlags;
     public int    dmDisplayFrequency;

     public int    dmICMMethod;
     public int    dmICMIntent;
     public int    dmMediaType;
     public int    dmDitherType;
     public int    dmReserved1;
     public int    dmReserved2;
     public int    dmPanningWidth;
     public int    dmPanningHeight;

     public int    dmPositionX; // Using a PointL Struct does not work
     public int    dmPositionY;
}

VB.NET Definition:

<StructLayout(LayoutKind.Sequential)> _
Public Structure DEVMODE
    Public Const CCHDEVICENAME As Integer = 32
    Public Const CCHFORMNAME As Integer = 32

    <MarshalAs(UnmanagedType.ByValTStr, SizeConst := CCHDEVICENAME)> _
    Public dmDeviceName As String
    Public dmSpecVersion As Short
    Public dmDriverVersion As Short
    Public dmSize As Short
    Public dmDriverExtra As Short
    Public dmFields As Integer

    Public dmOrientation As Short
    Public dmPaperSize As Short
    Public dmPaperLength As Short
    Public dmPaperWidth As Short

    Public dmScale As Short
    Public dmCopies As Short
    Public dmDefaultSource As Short
    Public dmPrintQuality As Short
    Public dmColor As Short
    Public dmDuplex As Short
    Public dmYResolution As Short
    Public dmTTOption As Short
    Public dmCollate As Short
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst := CCHFORMNAME)> _
    Public dmFormName As String
    Public dmLogPixels As Short
    Public dmBitsPerPel As Integer
    ' Declared wrong in the full framework
    Public dmPelsWidth As Integer
    Public dmPelsHeight As Integer
    Public dmDisplayFlags As Integer
    Public dmDisplayFrequency As Integer

    Public dmICMMethod As Integer
    Public dmICMIntent As Integer
    Public dmMediaType As Integer
    Public dmDitherType As Integer
    Public dmReserved1 As Integer
    Public dmReserved2 As Integer
    Public dmPanningWidth As Integer
    Public dmPanningHeight As Integer

    Public dmPositionX As Integer
    ' Using a PointL Struct does not work
    Public dmPositionY As Integer
End Structure

Notes:

C# add:

using System.Runtime.InteropServices;

Vb.NET add:

Imports System.Runtime.InteropServices

--

I couldn't use that structure to attach secondary monitor properly.

Instead I've used structure from: http://www.microsoft.com/indonesia/msdn/pinvoke.aspx, which works for me fine. I suppose that dmPositionX and dmPositionY should be placed after dmFields.

Tips & Tricks:

Since the DEVMODE members beyond dmDisplayFrequency do not have to be declared, the structure can vary in size. You should set dmSize to effective size of your implemetation before calling API functions:

DEVMODE d = new DEVMODE();
d.dmSize = Marshal.SizeOf(typeof(DEVMODE));

This way the API is informed of the version of DEVMODE used.

If you use Reflector, you can find Microsoft's managed version of the structure.

As of .net 3.5 the DEVMODE structure is declared incorrectly causing incorrect values to be returned after dmBitsPerPel. The structures above have been updated to reflect the correct implementation.

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
DEVMODE on MSDN