使用VB.NET开发的程序员知道很容易获得显示器的分辩率,但是,要改变VB.NET分辩率是一件很让人头疼的事。由于VB.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)
- ii = 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
程序运行所示,点击设置VB.NET分辩率,将会把显示器分辨率改为1024x768,刷新频率为85,是不是很简单?
【编辑推荐】