Checking for Screen Rotation Support using version 2 of the .NET Compact Framework - Visual Basic .Net version
Last year, I posted a snippet on
how to determine whether or not your device supported screen rotation using the .NET Compact Framework. I have recently been asked for a Visual Basic .NET version of the snippet. Other than some minor reordering / reformatting, the code below is a direct port of the example from the C# post.
''---------------------------------------------------------------------
''THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
''KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
''IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
''PARTICULAR PURPOSE.
''---------------------------------------------------------------------
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Module ScreenRotationCheckVBNet
' DeviceMode field value(s)
Private Const DisplayQueryOrientation As Int32 = &H1000000
' ChangeDisplaySettingsEx flags value(s)
Private Const ChangeDisplaySettingsTest As Int32 = 2
' P/Invoke signature(s)
Declare Function ChangeDisplaySettingsEx Lib "coredll.dll" _
(ByVal deviceName As String, ByRef deviceMode As DeviceMode, _
ByVal hwnd As IntPtr, ByVal flags As Int32, ByVal param As IntPtr) As Int32
''' <summary>
''' Application entry point
''' </summary>
Sub Main()
Dim message As String = String.Empty
Dim rotationSupported As Boolean = False
' prepare the DeviceMode structure
Dim devMode As New DeviceMode
devMode.Size = Marshal.SizeOf(devMode)
devMode.Fields = DisplayQueryOrientation
' ask the operating system if it supports screen rotation
Dim result As Int32
result = ChangeDisplaySettingsEx(Nothing, _
devMode, _
IntPtr.Zero, _
ChangeDisplaySettingsTest, _
IntPtr.Zero)
If (result = 0) Then
' ChangeDisplaySettingsEx call succeeded,
' we can check the results of our query
' non-zero value in DisplayOrientation means
' rotation is supported
If (devMode.DisplayOrientation <> 0) Then
rotationSupported = True
End If
' format the message to display
message = "Screen rotation supported: " + rotationSupported.ToString()
Else
' ChangeDisplaySettingsEx call failed
' format the message to display
message = "ChangeDisplaySettingsEx failed with error code: " + result.ToString()
End If
' display our findings
MessageBox.Show(message, "Screen rotation check")
End Sub
End Module
''' <summary>
''' Managed version of the DEVMODE structure
''' </summary>
Structure DeviceMode
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
Public DeviceName As String
Public SpecVersion As Int16
Public DriverVersion As Int16
Public Size As Int16
Public DriverExtra As Int16
Public Fields As Int32
Public Orientation As Int16
Public PaperSize As Int16
Public PaperLength As Int16
Public Scale As Int16
Public Copies As Int16
Public DefaultSource As Int16
Public PrintQuality As Int16
Public Color As Int16
Public Duplex As Int16
Public YResolution As Int16
Public TTOption As Int16
Public Collate As Int16
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
Public FormName As String
Public LogPixels As Int16
Public BitsPerPixel As Int32
Public PelsWidth As Int32
Public PelsHeight As Int32
Public DisplayFlags As Int32
Public DisplayFrequency As Int32
Public DisplayOrientation As Int32
End Structure
Enjoy!
-- DK
Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.