Monitors

Description

Here's a little sample that provides you with a means to enumerate all the available monitors on a system. Do you, perhaps, need to cover the entire screen with a single form? No problem! Drop the two provided modules, MMonitors.bas and CMonitor.cls, into your project and then add code something like this:

Color-coded with vbMarkUp - try it today!
Private Sub cmdCoverScreen_Click()
   ' Need to refresh collection at least once.
   Call MonitorsRefresh
   With Monitors("0")
      .PixelsToTwips = True
      Me.Move .Left, .Top, .Width, .Height
   End With
End Sub

The Monitors collection is keyed using the same values Windows uses to describe each monitor in the Display Properties dialog Settings tab. The astute among you will now be asking about Monitor Zero? That's a special one I added to the collection that represents the entire virtual desktop, so it therefore enables the call above to cover the entire screen.

Windows will also provide information about pseudo display devices, which are generally invisible and are often not part of the desktop. These critters are used by applications like Net Meeting to provide a display-like surface, and can generally be ignored for most application purposes. Nonetheless, you will find those at Key values higher than the number of actual physical monitors present on the system, and identifiable through the Visible and/or AttachedToDesktop properties of the CMonitor class.

I recently added some new functions that will facilitate repositioning a window where it was when the application was last shutdown. They go the extra mile of validating the coordinates, to be sure the user hasn't resized the display or changed the number of active monitors, and sliding an offscreen window back into visible space if desired. Also provided a very simple means of moving a window to the monitor where the mouse is, which may be a nice touch at application startup time?

Color-coded with vbMarkUp - try it today!
   ' Make sure form first appears on the monitor that
   ' the mouse is over.
   Call WindowSwitchMonitor(Me.hWnd, MonitorFromMouse())

CMonitor Properties

Handle The hMonitor value returned by EnumDisplayMonitors, MonitorFromPoint, MonitorFromRect, or MonitorFromWindow.
Index Long value that matches the Key used when building collection as well as how Windows identifies this monitor.
PixelsToTwips Boolean value that sets whether or not Pixel values are returned as Twips instead.
AttachedToDesktop Read only: Boolean value that indicates whether this display is part of the desktop.
Device Read only: String that represents the device name.
DeviceKey Read only: String that represents the device key.
DeviceString Read only: String that represents the device string.
DiminishedWorkspace Read only: Workspace is reduced by taskbar(s) to size smaller than monitor.
MultiMonitorSupport Read only: Boolean that indicates operating system support for multiple monitors.
Primary Read only: Boolean that indicates whether or not this is the primary monitor.
Removable Read only: Indicates whether the device is removeable.
VgaCompatible Read only: Indicates whether the device is VBA compatible.
Visible Read only: Interprets whether the device is visible based on the inverse of the DISPLAY_DEVICE_MIRRORING_DRIVER flag.
Bottom, WorkBottom Read only: Bottom coordinate for the display or work area.
Height, WorkHeight Read only: Height of the display or work area.
Left, WorkLeft Read only: Left coordinate for the display or work area.
Right, WorkRight Read only: Right coordinate for the display or work area.
Top, WorkTop Read only: Top coordinate for the display or work area.
Width, WorkWidth Read only: Width of the display or work area.

CMonitor Methods

Refresh                     Refreshes all cached information about this particular display device.

MMonitor Methods

MonitorCount Returns the number of physical monitors on the system.
MonitorSupport Indicates operating system support for multiple monitors. Effectively whether this system is Windows 98/2000 or better.
MonitorFromMouse Returns the number of the monitor that's under the mouse cursor.
MonitorFromPoint Returns the number of the monitor that's under the point (logical pixels) specified.
MonitorFromRect Returns the number of the monitor that the majority of the passed rectangle falls upon.
MonitorFromWindow Returns the number of the monitor that the majority of the passed window falls upon.
MonitorRect Returns a RECT structure that represents the display coordinates of the specified monitor number.
MonitorWorkRect Returns a RECT structure that represents the work area coordinates of the specified monitor number.
MonitorsRefresh Refreshes the global Monitors collection. Must be called at least once per application before accessing the collection directly.
FormatRect Returns cartesian coordinate display of rectangle coordinates. eg, "(0,0)-(1024,768)"
WindowMove Simple wrapper that accepts a RECT rather than individual coordinates around MoveWindow API.
WindowOffscreen Tests whether a window is wholly or partially offscreen, and optionally slides it back to nearest onscreen position if so.
WindowSwitchMonitor Moves a window from one monitor to the exact same coordinates on another monitor.
WindowRectAbsolute Simple wrapper around GetWindowRect API, which returns absolute window coordinates.
WindowRectRelative Returns coordinates of window relative to the monitor it principally occupies.

Published

This sample, or the one from which it originally derived, was published (or at least peripherally mentioned) in the following article(s):

APIs Usage

This sample uses the following API calls:

Module Library Function
CMonitor.cls kernel32

user32



GetVersionEx
RtlZeroMemory
EnumDisplayDevices
EqualRect
GetMonitorInfo
GetSystemMetrics
SystemParametersInfo
CStringBuilder.cls kernel32
msvbvm60
RtlMoveMemory
VarPtr
MMonitors.bas user32










EnumDisplayMonitors
EqualRect
GetCursorPos
GetSystemMetrics
GetWindowRect
IntersectRect
MonitorFromPoint
MonitorFromRect
MonitorFromWindow
MoveWindow
OffsetRect
UnionRect

Don't see what you're looking for? Here's a complete API cross-reference.

Download

Download Monitors.zip   Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution.

Download Monitors.zip, 27Kb, Last Updated: Friday, August 7, 2009

See Also

The following resources may also be of interest: