日期:2012-07-05  浏览次数:20379 次

在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,是不是很简单?