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