Declare Function ChangeDisplaySettingsEx Lib "coredll.dll" (ByVal lpszDeviceName As String, ByVal lpDevMode As Byte(), ByVal hwnd As IntPtr, ByVal dwflags As CDSFlags, ByVal lParam As IntPtr) As CDSRet
Public Enum DMD
DMDO_0 = 0
DMDO_90 = 1
DMDO_180 = 2
DMDO_270 = 4
End Enum
Public Enum DM_Fields
DM_DISPLAYORIENTATION = 8388608
DM_DISPLAYQUERYORIENTATION = 16777216
End Enum
Public Enum DM_Orient
DMORIENT_PORTRAIT = 1
DMORIENT_LANDSCAPE = 2
End Enum
' Flags for ChangeDisplaySettings
Enum CDSFlags
CDS_VIDEOPARAMETERS = &H20
CDS_RESET = &H40000000
End Enum
' Return values for ChangeDisplaySettings
Enum CDSRet
DISP_CHANGE_SUCCESSFUL = 0
DISP_CHANGE_RESTART = 1
DISP_CHANGE_FAILED = -1
DISP_CHANGE_BADMODE = -2
DISP_CHANGE_NOTUPDATED = -3
DISP_CHANGE_BADFLAGS = -4
DISP_CHANGE_BADPARAM = -5
End Enum
Do you know one? Please contribute it!
Additional information is needed.
As for example how to rotate screen and stay in fullscreen mode.
Additional notes please add some!
Please add some!
Public Class SelfMarshalledStruct
Public Sub New(ByVal size As Integer)
m_data = New Byte(size - 1) {}
End Sub
Public Function GetChar(ByVal offset As Integer) As Char
Return BitConverter.ToChar(m_data, offset)
End Function
Public Sub SetChar(ByVal offset As Integer, ByVal val As Char)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2)
End Sub
Public Function GetInt32(ByVal offset As Integer) As Int32
Return BitConverter.ToInt32(m_data, offset)
End Function
Public Sub SetInt32(ByVal offset As Integer, ByVal val As Int32)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 4)
End Sub
Public Function GetUInt32(ByVal offset As Integer) As UInt32
Return BitConverter.ToUInt32(m_data, offset)
End Function
Public Sub SetUInt32(ByVal offset As Integer, ByVal val As UInt32)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 4)
End Sub
Public Function GetInt16(ByVal offset As Integer) As Int16
Return BitConverter.ToInt16(m_data, offset)
End Function
Public Sub SetInt16(ByVal offset As Integer, ByVal val As Int16)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2)
End Sub
Public Function GetUInt16(ByVal offset As Integer) As UInt16
Return BitConverter.ToUInt16(m_data, offset)
End Function
Public Sub SetUInt16(ByVal offset As Integer, ByVal val As UInt16)
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2)
End Sub
Public Function GetStringUni(ByVal offset As Integer, ByVal len As Integer) As String
Return Encoding.Unicode.GetString(m_data, offset, len).TrimEnd(Chr(0))
End Function
Public Sub SetStringUni(ByVal str As String, ByVal offset As Integer, ByVal len As Integer)
Encoding.Unicode.GetBytes(str, 0, Math.Min(str.Length, len), m_data, offset)
End Sub
Default Public Property Item(ByVal offset As Integer) As Byte
Get
Return m_data(offset)
End Get
Set(ByVal value As Byte)
m_data(offset) = value
End Set
End Property
Public Function [Get](ByVal t As Type, ByVal offset As Integer) As Object
If t.IsPrimitive Then
If t.BaseType.ToString = "Int32" Then
Return GetInt32(offset)
ElseIf t.BaseType.ToString = "Int16" Then
Return GetInt16(offset)
ElseIf t.BaseType.ToString = "UInt32" Then
Return GetUInt32(offset)
ElseIf t.BaseType.ToString = "UInt16" Then
Return GetUInt16(offset)
ElseIf t.BaseType.ToString = "Byte" Then
Return Me(offset)
End If
End If
Return Nothing
End Function
Public Sub [Set](ByVal t As Type, ByVal offset As Integer, ByVal Val As Object)
If t.IsPrimitive Then
If t.BaseType.ToString = "Int32" Then
SetInt32(offset, CInt(Val))
ElseIf t.BaseType.ToString = "Int16" Then
SetInt16(offset, CShort(Val))
ElseIf t.BaseType.ToString = "UInt32" Then
SetUInt32(offset, DirectCast(Val, UInt32))
ElseIf t.BaseType.ToString = "UInt16" Then
SetUInt16(offset, CUShort(Val))
ElseIf t.BaseType.ToString = "Byte" Then
Me(offset) = CByte(Val)
End If
ElseIf t.ToString = "String" Then
SetStringUni(DirectCast(Val, String), offset, DirectCast(Val, String).Length)
End If
End Sub
Protected m_data As Byte()
Public ReadOnly Property Data() As Byte()
Get
Return m_data
End Get
End Property
End Class
Public Class devicemodeW
Inherits SelfMarshalledStruct
Private _DM_Orient As DM_Orient
Private _DM_Fields As DM_Fields
Private _DMD As DMD
Public Sub New()
MyBase.New(192)
dmSize = CUShort(Data.Length)
End Sub
Public Property dmDeviceName() As String
Get
Return GetStringUni(0, 64)
End Get
Set(ByVal value As String)
SetStringUni(value, 0, 64)
End Set
End Property
Public Property dmSpecVersion() As UShort
Get
Return GetUInt16(64)
End Get
Set(ByVal value As UShort)
SetUInt16(64, value)
End Set
End Property
Public Property dmDriverVersion() As UShort
Get
Return GetUInt16(66)
End Get
Set(ByVal value As UShort)
SetUInt16(66, value)
End Set
End Property
Public Property dmSize() As UShort
Get
Return GetUInt16(68)
End Get
Set(ByVal value As UShort)
SetUInt16(68, value)
End Set
End Property
Public Property dmDriverExtra() As UShort
Get
Return GetUInt16(70)
End Get
Set(ByVal value As UShort)
SetUInt16(70, value)
End Set
End Property
Public Property dmFields() As DM_Fields
Get
'Return DirectCast(GetUInt32(72), DM_Fields)
Return _DM_Fields
End Get
Set(ByVal value As DM_Fields)
_DM_Fields = value
SetUInt32(72, CInt(value))
End Set
End Property
Public Property dmOrientation() As DM_Orient
Get
'Return DirectCast(GetInt16(76), DM_Orient)
Return _DM_Orient
End Get
Set(ByVal value As DM_Orient)
SetInt16(76, CShort(value))
End Set
End Property
Public Property dmPaperSize() As Short
Get
Return GetInt16(78)
End Get
Set(ByVal value As Short)
SetInt16(78, value)
End Set
End Property
Public Property dmPaperLength() As Short
Get
Return GetInt16(80)
End Get
Set(ByVal value As Short)
SetInt16(80, value)
End Set
End Property
Public Property dmPaperWidth() As Short
Get
Return GetInt16(82)
End Get
Set(ByVal value As Short)
SetInt16(82, value)
End Set
End Property
Public Property dmScale() As Short
Get
Return GetInt16(84)
End Get
Set(ByVal value As Short)
SetInt16(84, value)
End Set
End Property
Public Property dmCopies() As Short
Get
Return GetInt16(86)
End Get
Set(ByVal value As Short)
SetInt16(86, value)
End Set
End Property
Public Property dmDefaultSource() As Short
Get
Return GetInt16(88)
End Get
Set(ByVal value As Short)
SetInt16(88, value)
End Set
End Property
Public Property dmPrintQuality() As Short
Get
Return GetInt16(90)
End Get
Set(ByVal value As Short)
SetInt16(90, value)
End Set
End Property
Public Property dmColor() As Short
Get
Return GetInt16(92)
End Get
Set(ByVal value As Short)
SetInt16(92, value)
End Set
End Property
Public Property dmDuplex() As Short
Get
Return GetInt16(94)
End Get
Set(ByVal value As Short)
SetInt16(94, value)
End Set
End Property
Public Property dmYResolution() As Short
Get
Return GetInt16(96)
End Get
Set(ByVal value As Short)
SetInt16(96, value)
End Set
End Property
Public Property dmTTOption() As Short
Get
Return GetInt16(98)
End Get
Set(ByVal value As Short)
SetInt16(98, value)
End Set
End Property
Public Property dmCollate() As Short
Get
Return GetInt16(100)
End Get
Set(ByVal value As Short)
SetInt16(100, value)
End Set
End Property
Public Property dmFormName() As String
Get
Return GetStringUni(102, 64)
End Get
Set(ByVal value As String)
SetStringUni(value, 102, 64)
End Set
End Property
Public Property dmLogPixels() As UShort
Get
Return GetUInt16(166)
End Get
Set(ByVal value As UShort)
SetUInt16(166, value)
End Set
End Property
Public Property dmBitsPerPel() As UInteger
Get
Return GetUInt32(168)
End Get
Set(ByVal value As UInteger)
SetUInt32(168, value)
End Set
End Property
Public Property dmPelsWidth() As UInteger
Get
Return GetUInt32(172)
End Get
Set(ByVal value As UInteger)
SetUInt32(172, value)
End Set
End Property
Public Property dmPelsHeight() As UInteger
Get
Return GetUInt32(176)
End Get
Set(ByVal value As UInteger)
SetUInt32(176, value)
End Set
End Property
Public Property dmDisplayFlags() As UInteger
Get
Return GetUInt32(180)
End Get
Set(ByVal value As UInteger)
SetUInt32(180, value)
End Set
End Property
Public Property dmDisplayFrequency() As UInteger
Get
Return GetUInt32(184)
End Get
Set(ByVal value As UInteger)
SetUInt32(184, value)
End Set
End Property
Public Property dmDisplayOrientation() As DMD
Get
'Return DirectCast(GetUInt32(188), DMD)
Return _DMD
End Get
Set(ByVal value As DMD)
_DMD = value
SetUInt32(188, CInt(value))
End Set
End Property
End Class
Public Class ScreenMode
'DEVICEMODE
<DllImport("coredll.dll")> _
Friend Shared Function ChangeDisplaySettingsEx(ByVal lpszDeviceName As String, ByVal lpDevMode As Byte(), ByVal hwnd As IntPtr, ByVal dwflags As CDSFlags, ByVal lParam As IntPtr) As CDSRet
End Function
Public Function ChangeMode(ByVal degree As Integer) As String
Dim devMode As New devicemodeW()
devMode.dmFields = DM_Fields.DM_DISPLAYORIENTATION
If degree = 0 Then
devMode.dmDisplayOrientation = DMD.DMDO_0
ElseIf degree = 90 Then
devMode.dmDisplayOrientation = DMD.DMDO_90
ElseIf degree = 180 Then
devMode.dmDisplayOrientation = DMD.DMDO_180
ElseIf degree = 270 Then
devMode.dmDisplayOrientation = DMD.DMDO_270
End If
Dim ret As CDSRet = ChangeDisplaySettingsEx(Nothing, devMode.Data, IntPtr.Zero, 0, IntPtr.Zero)
Return (ret.ToString())
End Function
Public Sub SetLandscape()
Try
ChangeMode(90)
Catch ex As Exception
'MsgBox(ex.ToString)
End Try
End Sub
Public Sub SetPortrait()
Try
ChangeMode(0)
Catch ex As Exception
'MsgBox(ex.ToString)
End Try
End Sub
End Class
C# Sample Code(it's directlly convert from the VB.net Code via conversion tool-RRave)
public class SelfMarshalledStruct
{
public SelfMarshalledStruct(int size)
{
m_data = new byte[size];
}
public char GetChar(int offset)
{
return BitConverter.ToChar(m_data, offset);
}
public void SetChar(int offset, char val)
{
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2);
}
public Int32 GetInt32(int offset)
{
return BitConverter.ToInt32(m_data, offset);
}
public void SetInt32(int offset, Int32 val)
{
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 4);
}
public UInt32 GetUInt32(int offset)
{
return BitConverter.ToUInt32(m_data, offset);
}
public void SetUInt32(int offset, UInt32 val)
{
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 4);
}
public Int16 GetInt16(int offset)
{
return BitConverter.ToInt16(m_data, offset);
}
public void SetInt16(int offset, Int16 val)
{
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2);
}
public UInt16 GetUInt16(int offset)
{
return BitConverter.ToUInt16(m_data, offset);
}
public void SetUInt16(int offset, UInt16 val)
{
Buffer.BlockCopy(BitConverter.GetBytes(val), 0, m_data, offset, 2);
}
public string GetStringUni(int offset, int len)
{
return Encoding.Unicode.GetString(m_data, offset, len).TrimEnd(Strings.Chr(0));
}
public void SetStringUni(string str, int offset, int len)
{
Encoding.Unicode.GetBytes(str, 0, Math.Min(str.Length, len), m_data, offset);
}
public byte Item {
get { return m_data(offset); }
set { m_data(offset) = value; }
}
public object Get(Type t, int offset)
{
if (t.IsPrimitive) {
if (t.BaseType.ToString == "Int32") {
return GetInt32(offset);
}
else if (t.BaseType.ToString == "Int16") {
return GetInt16(offset);
}
else if (t.BaseType.ToString == "UInt32") {
return GetUInt32(offset);
}
else if (t.BaseType.ToString == "UInt16") {
return GetUInt16(offset);
}
else if (t.BaseType.ToString == "Byte") {
return this(offset);
}
}
return null;
}
public void Set(Type t, int offset, object Val)
{
if (t.IsPrimitive) {
if (t.BaseType.ToString == "Int32") {
SetInt32(offset, (int)Val);
}
else if (t.BaseType.ToString == "Int16") {
SetInt16(offset, (short)Val);
}
else if (t.BaseType.ToString == "UInt32") {
SetUInt32(offset, (UInt32)Val);
}
else if (t.BaseType.ToString == "UInt16") {
SetUInt16(offset, (ushort)Val);
}
else if (t.BaseType.ToString == "Byte") {
this(offset) = (byte)Val;
}
}
else if (t.ToString == "String") {
SetStringUni((string)Val, offset, ((string)Val).Length);
}
}
protected byte[] m_data;
public byte[] Data {
get { return m_data; }
}
}
public class devicemodeW : SelfMarshalledStruct
{
private DM_Orient _DM_Orient;
private DM_Fields _DM_Fields;
private DMD _DMD;
public devicemodeW() : base(192)
{
dmSize = (ushort)Data.Length;
}
public string dmDeviceName {
get { return GetStringUni(0, 64); }
set { SetStringUni(value, 0, 64); }
}
public ushort dmSpecVersion {
get { return GetUInt16(64); }
set { SetUInt16(64, value); }
}
public ushort dmDriverVersion {
get { return GetUInt16(66); }
set { SetUInt16(66, value); }
}
public ushort dmSize {
get { return GetUInt16(68); }
set { SetUInt16(68, value); }
}
public ushort dmDriverExtra {
get { return GetUInt16(70); }
set { SetUInt16(70, value); }
}
public DM_Fields dmFields {
//Return DirectCast(GetUInt32(72), DM_Fields)
get { return _DM_Fields; }
set {
_DM_Fields = value;
SetUInt32(72, (int)value);
}
}
public DM_Orient dmOrientation {
//Return DirectCast(GetInt16(76), DM_Orient)
get { return _DM_Orient; }
set { SetInt16(76, (short)value); }
}
public short dmPaperSize {
get { return GetInt16(78); }
set { SetInt16(78, value); }
}
public short dmPaperLength {
get { return GetInt16(80); }
set { SetInt16(80, value); }
}
public short dmPaperWidth {
get { return GetInt16(82); }
set { SetInt16(82, value); }
}
public short dmScale {
get { return GetInt16(84); }
set { SetInt16(84, value); }
}
public short dmCopies {
get { return GetInt16(86); }
set { SetInt16(86, value); }
}
public short dmDefaultSource {
get { return GetInt16(88); }
set { SetInt16(88, value); }
}
public short dmPrintQuality {
get { return GetInt16(90); }
set { SetInt16(90, value); }
}
public short dmColor {
get { return GetInt16(92); }
set { SetInt16(92, value); }
}
public short dmDuplex {
get { return GetInt16(94); }
set { SetInt16(94, value); }
}
public short dmYResolution {
get { return GetInt16(96); }
set { SetInt16(96, value); }
}
public short dmTTOption {
get { return GetInt16(98); }
set { SetInt16(98, value); }
}
public short dmCollate {
get { return GetInt16(100); }
set { SetInt16(100, value); }
}
public string dmFormName {
get { return GetStringUni(102, 64); }
set { SetStringUni(value, 102, 64); }
}
public ushort dmLogPixels {
get { return GetUInt16(166); }
set { SetUInt16(166, value); }
}
public uint dmBitsPerPel {
get { return GetUInt32(168); }
set { SetUInt32(168, value); }
}
public uint dmPelsWidth {
get { return GetUInt32(172); }
set { SetUInt32(172, value); }
}
public uint dmPelsHeight {
get { return GetUInt32(176); }
set { SetUInt32(176, value); }
}
public uint dmDisplayFlags {
get { return GetUInt32(180); }
set { SetUInt32(180, value); }
}
public uint dmDisplayFrequency {
get { return GetUInt32(184); }
set { SetUInt32(184, value); }
}
public DMD dmDisplayOrientation {
//Return DirectCast(GetUInt32(188), DMD)
get { return _DMD; }
set {
_DMD = value;
SetUInt32(188, (int)value);
}
}
}
public class ScreenMode
{
//DEVICEMODE
[DllImport("coredll.dll")]
static internal extern CDSRet ChangeDisplaySettingsEx(string lpszDeviceName, byte[] lpDevMode, IntPtr hwnd, CDSFlags dwflags, IntPtr lParam);
public string ChangeMode(int degree)
{
devicemodeW devMode = new devicemodeW();
devMode.dmFields = DM_Fields.DM_DISPLAYORIENTATION;
if (degree == 0) {
devMode.dmDisplayOrientation = DMD.DMDO_0;
}
else if (degree == 90) {
devMode.dmDisplayOrientation = DMD.DMDO_90;
}
else if (degree == 180) {
devMode.dmDisplayOrientation = DMD.DMDO_180;
}
else if (degree == 270) {
devMode.dmDisplayOrientation = DMD.DMDO_270;
}
CDSRet ret = ChangeDisplaySettingsEx(null, devMode.Data, IntPtr.Zero, 0, IntPtr.Zero);
return (ret.ToString());
}
public void SetLandscape()
{
try {
ChangeMode(90);
}
catch (Exception ex) {
//MsgBox(ex.ToString)
}
}
public void SetPortrait()
{
try {
ChangeMode(0);
}
catch (Exception ex) {
//MsgBox(ex.ToString)
}
}
}