Use VB. NET to change the display resolution.

Source: Internet
Author: User

In VB. NET, we can easily obtain the resolution of the monitor. However, it is troublesome to change the resolution of the monitor. Because. net class library does not encapsulate the enumdisplaysettings and changedisplaysettings APIs, but we have to call them. Compared with VB6, VB. net has some minor changes to call API functions!

Next, let's try using these two API functions in VB.net.

Create a new project and add two buttons on form1. One is btngetdisp and the text attribute is set to "get resolution". The other is btnsetdisp and the text attribute is "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

'Call an API Function

Private declare function enumdisplaysettings lib "USER32" alias

"Enumdisplaysettingsa" (byval lpszdevicename as integer,

Byval imodenum as integer, byref lpdevmode as devmode) as Boolean

'Call an API Function

Private declare function changedisplaysettings lib "USER32" alias

"Changedisplaysettingsa" (byref lpdevmode as devmode,

Byval dwflags as integer) as integer

'Define the structure

Private structure devmode

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

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 resolution, parameter 1 width, parameter 2 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

Do

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 frequency is 85

. Dmdisplayfrequency = 85

End

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 display 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

'The call changes the resolution process

Changedisp (1024,768)

End if

End sub

 

The program runs as shown in. Clicking set resolution will change the display resolution to 1024x768 and the refresh frequency to 85. Isn't it easy?

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.