How to determine Monitor resolution via VBA code

How can I use VBA code to determine the resoltuion of the monitor (eg 1280 x 1024)?
LVL 1
MilewskpAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nffvrxqgrcfqvvcCommented:
You can use GetMonitorInfo API

Option Explicit

Private Const MONITOR_DEFAULTTOPRIMARY As Long = &H1&

Private Type RECT
rLeft As Long
rTop As Long
rRight As Long
rBottom As Long
End Type

Private Type MONITORINFO
cbSize As Long
rcMonitor As RECT
rcWork As RECT
dwFlags As Long
End Type

Private Declare Function GetMonitorInfoW Lib "User32.dll" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long

Private Sub Form_Load()
  Dim mi As MONITORINFO
  mi.cbSize = Len(mi)
  GetMonitorInfoW MONITOR_DEFAULTTOPRIMARY, mi
  Debug.Print mi.rcMonitor.rRight; mi.rcMonitor.rBottom
End Sub

Open in new window

0
doctorzoomCommented:
Try this:

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0
Const SM_CYSCREEN = 1
 
Sub CheckScreenResolution()
     
    Dim x  As Long
    Dim y  As Long
         
    x = GetSystemMetrics(SM_CXSCREEN)
    y = GetSystemMetrics(SM_CYSCREEN)

'now you have screen resolution in x and y

end sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilewskpAuthor Commented:
For computers with multiple monitors, which one are the results for? is there a way to get the res of all monitors?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

nffvrxqgrcfqvvcCommented:
They are for the primary display. I don't have that type of rig but you could try the following code example.
'// Usage

EnumMonitors

Open in new window

'//
'// Module1.bas
'//

Option Explicit

Private Type RECT
rLeft As Long
rTop As Long
rRight As Long
rBottom As Long
End Type

Private Declare Function EnumDisplayMonitors Lib "User32.dll" (ByVal hdc As Long, ByVal lprcClip As Long, ByVal lpfnEnum As Long, ByVal dwData As Long) As Long

Public Function EnumMonitors()
  '// Enumerates all display monitors.
  EnumDisplayMonitors 0, 0, AddressOf MonitorEnumProc, 0
End Function

Public Function MonitorEnumProc(ByVal hMonitor As Long, ByVal hdcMonitor As Long, ByRef lprcMonitor As RECT, ByVal dwData As Long) As Long
  Debug.Print lprcMonitor.rRight; lprcMonitor.rBottom
  MonitorEnumProc = 1
End Function

Open in new window

0
MilewskpAuthor Commented:
Hi ,
It looks like GetSystemMetrics() can't give you the resolution of each monitor, but will give the Width and height, in pixels, of the virtual screen (the bounding rectangle of all display monitors). Supported in Windows CE .NET 4.0 and later.
- http://msdn.microsoft.com/en-us/library/aa929225.aspx 
0
MilewskpAuthor Commented:
Thanks to both of you for your input.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.