HughLerwill
Programmer
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 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