Link to home
Start Free TrialLog in
Avatar of Milewskp
MilewskpFlag for Canada

asked on

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)?
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

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

ASKER CERTIFIED SOLUTION
Avatar of doctorzoom
doctorzoom

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Milewskp

ASKER

For computers with multiple monitors, which one are the results for? is there a way to get the res of all monitors?
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

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 
Thanks to both of you for your input.