Hello,

i have this code and it works (if the device is in landscape it retrieves
the display orientation ok):

DEVMODE sDevMode = {0};
sDevMode.dmSize = sizeof(DEVMODE);
sDevMode.dmFields = DM_DISPLAYORIENTATION;

ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_TEST, NULL);
if(sDevMode.dmDisplayOrientation != DMDO_0)
{
sDevMode.dmDisplayOrientation = DMDO_90;
ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_RESET, NULL);
}

I write this code in c# and it works fine to change the orientation of the
display, but when i try to query the current orientation i get always the
DMDO_0 value.

public static uint DM_DISPLAYORIENTATION = 0x00800000;

public static int DISP_CHANGE_SUCCESSFUL = 0;
public static int DISP_CHANGE_RESTART = 1;
public static int DISP_CHANGE_FAILED = -1;
public static int DISP_CHANGE_BADMODE = -2;
public static int DISP_CHANGE_NOTUPDATED = -3;
public static int DISP_CHANGE_BADFLAGS = -4;
public static int DISP_CHANGE_BADPARAM = -5;
public static uint CDS_RESET = 0x40000000;
public static uint CDS_TEST = 0x00000002;
public static uint DMDO_0 = 0;
public static uint DMDO_90 = 1;
public static uint DMDO_180 = 2;
public static uint DMDO_270 = 4;
private const int CCHDEVICENAME = 32;
private const int CCHFORMNAME = 32;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public uint 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 uint dmBitsPerPel;
public uint dmPelsWidth;
public uint dmPelsHeight;
public uint dmDisplayFlags;
public uint dmDisplayFrequency;
public uint dmDisplayOrientation;
}

[DllImport("coredll.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettingsEx(string lpszDeviceName,
[In] ref DEVMODE lpDevMode, IntPtr hwnd, uint dwFlags, IntPtr lParam);

and this is the sample i used:

DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
dm.dmFields = DM_DISPLAYORIENTATION;
ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, CDS_TEST, IntPtr.Zero);

if (dm.dmDisplayOrientation != DMDO_90)
{
dm.dmDisplayOrientation = DMDO_90;
ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, CDS_RESET, IntPtr.Zero);
}

the code above works fine and rotates the display to the right, the problem
is that if the device is already turned to the right the value
dm.dmDisplayOrientation after first call of function with parameter CDS_TEST
is always DMDO_0.

can anyone give me a hint? why i can get the correct rotation value? because
i can change it.

andrei

Re: ChangeDisplaySettingsEx by ctacke/>

ctacke/>
Fri Dec 07 07:19:19 PST 2007

I'd take a closer look at the packing of the struct. Just looking at your
managed definition you have 2-byte misalignment at dmFormName and
dmBitsPerPixel. In native code the compiler is going to pad those out by 2
bytes - I'm not sure managed code will - I think you may need to pad it
manually.

My bet is that the struct you're passing in is 4 bytes shorter than the API
expects and since orientation is at the end, you're getting undefined
behavior (you're only lucky it's consistent).


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com




