Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi Monitor Madness

Status
Not open for further replies.

HughLerwill

Programmer
Nov 22, 2004
1,818
GB
Dear All,

The function GetMonitorForForm defaults, as required, to the primary display when the vga lead to the secondary monitor is unplugged; however when the secondary monitor is simply powered down (Switched off and left plugged in) it behaves as if the secondary is still active. Is there a way to detect if the/ a secondary display is valid AND switched on?

'adapted with ref to Option Explicit

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

'Constants for the default return value when a valid monitor cannot be found
Private Enum dwFlags
MONITOR_DEFAULTTONULL = &H0 'If the monitor is not found, return 0
MONITOR_DEFAULTTOPRIMARY& = &H1 'If the monitor is not found, return the primary monitor
MONITOR_DEFAULTTONEAREST = &H2 'If the monitor is not found, return the nearest monitor
End Enum

Private Declare Function MonitorFromRect Lib "user32" (rc As RECT, ByVal dwFlags As dwFlags) As Long

Public Function GetMonitorForForm(Frm As Form) As Long

'Returns the Monitor Handle of the Monitor on which most of the Frm lies
' defaults to primary if most of Frm lies on secondary monitor and that monitor is disconnected (VGA lead unplugged)
'Problem: DOES not default to primary if secondary monitor (LCD; not tested on CRT yet) is simply switched off (i.e. it is still connected with VGA lead)

Dim Frect As RECT
Dim tppX&, tppY&

tppX = Screen.TwipsPerPixelX
tppY = Screen.TwipsPerPixelY
With Frect
.Left = Frm.Left \ tppX
.Top = Frm.Top \ tppY
.Right = .Left + Frm.Width \ tppX
.Bottom = .Top + Frm.Height \ tppY
End With

GetMonitorForForm = MonitorFromRect(Frect, MONITOR_DEFAULTTOPRIMARY)

End Function

Ref. VB6 SP6 on Vista Ultimate 32
 
The Win32_DesktopMonitor class may be what you are looking for:
Code:
Public Sub DesktopMonitor()
        
    Dim objWMIService As Object, objItem As Object, colItems
    Dim strComputer As String
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set colItems = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")
    
    For Each objItem In colItems
        Debug.Print objItem.DeviceID
        Debug.Print objItem.Availability
        Debug.Print objItem.ConfigManagerErrorCode
        Debug.Print objItem.Caption
        Debug.Print objItem.Description
        Debug.Print objItem.DisplayType
        Debug.Print objItem.MonitorManufacturer
        Debug.Print objItem.MonitorType
        Debug.Print objItem.Name
        Debug.Print objItem.Status
        Debug.Print objItem.StatusInfo
        Debug.Print objItem.SystemName
        Debug.Print
        Debug.Print
    Next
End Sub
For more info, see:
 

I tried using the CIM_Display class and still get an Availability status of 3 when the monitor is powered off, unplugged from the power supply, or not connected to the board.

I guess on W2000 and XP the ready state only matters to the OS whether the drivers are installed and the device in device manager is available when the system boots....Or it is beyond what I know.
 
SBert,
Thanks for the response but your WMI code only seems to report deets for the primary monitor for me.

I'm guessing that Windows detects a monitor if it is configured via Display properties and it is plugged-in/ connected. It seems to be left up to the user to check that the monitor is actually switched on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top