在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
-
-
private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Boolean
-
-
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 Shortdim dmCopies As Shortdim dmDefaultSource As Shortdim dmPrintQuality As Shortdim dmColor As Short
-
dim dmDuplex As Shortdim dmYResolution As Shortdim dmTTOption As Shortdim 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 Integerdim dmDisplayFrequency As Integerend 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
-
-
.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,是不是很简单?
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gujiawei_27/article/details/6469635