扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在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
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
''改变分辩率过程,参数一宽度,参数二高度
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
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者