关于分辨率的 问题
话说 想编一个游戏玩,就是类似于 帝国或者三国志那样的,鼠标移动到边缘时,地图能够能够自由的移动。可以实现了,但是有个问题一直解决不了。
我是笔记本电脑,一般的分辨率是 1366*768 因此 找了一段代码能够实现 设置分辨率,但是当程序运行的时候,画图怎么也画不满整个屏幕,右边总是留一段空白。
如果通过点击桌面---属性--设置好分辨率1024*768,不用程序代码设置,程序运行的时候能够画满整个屏幕。
但是作为游戏来说,必须是有游戏来自动识别原来的分辨率,然后设定游戏所需要分辨率,才是完美。
具体代码如下:
Dim Old_width, Old_height As Single '定义原来的分辨率
Dim DevM As DEVMODE '设置分辨率所用
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Long '设定分辨率所用
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long '设定分辨率所用
Private Declare Function GdiTransparentBlt Lib "gdi32" (ByVal hdc1 As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal W1 As Long, ByVal H1 As Long, ByVal Hdc2 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal W2 As Long, ByVal H2 As Long, ByVal Color As Long) As Long '绘图所用
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long '获取鼠标位置所用
'========以下设定分辨率所用
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const ENUM_CURRENT_SETTINGS = -1
Private Type DEVMODE
dmDeviceName As String * CCDEVICENAME
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 * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
'==完毕
'==以下获取鼠标位置所用
Private Type POINTAPI
x As Long
y As Long
End Type
Dim p As POINTAPI
'==完毕
Dim Xm, Ym As Single '设定地图从什么位置开始绘图
Private Sub Form_Load()
Xm = 0 '定义初始绘图时地图从什么坐标开始画
Ym = 0 '定义初始绘图时地图从什么坐标开始画
'获取原来的分辨率,并保存在变量中
EnumDisplaySettings 0, ENUM_CURRENT_SETTINGS, DevM
Old_width = DevM.dmPelsWidth
Old_height = DevM.dmPelsHeight
'获取原来的分辨率完毕
'设置目前的分辨率
EnumDisplaySettings 0, 0, DevM
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
DevM.dmPelsWidth = 1024 '设定成想要的分辨率
DevM.dmPelsHeight = 768
ChangeDisplaySettings DevM, 0
'设置完毕
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 2 Then
If MsgBox("退出吗?", vbOKCancel, "询问") = 1 Then
EnumDisplaySettings 0, 0, DevM
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
DevM.dmPelsWidth = Old_width '设回原分辨率
DevM.dmPelsHeight = Old_height
ChangeDisplaySettings DevM, 0
Unload Me
End If
End If
End Sub
Private Sub Timer1_Timer() '时间控件,没100毫秒重复一次
GetCursorPos p '获取鼠标的位置
Text1.Text = p.x '测试用,最后删掉
Text2.Text = p.y '测试用,最后删掉
'当鼠标的位置处于上下左右指定的位置时,修改Xm,YM使的开始绘制的地图发生变化,实现地图的移动
If p.x < 15 Then
If Xm - 15 <= 0 Then
Xm = 0
Else
Xm = Xm - 15
End If
End If
If p.x > 1020 Then
If Xm + 15 > Me.picture1.ScaleWidth - 1024 Then
Xm = Me.picture1.ScaleWidth - 1024
Else
Xm = Xm + 15
End If
End If
If p.y < 15 Then
If Ym - 15 < 0 Then
Ym = 0
Else
Ym = Ym - 15
End If
End If
If p.y > 750 Then
If Ym + 15 > Me.picture1.ScaleHeight - 768 Then
Ym = Me.picture1.ScaleHeight - 768
Else
Ym = Ym + 15
End If
End If
Me.Cls
GdiTransparentBlt Me.hdc, 0, 0, 1024, 768, Me.picture1.hdc, Xm, Ym, 1024, 768, RGB(255, 255, 255) '绘制地图
End Sub
说明:表单为 无标题栏 可擦写 一个picture1 两个 text 一个 time 控件
下边就是运行的结果,已经实现了类似于帝国时代那种地图的移动方式,但就是画不满,为什么啊,请帮忙分析一下