首页 > 编程 > .NET > 正文

在VB.NET中改变显示器的分辩率

2024-07-10 13:02:37
字体:
来源:转载
供稿:网友
在vb.net中,我们很容易获得显示器的分辩率,但是,要改变显示器的分辩率就比较麻烦了。由于.net的类库没有将enumdisplaysettings 和changedisplaysettings这两个api函数进行封装,但是我们得调用它们函数,相对于vb6来说,vb.net调用api函数是有一些小的改动!
下面,我们就尝试一下在vb.net中,使用这两个api函数。
新建一个项目,在form1上添加两个按钮,一个名为btngetdisp,将其text属性设置为“得到分辩率”;另一个按钮名为btnsetdisp,text属性为“设置分辩率”。然后在代码窗口里添加以下代码:

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

'刷新频率常量
private const dm_displayfrequency as integer = &h400000

'调用api函数
private declare function enumdisplaysettings lib "user32" alias "enumdisplaysettingsa"
(byval lpszdevicename as integer, byval imodenum as integer, byref lpdevmode as devmode) as boolean
'调用api函数
private declare function changedisplaysettings lib "user32" alias "changedisplaysettingsa"
(byref lpdevmode as devmode, byval dwflags as integer) as integer

'定义结构
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

'改变分辩率过程,参数一宽度,参数二高度
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
'刷新频率为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("您的显示器分辨率是" & x & " x " & y)
end sub

private sub btnsetdisp_click(byval sender as system.object, byval e as system.eventargs) handles btnsetdisp.click
if msgbox("您确认要将显示器分辨率改为1024x768吗?", msgboxstyle.okcancel, "系统消息") = msgboxresult.ok then
'调用改变分辩率过程
changedisp(1024, 768)
end if
end sub

程序运行如下图所示,点击设置分辩率,将会把显示器分辨率改为1024x768,是不是很简单?注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表