在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
|