我正在使用以下代码:
Private Sub Form_Load()
ResWidth = Screen.Width \ Screen.TwipsPerPixelX
ResHeight = Screen.Height \ Screen.TwipsPerPixelY
ScreenRes = ResWidth & "x" & ResHeight
MsgBox (ScreenRes)
End Sub
Run Code Online (Sandbox Code Playgroud)
以及我在谷歌上搜索过的其他几个类似的代码。问题是,我总是收到一个消息框,说我的分辨率是 1200x1200,尽管我的实际分辨率是 1920x1200。为什么我会得到不好的结果?
不知道为什么这不起作用,但您可以利用 Windows API。
Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long
Run Code Online (Sandbox Code Playgroud)
然后当您需要屏幕宽度和高度时,定义这些常量:
Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Run Code Online (Sandbox Code Playgroud)
然后你就可以GetSystemMetrics
在任何你需要的地方使用它。如果将声明和常量添加到模块 (.BAS) 更有意义,则只需将声明和常量公开即可。
Dim width as Long, height as Long
width = GetSystemMetrics(SM_CXSCREEN)
height = GetSystemMetrics(SM_CYSCREEN)
Run Code Online (Sandbox Code Playgroud)
Microsoft 支持上的 GetSystemMetrics