"Andrew" <Andrew@discussions.microsoft.com> wrote in message
news:92753C82-1167-48A2-BCBF-04366B838DFC@microsoft.com...
> Hello,
>
> i have this code and it works (if the device is in landscape it retrieves
> the display orientation ok):
>
> DEVMODE sDevMode = {0};
> sDevMode.dmSize = sizeof(DEVMODE);
> sDevMode.dmFields = DM_DISPLAYORIENTATION;
>
> ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_TEST, NULL);
> if(sDevMode.dmDisplayOrientation != DMDO_0)
> {
> sDevMode.dmDisplayOrientation = DMDO_90;
> ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_RESET, NULL);
> }
>
> I write this code in c# and it works fine to change the orientation of the
> display, but when i try to query the current orientation i get always the
> DMDO_0 value.
>
> public static uint DM_DISPLAYORIENTATION = 0x00800000;
>
> public static int DISP_CHANGE_SUCCESSFUL = 0;
> public static int DISP_CHANGE_RESTART = 1;
> public static int DISP_CHANGE_FAILED = -1;
> public static int DISP_CHANGE_BADMODE = -2;
> public static int DISP_CHANGE_NOTUPDATED = -3;
> public static int DISP_CHANGE_BADFLAGS = -4;
> public static int DISP_CHANGE_BADPARAM = -5;
> public static uint CDS_RESET = 0x40000000;
> public static uint CDS_TEST = 0x00000002;
> public static uint DMDO_0 = 0;
> public static uint DMDO_90 = 1;
> public static uint DMDO_180 = 2;
> public static uint DMDO_270 = 4;
> private const int CCHDEVICENAME = 32;
> private const int CCHFORMNAME = 32;
>
> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
> struct DEVMODE
> {
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
> public string dmDeviceName;
> public short dmSpecVersion;
> public short dmDriverVersion;
> public short dmSize;
> public short dmDriverExtra;
> public uint 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 uint dmBitsPerPel;
> public uint dmPelsWidth;
> public uint dmPelsHeight;
> public uint dmDisplayFlags;
> public uint dmDisplayFrequency;
> public uint dmDisplayOrientation;
> }
>
> [DllImport("coredll.dll", CharSet = CharSet.Auto)]
> static extern int ChangeDisplaySettingsEx(string lpszDeviceName,
> [In] ref DEVMODE lpDevMode, IntPtr hwnd, uint dwFlags, IntPtr lParam);
>
> and this is the sample i used:
>
> DEVMODE dm = new DEVMODE();
> dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
> dm.dmFields = DM_DISPLAYORIENTATION;
> ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, CDS_TEST, IntPtr.Zero);
>
> if (dm.dmDisplayOrientation != DMDO_90)
> {
> dm.dmDisplayOrientation = DMDO_90;
> ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, CDS_RESET,
> IntPtr.Zero);
> }
>
> the code above works fine and rotates the display to the right, the
> problem
> is that if the device is already turned to the right the value
> dm.dmDisplayOrientation after first call of function with parameter
> CDS_TEST
> is always DMDO_0.
>
> can anyone give me a hint? why i can get the correct rotation value?
> because
> i can change it.
>
> andrei
>



Re: ChangeDisplaySettingsEx by Andrew

Andrew
Fri Dec 07 08:07:01 PST 2007

that's not the problem because in both cases c++ and c# the size is 192.
anyway i tried to move the dmFormName with 2 bytes by putting before it a 2
bytes type and now it doesn't work at all. i think that the problem is when
executing the query information (CDS_TEST) because when the parametyer is
CDS_RESET the code works just fine and the display is rotated.



"<ctacke/>" wrote:

