科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件在VB.NET中改变显示器的分辩率

在VB.NET中改变显示器的分辩率

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在VB.NET中,我们很容易获得显示器的分辩率,但是,要改变显示器的分辩率就比较麻烦了。由于.NET的类库没有将EnumDisplaySettings 和ChangeDisplaySettings这两个API函数进行封装,但是我们得调用它们函数,相对于VB6来说,VB.NET调用API函数是有一些小的改动!

作者:李洪根 来源:soft6 2008年5月23日

关键字: 显示器 改变 VB vb.net Windows

  • 评论
  • 分享微博
  • 分享邮件
在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


 

[下一页]


 

  ''改变分辩率过程,参数一宽度,参数二高度
  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

  程序运行如下图所示,点击设置分辩率,将会把显示器分辨率改为1024x768,刷新频率为85,是不是很简单?

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章