在vb.net中,我们很容易获得显示器的分辩率,但是,要改变显示器的分辩率就比较麻烦了。由于。net的类库没有将 enumdisplaysettings 和ChangeDisplaySettings这两个API函数进行封装,但是我们得调用它们函数,相对于VB6来说,VB.NET调用API函数是有一 些小的改动!
下面,我们就尝试一下在vb.net中,使用这两个api函数。在VB.NET中改变显示器的分辨率
新建一个项目,在form1上添加两个按钮,一个名为btngetdisp,将其text属性设置为“得到分辩率”;另一个按钮名为btnsetdisp,text属性为“设置分辩率”。然后在代码窗口里添加以下代码:
Code:
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在VB.NET中改变显示器的分辨率
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
在VB.NET中改变显示器的分辨率
end Sub
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在VB.NET中改变显示器的分辨率
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
在VB.NET中改变显示器的分辨率
end Sub