Hiya,
I found a C# module to change the screen resolution. However, i havnt a donkeys with C#. Don't suppose anyone can convert it into VB.NET for me?
Many Thanks
Pete
Printable View
Hiya,
I found a C# module to change the screen resolution. However, i havnt a donkeys with C#. Don't suppose anyone can convert it into VB.NET for me?
Many Thanks
Pete
VB Code:
' Original C# Version Copyright 2002 Omniscium, all rights reserved. ' Converted to VB.NET by Lunatic3 ' Please send your comments :) Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Runtime.InteropServices #Region "DEVMODE_STRUCT" <StructLayout(LayoutKind.Sequential)> _ Public Structure DEVMODE <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _ 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:=32)> _ Public dmFormName As String ', Public dmLogPixels As Short Public dmBitsPerPel As Short 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 End Structure #End Region #Region "PINVOKEDEF" Class User32 Public Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" (ByVal deviceName As String, ByVal modeNum As Integer, ByRef devMode As DEVMODE) As Integer Public Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" (ByRef devMode As DEVMODE, ByVal flags As Integer) As Integer Public Const ENUM_CURRENT_SETTINGS As Integer = -1 Public Const CDS_UPDATEREGISTRY As Integer = &H1 Public Const CDS_TEST As Integer = &H2 Public Const DISP_CHANGE_SUCCESSFUL As Integer = 0 Public Const DISP_CHANGE_RESTART As Integer = 1 Public Const DISP_CHANGE_FAILED As Integer = -1 End Class #End Region Namespace Resolution Module Module1 Class CResolution Shared Sub Main(ByVal args() As String) Dim screen As Screen = screen.PrimaryScreen Console.WriteLine("Screen Width = " & screen.Bounds.Width) Console.WriteLine("Screen Height = " & screen.Bounds.Height) If args.Length <> 2 Then Console.WriteLine("Please pass in the width followed by the height of the resolution you want.") Return End If Dim iWidth As Integer = Int32.Parse(args(0)) Dim iHeight As Integer = Int32.Parse(args(1)) Dim dm As DEVMODE = New DEVMODE() dm.dmDeviceName = New [String](New Char(32) {}) dm.dmFormName = New [String](New Char(32) {}) dm.dmSize = CType(Marshal.SizeOf(dm), Short) If 0 <> User32.EnumDisplaySettings(Nothing, User32.ENUM_CURRENT_SETTINGS, dm) Then dm.dmPelsWidth = iWidth dm.dmPelsHeight = iHeight Dim iRet As Integer = User32.ChangeDisplaySettings(dm, User32.CDS_TEST) If iRet = User32.DISP_CHANGE_FAILED Then Console.WriteLine("Change failed") Else iRet = User32.ChangeDisplaySettings(dm, User32.CDS_UPDATEREGISTRY) Select Case iRet Case User32.DISP_CHANGE_SUCCESSFUL Console.WriteLine("Changed the resolution.") Case User32.DISP_CHANGE_RESTART Console.WriteLine("You will need to reboot for the change to happen.") Case Else Console.WriteLine("Failed to change the resolution.") End Select End If End If End Sub End Class End Module End Namespace
Thankyou very much, been trying to do this for ages.
Pete
This raises an interesting question....
Do you think MS will ever make a class to do this internal to the framework? OR are we stuck using the above unmanaged Win32API?
It seems to me that almost all of the truly platform-dependent stuff is intentionally left out of the Framework, although hopefully MS will keep going with stuff like Windows Management Interface. I cannot believe things like formatted printing were left out of the Framework "by accident". Here's a funny MS quote:
ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vaconCallingWindowsAPIs.htm
"Windows API calls were an important part of Visual Basic programming in the past, but are seldom necessary with Visual Basic .NET. Whenever possible, you should use managed code from the .NET Framework to perform tasks instead of Windows API calls."
... unless you want to write basic utility software...