Hi,

Not sure how useful this is to anyone but I had alot of trouble finding a VB.NET Screen Resolution Change functionality, so I managed to get it working, and the code is below.

Can be called by: ChangeRes(1024,768)

Any comments are welcome, as are bug fixes!!!!

VB Code:
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Module resChanger
  4.  
  5.     Const ENUM_CURRENT_SETTINGS As Integer = -1
  6.     Const CDS_UPDATEREGISTRY As Integer = &H1
  7.     Const CDS_TEST As Long = &H2
  8.  
  9.     Const CCDEVICENAME As Integer = 32
  10.     Const CCFORMNAME As Integer = 32
  11.  
  12.     Const DISP_CHANGE_SUCCESSFUL As Integer = 0
  13.     Const DISP_CHANGE_RESTART As Integer = 1
  14.     Const DISP_CHANGE_FAILED As Integer = -1
  15.  
  16.     Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
  17.     Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Long) As Integer
  18.  
  19.     <StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
  20.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public dmDeviceName As String
  21.         Public dmSpecVersion As Short
  22.         Public dmDriverVersion As Short
  23.         Public dmSize As Short
  24.         Public dmDriverExtra As Short
  25.         Public dmFields As Integer
  26.         Public dmOrientation As Short
  27.         Public dmPaperSize As Short
  28.         Public dmPaperLength As Short
  29.         Public dmPaperWidth As Short
  30.         Public dmScale As Short
  31.         Public dmCopies As Short
  32.         Public dmDefaultSource As Short
  33.         Public dmPrintQuality As Short
  34.         Public dmColor As Short
  35.         Public dmDuplex As Short
  36.         Public dmYResolution As Short
  37.         Public dmTTOption As Short
  38.         Public dmCollate As Short
  39.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
  40.         Public dmUnusedPadding As Short
  41.         Public dmBitsPerPel As Short
  42.         Public dmPelsWidth As Integer
  43.         Public dmPelsHeight As Integer
  44.         Public dmDisplayFlags As Integer
  45.         Public dmDisplayFrequency As Integer
  46.     End Structure
  47.  
  48.     Public Sub changeRes(ByVal theWidth As Integer, ByVal theHeight As Integer)
  49.  
  50.         Dim DevM As DEVMODE
  51.  
  52.         DevM.dmDeviceName = New [String](New Char(32) {})
  53.         DevM.dmFormName = New [String](New Char(32) {})
  54.         DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
  55.  
  56.  
  57.         If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
  58.             Dim lResult As Integer
  59.  
  60.             DevM.dmPelsWidth = theWidth
  61.             DevM.dmPelsHeight = theHeight
  62.             DevM.dmPelsWidth = 1280
  63.             DevM.dmPelsHeight = 1024
  64.  
  65.             lResult = ChangeDisplaySettings(DevM, CDS_TEST)
  66.  
  67.             If lResult = DISP_CHANGE_FAILED Then
  68.                 MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
  69.             Else
  70.  
  71.                 lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
  72.  
  73.                 Select Case lResult
  74.                     Case DISP_CHANGE_RESTART
  75.                         MsgBox("You must restart your computer to apply these changes.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Has Changed")
  76.                     Case DISP_CHANGE_SUCCESSFUL
  77.                         MsgBox("Display Change Successful.", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Screen Resolution Successful")
  78.                     Case Else
  79.                         MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
  80.                 End Select
  81.             End If
  82.  
  83.  
  84.         End If
  85.     End Sub
  86.  
  87. End Module