Reputation: 87
I am using the following code:
Private Sub Form_Load()
ResWidth = Screen.Width \ Screen.TwipsPerPixelX
ResHeight = Screen.Height \ Screen.TwipsPerPixelY
ScreenRes = ResWidth & "x" & ResHeight
MsgBox (ScreenRes)
End Sub
And several other similar codes I've googled for. The problem is, I always get a message box saying that my resolution is 1200x1200, although my actual resolution is 1920x1200. Why am I getting bad results?
Upvotes: 4
Views: 15456
Reputation: 1
If you are working with any Windows Mobile, the way is a bit different. You need to use the following formula:
Width = (Screen.PrimaryScreen.WorkingArea.Width * 0.32)
In the previous example is assigned the 32% of the screen width to width
variable.
Upvotes: 0
Reputation: 3220
It appears that there is a problem with Screen object in VB6. As per KB253940 PRB: Incorrect Screen Object Width/Height After the Desktop Is Resized:
Inside the Visual Basic IDE, the Screen object reports an incorrect value for the desktop width after the screen resolution is changed. When the application is executing outside the IDE, the Width and Height properties of the Screen object return incorrect values if the resolution is changed from the Display Properties icon in the System Tray.
KB suggests to use GetDeviceCaps API function to work around the problem:
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
Upvotes: 2
Reputation: 18252
Not sure why that doesn't work, but you could tap into the Windows API.
Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long
And then when you need the screen width and height, define these constants:
Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Then you can use GetSystemMetrics
wherever you need it. If it makes more sense to add the declaration and constants to a Module (.BAS), then just make the declaration and constants public.
Dim width as Long, height as Long
width = GetSystemMetrics(SM_CXSCREEN)
height = GetSystemMetrics(SM_CYSCREEN)
GetSystemMetrics on Microsoft Support
Upvotes: 5