VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 在VB中改变显示器的分辨率
发表评论(0)作者:, 平台:, 阅读:9410, 日期:2000-05-25
在VB中改变显示器的分辨率

重庆邮电学院

徐原

[请作者提供详细的邮政地址给我们]

有一些游戏如《Delta Force》可以让玩家在玩游戏时改变显示器的分辨率,现在介绍一种在VB中实现的方法。


这里要用到一个在VB的API浏览器中没有的函数,声明如下:


Private Declare Function ChangeDisplaySettings Lib "user32" Alias_ "ChangeDisplaySettingsA" (ByVal lpDevMode As Long, ByVal dwflags As Long) As Long


第一个参数指向一个DEVMODE结构,第二个参数为一些标志,这些标志有:



标志

含义

0

当前屏幕的图形模式将被动态地改变

CDS_UPDATEREGISTRY

当前屏幕的图形模式将被动态地改

变并且注册表里的屏幕分辨率的值也_

被更新

(注册表中保存有屏幕的分辨率及相关属性

,以便以后开机或重_

起时加载),

在USER文件中也保存该模式


CDS_TEST 仅供系统测试,

看这种图形模式是否能够正常


CDS_FULLSCREEN 临时改变

Windows NT:

如果切换到另外的桌面,该模式不会被保存


CDS_GLOBAL

该设置将被保存在全局设置区内,对所有用户都起作用


CDS_SET_PRIMARY

设置该设备为私有设备,这里对屏幕对象不起作用


CDS_RESET 恢复以前的设置



声明:

Private Declare Function

lstrcpy Lib "kernel32"

Alias "lstrcpyA" (lpString1 As_

Any, lpString2 As Any) As Long


Private Declare Function ChangeDisplaySettings

Lib "user32" Alias_

"ChangeDisplaySettingsA" (ByVal lpDevMode As Long,

ByVal dwflags As Long) As Long


Private Const CCHDEVICENAME = 32

Private Const CCHFORMNAME = 32


Private Type DEVMODE’详细参考MSDN

dmDeviceName As String * CCHDEVICENAME

dmSpecVersion As Integer

dmDriverVersion As Integer

dmSize As Integer

dmDriverExtra As Integer

dmFields As Long

dmOrientation As Integer

dmPaperSize As Integer

dmPaperLength As Integer

dmPaperWidth As Integer

dmScale As Integer

dmCopies As Integer

dmDefaultSource As Integer

dmPrintQuality As Integer

dmColor As Integer

dmDuplex As Integer

dmYResolution As Integer

dmTTOption As Integer

dmCollate As Integer

dmFormName As String * CCHFORMNAME

dmUnusedPadding As Integer

dmBitsPerPel As Integer

dmPelsWidth As Long

dmPelsHeight As Long

dmDisplayFlags As Long

dmDisplayFrequency As Long

End Type


Public Function SetScreen

(Width As Integer,

Height As Integer, Optional Color

As Integer = 16) As Long'这里的

16指的是真16色

Const DM_PELSWIDTH = &H80000

Const DM_PELSHEIGHT = &H100000

Const DM_BITSPERPEL = &H40000

Dim NewDevMode As DEVMODE

Dim pDevmode As Long


With NewDevMode

.dmSize = Len(NewDevMode)'一般为122

If Color = -1 Then

.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT

Else

.dmFields = DM_PELSWIDTH Or

DM_PELSHEIGHT Or DM_BITSPERPEL

End If

.dmPelsWidth = Width

.dmPelsHeight = Height

If Color < > -1 Then

.dmBitsPerPel = Color

End If

End With


pDevmode = lstrcpy(NewDevMode, NewDevMode)

SetDisplayMode = ChangeDisplaySettings(pDevmode, 0)

End Function




Private Sub Change_Click()

SetScreen Val(Text1), Val(Text2), Val(Text3)

End Sub


’下面三个文本框分别存放分辨率和颜色值

Private Sub Text1_GotFocus()

Text1.SelStart = 0

Text1.SelLength = Len(Text1)

End Sub


Private Sub Text2_GotFocus()

Text2.SelStart = 0

Text2.SelLength = Len(Text1)

End Sub


Private Sub Text3_GotFocus()

Text3.SelStart = 0

Text3.SelLength = Len(Text1)

End Sub



该程序在VB6.0企业版下调试通过。