2010-08-22 127 views
4

我使用下面的代码:如何在VB6中检测我的显示器分辨率?

Private Sub Form_Load() 
    ResWidth = Screen.Width \ Screen.TwipsPerPixelX 
    ResHeight = Screen.Height \ Screen.TwipsPerPixelY 
    ScreenRes = ResWidth & "x" & ResHeight 
    MsgBox (ScreenRes) 
End Sub 

而且我GOOGLE了其他几个类似的代码。问题是,我总是收到一个消息框,说我的分辨率是1200x1200,尽管我的实际分辨率是1920x1200。为什么我会得到不好的结果?

+0

添加screen.width,screen.height,twipsperpixelx,和你的消息框twipsperpixely值,您能得到什么? – jac 2010-08-22 05:04:25

+0

在我的系统上正常工作!好奇的问题.... – Dabblernl 2010-08-22 20:24:01

+1

我认为这应该工作,它看起来很好。系统有什么不寻常之处吗? – MarkJ 2010-08-23 08:17:52

回答

4

不知道为什么这样不起作用,但可以使用Windows API。

Private Declare Function GetSystemMetrics Lib "user32" _ 
    (ByVal nIndex As Long) As Long 

然后当你需要的屏幕宽度和高度,定义这些常量:

Private Const SM_CXSCREEN = 0 
Private Const SM_CYSCREEN = 1 

然后你可以使用GetSystemMetrics无论你需要它。如果将声明和常量添加到模块(.BAS)更有意义,那么只需公开声明和常量。

Dim width as Long, height as Long 
width = GetSystemMetrics(SM_CXSCREEN) 
height = GetSystemMetrics(SM_CYSCREEN) 

GetSystemMetrics on Microsoft Support

2

似乎存在与VB6 Screen对象的问题。根据KB253940 PRB: Incorrect Screen Object Width/Height After the Desktop Is Resized

在Visual Basic IDE中,Screen对象在屏幕分辨率更改后报告了一个不正确的桌面宽度值。当应用程序在IDE外部执行时,如果从系统托盘中的“显示属性”图标更改了分辨率,Screen对象的“宽度”和“高度”属性将返回不正确的值。

KB建议使用GetDeviceCaps API函数来解决此问题:

Private Declare Function GetDeviceCaps Lib "gdi32" _ 
     (ByVal hdc As Long, ByVal nIndex As Long) As Long 

Private Const HORZRES = 8 
Private Const VERTRES = 10 

Private Sub Form_Load() 
    ResWidth = GetDeviceCaps(Form1.hdc, HORZRES) 
    ResHeight = GetDeviceCaps(Form1.hdc, VERTRES) 
    ScreenRes = ResWidth & "x" & ResHeight 
    MsgBox (ScreenRes) 
End Sub