|
The Code
Welcome to the changing screen
resolutions tutorial. This will be very simple.
I have a class, given to me by a vbProgrammer named tzodem (thanks
man) that changes screen resolutions. Create a new project, and
create a new class called ScreenSettings and put the following code
into it:
Option Explicit On
Imports System.Runtime.InteropServices
Public Class ScreenSettings
Const ENUM_CURRENT_SETTINGS As Integer = -1
Const CDS_UPDATEREGISTRY As Integer = &H1
Const CDS_TEST As Long = &H2
Const CCDEVICENAME As Integer = 32
Const CCFORMNAME As Integer = 32
Const DISP_CHANGE_SUCCESSFUL As Integer = 0
Const DISP_CHANGE_RESTART As Integer = 1
Const DISP_CHANGE_FAILED As Integer = -1
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA"
(ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef
lpDevMode As DEVMODE) As Integer
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA"
(ByRef DEVMODE As DEVMODE, ByVal flags As Long) As Integer
<StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)>
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
<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)>
Public dmFormName As String
Public dmUnusedPadding As Short
Public dmBitsPerPel As Short
Public dmPelsWidth As Integer
Public dmPelsHeight As Integer
Public dmDisplayFlags As Integer
Public dmDisplayFrequency As Integer
End Structure
Dim oldWidth, oldHeight As Integer
Public Sub New(ByVal width As Integer, ByVal height As Integer)
oldWidth = Screen.PrimaryScreen.Bounds.Width
oldHeight = Screen.PrimaryScreen.Bounds.Height
changeRes(width, height)
End Sub
Public Sub Dispose()
changeRes(oldWidth, oldHeight)
End Sub
Private Sub changeRes(ByVal theWidth As Integer, ByVal theHeight As
Integer)
Try
Dim DevM As DEVMODE
DevM.dmDeviceName = New [String](New Char(32) {})
DevM.dmFormName = New [String](New Char(32) {})
DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM)
Then
Dim lResult As Integer
DevM.dmPelsWidth = theWidth
DevM.dmPelsHeight = theHeight
lResult = ChangeDisplaySettings(DevM, CDS_TEST)
If lResult = DISP_CHANGE_FAILED Then
MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly +
MsgBoxStyle.Critical, "Screen Resolution Change Failed")
Else
lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
Select Case lResult
Case DISP_CHANGE_RESTART
MsgBox("You must restart your computer to apply these changes.",
MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Has
Changed")
Case DISP_CHANGE_SUCCESSFUL
Case Else
MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly +
MsgBoxStyle.Critical, "Screen Resolution Change Failed")
End Select
End If
End If
Catch ex As Security.SecurityException
MsgBox("You do not have authorization to change the resolution")
System.Environment.Exit(System.Environment.ExitCode)
End Try
End Sub
End Class
I really don't understand this code comprehensively, but I do get
the gist of how it works. Go back to form1:
Dim SetRes As ScreenSettings
In form1_load:
SetRes = New ScreenSettings(640,
480)
In form1_closed:
SetRes.Dispose()
There you go! It changes your
resolution to 640x480. Pretty simple huh? Thanks to tzodem for
this code!
There is no
source code for this tutorial as it is relatively short.
|