> I'd take a closer look at the packing of the struct. Just looking at your
> managed definition you have 2-byte misalignment at dmFormName and
> dmBitsPerPixel. In native code the compiler is going to pad those out by 2
> bytes - I'm not sure managed code will - I think you may need to pad it
> manually.
>
> My bet is that the struct you're passing in is 4 bytes shorter than the API
> expects and since orientation is at the end, you're getting undefined
> behavior (you're only lucky it's consistent).
>
>
> --
>
> Chris Tacke, eMVP
> Join the Embedded Developer Community
> http://community.opennetcf.com
>
>
>
>
> "Andrew" <Andrew@discussions.microsoft.com> wrote in message
> news:92753C82-1167-48A2-BCBF-04366B838DFC@microsoft.com...
> > Hello,
> >
> > i have this code and it works (if the device is in landscape it retrieves
> > the display orientation ok):
> >
> > DEVMODE sDevMode = {0};
> > sDevMode.dmSize = sizeof(DEVMODE);
> > sDevMode.dmFields = DM_DISPLAYORIENTATION;
> >
> > ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_TEST, NULL);
> > if(sDevMode.dmDisplayOrientation != DMDO_0)
> > {
> > sDevMode.dmDisplayOrientation = DMDO_90;
> > ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_RESET, NULL);
> > }
> >
> > I write this code in c# and it works fine to change the orientation of the
> > display, but when i try to query the current orientation i get always the
> > DMDO_0 value.
> >
> > public static uint DM_DISPLAYORIENTATION = 0x00800000;
> >
> > public static int DISP_CHANGE_SUCCESSFUL = 0;
> > public static int DISP_CHANGE_RESTART = 1;
> > public static int DISP_CHANGE_FAILED = -1;
> > public static int DISP_CHANGE_BADMODE = -2;
> > public static int DISP_CHANGE_NOTUPDATED = -3;
> > public static int DISP_CHANGE_BADFLAGS = -4;
> > public static int DISP_CHANGE_BADPARAM = -5;
> > public static uint CDS_RESET = 0x40000000;
> > public static uint CDS_TEST = 0x00000002;
> > public static uint DMDO_0 = 0;
> > public static uint DMDO_90 = 1;
> > public static uint DMDO_180 = 2;
> > public static uint DMDO_270 = 4;
> > private const int CCHDEVICENAME = 32;
> > private const int CCHFORMNAME = 32;
> >
> > [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
> > struct DEVMODE
> > {
> > [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
> > public string dmDeviceName;
> > public short dmSpecVersion;
> > public short dmDriverVersion;
> > public short dmSize;
> > public short dmDriverExtra;
> > public uint 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 uint dmBitsPerPel;
> > public uint dmPelsWidth;
> > public uint dmPelsHeight;
> > public uint dmDisplayFlags;
> > public uint dmDisplayFrequency;
> > public uint dmDisplayOrientation;
> > }
> >
> > [DllImport("coredll.dll", CharSet = CharSet.Auto)]
> > static extern int ChangeDisplaySettingsEx(string lpszDeviceName,
> > [In] ref DEVMODE lpDevMode, IntPtr hwnd, uint dwFlags, IntPtr lParam);
> >
> > and this is the sample i used:
> >
> > DEVMODE dm = new DEVMODE();
> > dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
> > dm.dmFields = DM_DISPLAYORIENTATION;
> > ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, CDS_TEST, IntPtr.Zero);
> >
> > if (dm.dmDisplayOrientation != DMDO_90)
> > {
> > dm.dmDisplayOrientation = DMDO_90;
> > ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, CDS_RESET,
> > IntPtr.Zero);
> > }
> >
> > the code above works fine and rotates the display to the right, the
> > problem
> > is that if the device is already turned to the right the value
> > dm.dmDisplayOrientation after first call of function with parameter
> > CDS_TEST
> > is always DMDO_0.
> >
> > can anyone give me a hint? why i can get the correct rotation value?
> > because
> > i can change it.
> >
> > andrei
> >
>
>
>

Re: ChangeDisplaySettingsEx by Christopher

Christopher
Sun Dec 09 13:10:19 PST 2007

Hi,

"Andrew" <Andrew@discussions.microsoft.com> wrote in message
news:92753C82-1167-48A2-BCBF-04366B838DFC@microsoft.com...
> ChangeDisplaySettingsEx(NULL, &sDevMode, NULL, CDS_TEST, NULL);

Are you doing this because you are targeting .NET Compact Framework v1.0? If
you are developing for .NET Compact Framework v2.0 or above, are you aware
of the SystemSettings class (in particular the ScreenOrientation property?).

This can be used to query the current orientation:

using Microsoft.WindowsCE.Forms;
MessageBox.Show(SystemSettings.ScreenOrientation.ToString(), "Current
orientation");

Or set the desired orientation:

using Microsoft.WindowsCE.Forms;
SystemSettings.ScreenOrientation = ScreenOrientation.Angle90;

To use this class just add a reference to the Microsoft.WindowsCE.Forms
assembly.

Hope this helps,
Christopher Fairbairn