Change the resolution of the display in VB.NET
Microsoft MVP Li Honggen
In VB.NET, we can easily get the resolution of the display, however, it is more troublesome to change the resolution of the display. Since the .NET's class library does not encapsulate EnumdisPlaySettings and ChangeDisplaySettings, we have to call them functions. For VB6, VB.NET calls the API function is some small changes!
Below, we try to use these two API functions in VB.NET.
Create a new project, add two buttons to FORM1, one named btNgetDisp, set its text attribute to "get the resolution"; another button is btnsetdisp, the Text property is "Setting Rate". Then add the following code in the code window:
Private const ccdevicename as short = 32
Private const ccFormname as short = 32
Private const DM_PELSWIDTH AS INTEGER = & h80000
Private const DM_PELSHEIGHT AS INTEGER = & H100000
'Refresh frequency constant
Private const dm_displayfrequency as integer = & h400000
'Calling the API function
Private Declare Function EnumdisPlaySettings Lib "User32" Alias "EnumdisplaySettingsa" (byval) (byval Imodenum as ") as boolean
'Calling the API function
Private Declare Function ChangeDisplaySettings Lib "User32" Alias "ChangeDisplaySettingsa" (ByVal dwflags as uncmode, byval dwflags as integer) AS INTEGER
'Define structure
Private structure devmode
Dim DMSPECVERSION AS SHORT
Dim DMDriverVersion As SHORT
Dim DMSIZE AS SHORT
Dim DMDriveRextra As Short
DIM DMFIELDS AS INTEGER
Dim DMorientation as Short
Dim DMPAPERSIZE AS SHORT
Dim DMPAPERLENGTH AS SHORT
Dim DMPAPERWIDTH AS SHORT
Dim DMScale as Short
Dim DMCOPIES AS SHORT
Dim DMDefaultsource As Short
DIM DMPRINTQUALITY AS SHORT
Dim DMColor As Short
Dim DMDUPLEX AS SHORT
DIM DMYRESOLUTION As Shortdim DMTTOPTION AS SHORT
Dim DMCOLLATE AS SHORT
Dim DMunusedPadding as Short
Dim DMBITSPEL AS SHORT
Dim DMPELSWIDTH AS INTEGER
DIM DMPELSHEIGHT AS INTEGER
DIM DMDISPLAYFLAGS AS INTEGER
DIM DMDISPLAYFREQUENCY AS INTEGER
End structure
'Change the resolution process, parameter a width, parameter two height
Private subwaydisp (byref iWidth as single, byref iheight as single)
DIM BLNWORKED AS BOOLEAN
DIM I as integer
DIM DEVM As Form1.devmode
i = 0
DO
BLNWORKED = EnumdisplaySettings (0, I, DEVM)
i = i 1
Loop unsil (bLnWorked = false)
With devm
.dmfields = DM_PELSWIDTH OR DM_PELSHEIGHT OR DM_DISPLAYFREQUENCY
.dmpelswidth = iWidth
.dmpelsheight = Iheight
'Refresh frequency is 85
.dmdisplayfrequency = 85
End with
Call changeisplaySettings (devm, 0)
End Sub
Private sub btngetdisp_click (byvale as system.Object, byval e as system.eventargs) Handles btnetdisp.click
DIM X as short = system.windows.forms.screen.primaryScreen.bounds.width
Dim y as short = system.windows.Forms.Screen.primaryScreen.bounds.height
Msgbox ("Your Display Resolution is" & X & "X" & Y)
End Sub
Private sub btnsetdisp_click (byvale as system.object, byval e as system.eventargs) Handles btnetddisp.click
IF msgbox ("Do you confirm to change the display to 1024x768?"
'Call change resolution
ChangeDISP (1024, 768)
END IF
End Sub
The program runs as shown below, click the setup resolution, will change the display resolution to 1024x768, the refresh frequency is 85, is it simple?