Change the resolution of the display in vb.net

Source: Internet
Author: User
Tags integer
Shown in vb.net, we can easily get the resolution of the display, but it is more troublesome to change the resolution of the display. Because. NET's class library does not encapsulate the two API functions of EnumDisplaySettings and changedisplaysettings, but we have to call them functions, compared to VB6, VB. NET call API functions are some minor changes!
Now, let's try these two API functions in vb.net.
Create a new item, add two buttons to the Form1, one named Btngetdisp, set its Text property to "Get Resolution", and another button named Btnsetdisp,text property to "set resolution." 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 API functions
Private Declare Function enumdisplaysettings Lib "user32" Alias "Enumdisplaysettingsa"
(ByVal lpszdevicename As Integer, ByVal imodenum As Integer, ByRef lpdevmode as DEVMODE) As Boolean
' Calling API functions
Private Declare Function changedisplaysettings Lib "user32" Alias "Changedisplaysettingsa"
(ByRef Lpdevmode as DEVMODE, ByVal dwflags as Integer) As Integer

' Define Structure
Private Structure DEVMODE
<vbfixedstring (ccdevicename), System.Runtime.InteropServices.MarshalAs
(SYSTEM.RUNTIME.INTEROPSERVICES.UNMANAGEDTYPE.BYVALTSTR, Sizeconst:=ccdevicename) > Public dmdevicename as String
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 Short
Dim Dmttoption as Short
Dim Dmcollate as Short
<vbfixedstring (ccformname), System.Runtime.InteropServices.MarshalAs
(SYSTEM.RUNTIME.INTEROPSERVICES.UNMANAGEDTYPE.BYVALTSTR, Sizeconst:=ccformname) > Public dmformname as String
Dim dmunusedpadding as Short
Dim Dmbitsperpel 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 width, parameter two height
Private Sub Changedisp (ByRef iwidth as single, ByRef iheight as single)
Dim blnworked as Boolean
Dim I as Integer
Dim DEVM as Form1.devmode
i = 0
Todo
blnworked = enumdisplaysettings (0, I, DEVM)
i = i + 1
Loop Until (blnworked = False)

With DEVM
. dmfields = Dm_pelswidth or Dm_pelsheight or dm_displayfrequency
. Dmpelswidth = iwidth
. Dmpelsheight = Iheight
' Refresh rate is 85
. dmdisplayfrequency = 85
End With
Call ChangeDisplaySettings (DEVM, 0)
End Sub

Private Sub Btngetdisp_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btngetdisp.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 monitor resolution is" & X & "X" & Y)
End Sub

Private Sub Btnsetdisp_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnsetdisp.click
If MsgBox ("Are you sure you want to change the display resolution to 1024x768?) ", Msgboxstyle.okcancel," system Message "= Msgboxresult.ok Then
' Call to change the resolution process
Changedisp (1024, 768)
End If
End Sub

The program is run as shown in the following figure, click Set Resolution, will change the display to 1024x768, is it simple?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.