在VB.NET中改变显示器的分辩率
在vb.net中,我们很容易获得显示器的分辩率,但是,要改变显示器的分辩率就比较麻烦了。由于.net的类库没有将enumdisplaysettings 和ChangeDisplaySettings这两个API函数进行封装,但是我们得调用它们函数,相对于VB6来说,VB.NET调用API函数是有一些小的改动!在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
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
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
注:这是我在网上找到的到目前为止能在在vb.net中,我们很容易获得显示器的分辩率,但是,要改变显示器的分辩率就比较麻烦了。由于.net的类库没有将enumdisplaysettings 和ChangeDisplaySettings这两个API函数进行封装,但是我们得调用它们函数,相对于VB6来说,VB.NET调用API函数是有一些小的改动!
在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
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
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
注:这是我在网上找到的到目前为止能在VB2005运行的最为完善的程序,但还是有一句出错哪位大侠知道原因?
blnworked = EnumDisplaySettings(0, i, DevM)这里出错了 --------------------编程问答-------------------- 很高兴你是先自己搜索,才来提问的,我很乐意帮你找问题。
你去掉Form1.就可以了。 --------------------编程问答--------------------
--------------------编程问答-------------------- 做法和那个文章的一样。添加2个按钮修改按钮的名称。 --------------------编程问答-------------------- 路过,有点长 --------------------编程问答--------------------
Public Class form2
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
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 New 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
End Class
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。 --------------------编程问答-------------------- 好东西,顶一下,不过我以前就这样试了不行! --------------------编程问答-------------------- 那次也搞了一下,不过貌似没成功,这组API在VB.NET下调用时就是光些毛病,鬼知道。搞好了的时候再来拜读,复制粘贴并保存到硬盘! --------------------编程问答--------------------
<StructLayout(LayoutKind.Sequential)> _
Public Structure DEVMODE
<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 32)> _
Public dmDeviceName As String
Public dmSpecVersion As Short
Public dmDriverVersion As Short
Public dmSize As Short
Public dmDriverExtra As Short
Public dmFields As DM
Public dmOrientation As Short
Public dmPaperSize As Short
Public dmPaperLength As Short
Public dmPaperWidth As Short
Public dmScale As Short
Public dmCopies As Short
Public dmDefaultSource As Short
Public dmPrintQuality As Short
Public dmColor As Short
Public dmDuplex As Short
Public dmYResolution As Short
Public dmTTOption As Short
Public dmCollate As Short
<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 32)> _
Public dmFormName As String
Public dmLogPixels As Short
Public dmBitsPerPel As Integer ' Declared wrong in the full framework
Public dmPelsWidth As Integer
Public dmPelsHeight As Integer
Public dmDisplayFlags As Integer
Public dmDisplayFrequency As Integer
Public dmICMMethod As Integer
Public dmICMIntent As Integer
Public dmMediaType As Integer
Public dmDitherType As Integer
Public dmReserved1 As Integer
Public dmReserved2 As Integer
Public dmPanningWidth As Integer
Public dmPanningHeight As Integer
Public dmPositionX As Integer ' Using a PointL Struct does not work
Public dmPositionY As Integer
End Structure
http://www.codeproject.com/KB/dotnet/Display_Settings.aspx --------------------编程问答-------------------- 好长的CODE
补充:.NET技术 , VB.NET