-
Dec 2nd, 2021, 01:57 PM
#1
Thread Starter
Hyperactive Member
Test if second Monitor switched on
I have an app that was opened and moved to show on the 2nd monitor at work.
We sometimes work from home and only have one monitor.
Today I had to go into the settings to reset the Left property of the form to Zero
so that the form would be visible on Monitor 1.
I am looking for a way at program startup that can check if the 2nd monitor is active, if not then
reset Left to Zero automatically so it can be seen on the a single monitor.
Pls note that we are connecting via remote desktop.
can anyone help.
tks
Last edited by Shaggy Hiker; Dec 4th, 2021 at 07:43 PM.
-
Dec 2nd, 2021, 03:45 PM
#2
Re: Test if second Monitor switched on
Form1.frm:
Code:
Option Explicit
Private Const WIN32_NULL As Long = 0
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" ( _
ByVal hWnd As Long, _
ByRef RECT As RECT) As Long
Private Enum MFR_FLAGS
MONITOR_DEFAULTTONULL = &H0&
MONITOR_DEFAULTTOPRIMARY = &H1&
MONITOR_DEFAULTTONEAREST = &H2&
End Enum
Private Declare Function MonitorFromRect Lib "user32" ( _
ByRef RECT As RECT, _
ByVal dwFlags As MFR_FLAGS) As Long
Private Sub cmdIsForm2OnScreen_Click()
Dim RECT As RECT
If GetWindowRect(Form2.hWnd, RECT) Then
If MonitorFromRect(RECT, MONITOR_DEFAULTTONULL) = WIN32_NULL Then
MsgBox "No, it is offscreen"
Else
MsgBox "Yes"
End If
Else
MsgBox "GetWindowRect error " & CStr(Err.LastDllError)
End If
End Sub
Private Sub cmdMoveForm2ToPrimary_Click()
Form2.Move 0, 0
End Sub
Private Sub cmdShowForm2_Click()
Form2.Show vbModeless, Me
End Sub
Form2.frm:
Code:
Option Explicit
Private Sub cmdMoveOffScreen_Click()
Move -2 * Width, -2 * Height 'Or somewhere else not on any monitor.
End Sub
-
Dec 2nd, 2021, 04:07 PM
#3
Re: Test if second Monitor switched on
In a BAS module:
Code:
Option Explicit
Public Type RECT
Left As Long
Top As Long
Right As Long ' This is +1 (right - left = width)
Bottom As Long ' This is +1 (bottom - top = height)
End Type
Private Declare Function EnumDisplayMonitors Lib "user32" (ByVal hDC As Long, lprcClip As Any, ByVal lpfnEnum As Long, dwData As Long) As Long
Public Function MonitorCount() As Long
' This does NOT count disabled monitors.
EnumDisplayMonitors 0&, ByVal 0&, AddressOf MonitorCountEnum, MonitorCount
End Function
Private Function MonitorCountEnum(ByVal hMonitor As Long, ByVal hdcMonitor As Long, uRect As RECT, dwData As Long) As Long
dwData = dwData + 1
MonitorCountEnum = 1 ' Count them all.
End Function
Quick test in Form1:
Code:
Private Sub Form_Load()
Debug.Print MonitorCount
End Sub
Ahhh, Dil beat me to the "Submit Reply" button, but mine's less code.
Last edited by Elroy; Dec 2nd, 2021 at 04:11 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 2nd, 2021, 04:12 PM
#4
Re: Test if second Monitor switched on
I fail to see how a count of monitors helps much. What if there are 3 monitors? Or 2 but arranged differently from the program's startup assumptions about Form positioning?
Better to test whether the Form in question is displayed on any monitor, and if not move it where it will be visible. Then the user can drag it wherever after that.
-
Dec 2nd, 2021, 04:13 PM
#5
Re: Test if second Monitor switched on
I thought we were just testing to see if we had one monitor or two.
Ahhh, I see. Yeah, that sometimes happens to me too, but I typically do my development on my primary monitor, regardless of where that's positioned among the monitors.
However, in my primary application, I do have certain forms that "remember" where they were last. But, they also must check to make sure they're visible.
Let me get that code too. Ahhhh, here's some code on how I do it:
Code:
Top = GetSetting(App.Title, "Settings", "TekscanControllerTop", -15&)
Left = GetSetting(App.Title, "Settings", "TekscanControllerLeft", -15&)
If Not FormIsFullyOnMonitor(Me) Then CenterOnPrimaryMonitor Me
k_zeon, if you think that FormIsFullyOnMonitor procedure or the CenterOnPrimaryMonitor procedure will help you, let me know.
Otherwise, to do it in the IDE would take an Add-In. I've never found the need, as setting the Left property on the Properties Window to zero is easy enough.
Last edited by Elroy; Dec 2nd, 2021 at 04:23 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 2nd, 2021, 04:14 PM
#6
Re: Test if second Monitor switched on
Silly wabbit, thread titles are for obfuscation.
-
Dec 2nd, 2021, 05:17 PM
#7
Thread Starter
Hyperactive Member
Re: Test if second Monitor switched on
Originally Posted by Elroy
I thought we were just testing to see if we had one monitor or two.
Ahhh, I see. Yeah, that sometimes happens to me too, but I typically do my development on my primary monitor, regardless of where that's positioned among the monitors.
However, in my primary application, I do have certain forms that "remember" where they were last. But, they also must check to make sure they're visible.
Let me get that code too. Ahhhh, here's some code on how I do it:
Code:
Top = GetSetting(App.Title, "Settings", "TekscanControllerTop", -15&)
Left = GetSetting(App.Title, "Settings", "TekscanControllerLeft", -15&)
If Not FormIsFullyOnMonitor(Me) Then CenterOnPrimaryMonitor Me
k_zeon, if you think that FormIsFullyOnMonitor procedure or the CenterOnPrimaryMonitor procedure will help you, let me know.
Otherwise, to do it in the IDE would take an Add-In. I've never found the need, as setting the Left property on the Properties Window to zero is easy enough.
Hi Elroy.
Just after i posted , i did find code that checks to see if on visible screen ,if not then it puts it there.
I found it on this website a function called EnsureFormIsInsideMonitor me
Code:
https://www.vbforums.com/showthread.php?697863-VB-Appliation-on-Extended-Monitors&highlight=EnsureFormIsInsideMonitor
I have not tested yet as i dont have a second monitor attached to my development machine, but i will have tomo.
I am hoping that if i exit my app in screen 2 and save its form positions.
If i then switch off screen 2 and then re run my app, it should then show on monitor 1.
I would be very interested in your ' If Not FormIsFullyOnMonitor(Me) Then CenterOnPrimaryMonitor Me ' functions.
tks
-
Dec 2nd, 2021, 05:24 PM
#8
Thread Starter
Hyperactive Member
Re: Test if second Monitor switched on
Originally Posted by dilettante
Form1.frm:
Code:
Option Explicit
Private Const WIN32_NULL As Long = 0
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" ( _
ByVal hWnd As Long, _
ByRef RECT As RECT) As Long
Private Enum MFR_FLAGS
MONITOR_DEFAULTTONULL = &H0&
MONITOR_DEFAULTTOPRIMARY = &H1&
MONITOR_DEFAULTTONEAREST = &H2&
End Enum
Private Declare Function MonitorFromRect Lib "user32" ( _
ByRef RECT As RECT, _
ByVal dwFlags As MFR_FLAGS) As Long
Private Sub cmdIsForm2OnScreen_Click()
Dim RECT As RECT
If GetWindowRect(Form2.hWnd, RECT) Then
If MonitorFromRect(RECT, MONITOR_DEFAULTTONULL) = WIN32_NULL Then
MsgBox "No, it is offscreen"
Else
MsgBox "Yes"
End If
Else
MsgBox "GetWindowRect error " & CStr(Err.LastDllError)
End If
End Sub
Private Sub cmdMoveForm2ToPrimary_Click()
Form2.Move 0, 0
End Sub
Private Sub cmdShowForm2_Click()
Form2.Show vbModeless, Me
End Sub
Form2.frm:
Code:
Option Explicit
Private Sub cmdMoveOffScreen_Click()
Move -2 * Width, -2 * Height 'Or somewhere else not on any monitor.
End Sub
tks dilettante. will try this tomo
-
Dec 2nd, 2021, 05:30 PM
#9
Re: Test if second Monitor switched on
It's a bit of code, and it must go into a BAS module. I stripped it out of a much larger module that does all kinds of things with monitors.
But here's the fully functioning code for FormIsFullyOnMonitor and CenterOnPrimaryMonitor:
Code:
Option Explicit
'
Public Type RECT
Left As Long
Top As Long
Right As Long ' This is +1 (right - left = width)
Bottom As Long ' This is +1 (bottom - top = height)
End Type
Private Type MONITORINFO
cbSize As Long
rcMonitor As RECT
rcWork As RECT
dwFlags As Long
End Type
'
Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Public Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, Optional ByVal bRepaint As Long = 1&) As Long
'
Private Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long
Private Declare Function MonitorFromWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal dwFlags As Long) As Long
Private Declare Function EnumDisplayMonitors Lib "user32" (ByVal hDC As Long, lprcClip As Any, ByVal lpfnEnum As Long, dwData As Long) As Long
'
Public Function FormIsFullyOnMonitor(frm As Form) As Boolean
' This tells us whether or not a form is FULLY visible on its monitor.
'
Dim hMonitor As Long
Dim r1 As RECT
Dim r2 As RECT
Dim uMonInfo As MONITORINFO
'
hMonitor = hMonitorForForm(frm)
GetWindowRect frm.hWnd, r1
uMonInfo.cbSize = LenB(uMonInfo)
GetMonitorInfo hMonitor, uMonInfo
r2 = uMonInfo.rcWork
'
FormIsFullyOnMonitor = (r1.Top >= r2.Top) And (r1.Left >= r2.Left) And (r1.Bottom <= r2.Bottom) And (r1.Right <= r2.Right)
End Function
Public Sub CenterOnPrimaryMonitor(frm As Form, _
Optional bUseWorkingArea As Boolean = True, _
Optional iWidthDivisor As Long = 2&, _
Optional iHeightDivisor As Long = 3&)
' Works with new Win10 scaling.
'
' This will work anytime after (or within) Form_Load.
Dim hMonitor As Long
hMonitor = hPrimaryMonitor
CenterFormOnMonitor frm, hPrimaryMonitor, True, iWidthDivisor, iHeightDivisor
If frm.Top < 0 Then frm.Top = 0
End Sub
Public Function hMonitorForForm(frm As Form) As Long
' The monitor that the window is MOSTLY on.
Const MONITOR_DEFAULTTONULL = &H0
hMonitorForForm = MonitorFromWindow(frm.hWnd, MONITOR_DEFAULTTONULL)
End Function
Public Function hPrimaryMonitor() As Long
' This is the primary monitor.
EnumDisplayMonitors 0&, ByVal 0&, AddressOf PrimaryMonitorHandleEnum, hPrimaryMonitor
End Function
Public Sub CenterFormOnMonitor(frm As Form, _
Optional ByVal hMonitor As Long = True, _
Optional bUseWorkingArea As Boolean, _
Optional iWidthDivisor As Long = 2&, _
Optional iHeightDivisor As Long = 3&)
' This works with new Win10 scaling.
Dim lLeft As Long
Dim lTop As Long
'
If MonitorWidthPx(hMonitor) = 0 Then
hMonitor = hPrimaryMonitor
If hMonitor = 0 Then Exit Sub ' Something wrong.
End If
If bUseWorkingArea Then
lLeft = (MonitorWidthPx(hMonitor, True) - WindowWidthInPixels(frm.hWnd)) \ iWidthDivisor
lTop = (MonitorHeightPx(hMonitor, True) - WindowHeightInPixels(frm.hWnd)) \ iHeightDivisor
Else
lLeft = (MonitorWidthPx(hMonitor, False) - WindowWidthInPixels(frm.hWnd)) \ iWidthDivisor
lTop = (MonitorHeightPx(hMonitor, False) - WindowHeightInPixels(frm.hWnd)) \ iHeightDivisor
End If
PositionFormOnMonitor frm, hMonitor, lLeft, lTop
End Sub
Public Sub PositionFormOnMonitor(frm As Form, _
hMonitor As Long, _
ByVal lLeftPixel As Long, _
ByVal lTopPixel As Long)
' This can be used to position windows on other programs so long as you have the hWnd.
Dim lHeight As Long
Dim lWidth As Long
'
lHeight = WindowHeightInPixels(frm.hWnd)
lWidth = WindowWidthInPixels(frm.hWnd)
'
lLeftPixel = lLeftPixel + MonitorLeftPx(hMonitor)
lTopPixel = lTopPixel + MonitorTopPx(hMonitor)
'
MoveWindow frm.hWnd, lLeftPixel, lTopPixel, lWidth, lHeight
End Sub
Public Function MonitorWidthPx(hMonitor As Long, Optional bUseWorkingArea As Boolean) As Long
Dim uMonInfo As MONITORINFO
'
uMonInfo.cbSize = LenB(uMonInfo)
If GetMonitorInfo(hMonitor, uMonInfo) = 0 Then Exit Function
'
If bUseWorkingArea Then ' The "Work" verions exclude the taskbar.
MonitorWidthPx = uMonInfo.rcWork.Right - uMonInfo.rcWork.Left
Else
MonitorWidthPx = uMonInfo.rcMonitor.Right - uMonInfo.rcMonitor.Left
End If
End Function
Public Function MonitorHeightPx(hMonitor As Long, Optional bUseWorkingArea As Boolean) As Long
Dim uMonInfo As MONITORINFO
uMonInfo.cbSize = LenB(uMonInfo)
If GetMonitorInfo(hMonitor, uMonInfo) = 0 Then Exit Function
'
If bUseWorkingArea Then ' The "Work" verions exclude the taskbar.
MonitorHeightPx = uMonInfo.rcWork.Bottom - uMonInfo.rcWork.Top
Else
MonitorHeightPx = uMonInfo.rcMonitor.Bottom - uMonInfo.rcMonitor.Top
End If
End Function
Public Function MonitorLeftPx(hMonitor As Long, Optional bUseWorkingArea As Boolean) As Long
Dim uMonInfo As MONITORINFO
uMonInfo.cbSize = LenB(uMonInfo)
If GetMonitorInfo(hMonitor, uMonInfo) = 0 Then Exit Function
'
If bUseWorkingArea Then ' The "Work" verions exclude the taskbar.
MonitorLeftPx = uMonInfo.rcWork.Left
Else
MonitorLeftPx = uMonInfo.rcMonitor.Left
End If
End Function
Public Function MonitorTopPx(hMonitor As Long, Optional bUseWorkingArea As Boolean) As Long
Dim uMonInfo As MONITORINFO
uMonInfo.cbSize = LenB(uMonInfo)
If GetMonitorInfo(hMonitor, uMonInfo) = 0 Then Exit Function
'
If bUseWorkingArea Then ' The "Work" verions exclude the taskbar.
MonitorTopPx = uMonInfo.rcWork.Top
Else
MonitorTopPx = uMonInfo.rcMonitor.Top
End If
End Function
Public Function WindowHeightInPixels(hWndOfInterest As Long) As Long
Dim r As RECT
GetWindowRect hWndOfInterest, r
WindowHeightInPixels = r.Bottom - r.Top
End Function
Public Function WindowWidthInPixels(hWndOfInterest As Long) As Long
Dim r As RECT
GetWindowRect hWndOfInterest, r
WindowWidthInPixels = r.Right - r.Left
End Function
Private Function PrimaryMonitorHandleEnum(ByVal hMonitor As Long, ByVal hdcMonitor As Long, uRect As RECT, dwData As Long) As Long
Const MONITORINFOF_PRIMARY = &H1
Dim uMonInfo As MONITORINFO
uMonInfo.cbSize = LenB(uMonInfo)
GetMonitorInfo hMonitor, uMonInfo
If uMonInfo.dwFlags = MONITORINFOF_PRIMARY Then
dwData = hMonitor
PrimaryMonitorHandleEnum = 0 ' Found it.
Else
PrimaryMonitorHandleEnum = 1 ' Keep looking.
End If
End Function
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 3rd, 2021, 11:55 AM
#10
Re: [RESOLVED] Test if second Monitor switched on
>I have not tested yet as i dont have a second monitor attached to my development machine, but i will have tomo.
So how's it going?
-
Dec 3rd, 2021, 04:50 PM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
Originally Posted by Magic Ink
>I have not tested yet as i dont have a second monitor attached to my development machine, but i will have tomo.
So how's it going?
I'm glad you asked. Not so good. I have tried all options i have been given and none seem to work as expected.
Elroy & dilettante , i tried the codes above and the ones i mentioned and neither do what i need. can you help
I have a system setup with 2 monitors. Monitor 1 is Primary, Monitor 2 is Secondary
I run an app ie just a single form. As it loads it positions itself based on where it was last. ie on the 2nd monitor.
I close the app. Switch off monitor 2.
If i now re run the app, it shows in the task bar, but not on Monitor 1. Its still showing on Monitor 2 even though its not switched on.
I need to find a way that if monitor 2 is not on, then it defaults to monitor 1.
Just tried this and its does not appear to do that. I have been googling for a few hours and not sure there is an api to check the power state of a monitor.
If i could do a check at startup like If Monitor2 = off then put form on Monitor1
any help
tks
Last edited by k_zeon; Dec 3rd, 2021 at 05:16 PM.
-
Dec 3rd, 2021, 05:15 PM
#12
Re: [RESOLVED] Test if second Monitor switched on
Ok, we're talking about a compiled EXE?
If that's the case, my code in post #5 (with the support code in post #9) should do it.
If we're talking about designer forms in the IDE, that'll take an Add-In.
k_zeon, maybe post an example of what's not working for you.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 3rd, 2021, 05:18 PM
#13
Re: [RESOLVED] Test if second Monitor switched on
Ok, another problem. If you just turn a monitor off, Windows doesn't seem to be smart enough to get rid of that monitor from the desktop. At a minimum, you need to logoff and log back on. Is that what we're talking about?
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 3rd, 2021, 05:23 PM
#14
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
Originally Posted by Elroy
Ok, we're talking about a compiled EXE?
If that's the case, my code in post #5 (with the support code in post #9) should do it.
If we're talking about designer forms in the IDE, that'll take an Add-In.
k_zeon, maybe post an example of what's not working for you.
Hi Elroy
i have tested your example on a system that has 2 monitors.
I can drag my exe form from Monitor 1 to Monitor 2
I can then close the app.exe . Re Open and it opens on Monitor 2. All good so far.
If i close my exe form and then manually power of Monitor 2 and the reopen my exe it will show on the taskbar of Monitor 1
but its not shown on Monitor 1, its shown on Monitor 2 even though Monitor 2 is off. I need to switch Monitor 2 back on and drag the form exe back to Monitor 1
I have a project that i can zip up but not sure where to post it.?
tks
-
Dec 3rd, 2021, 05:23 PM
#15
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
Originally Posted by Elroy
Ok, another problem. If you just turn a monitor off, Windows doesn't seem to be smart enough to get rid of that monitor from the desktop. At a minimum, you need to logoff and log back on. Is that what we're talking about?
yes correct.
-
Dec 3rd, 2021, 05:25 PM
#16
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
Originally Posted by Elroy
Ok, another problem. If you just turn a monitor off, Windows doesn't seem to be smart enough to get rid of that monitor from the desktop. At a minimum, you need to logoff and log back on. Is that what we're talking about?
I did some research and dont think the is an API to detect the powerstate of monitors. Maybe wrong ?
-
Dec 3rd, 2021, 06:10 PM
#17
Re: [RESOLVED] Test if second Monitor switched on
With reference to my code from https://www.vbforums.com/showthread....sInsideMonitor which you referenced in post #7 there is a rem in there which states '' * disconnected means unplugged; a monitor which is switched off remains 'connected' as long as it is plugged-in via e.g. its VGA lead." I never found a way or API to overcome that short coming.
I seem to remember that the 'problem' persists after a reboot and the computer continues to 'use' the physically connected but unwanted monitor even when it is turned off, the monitor has to be physically disconnected to correct the situation. It could be that the situation (Windows OS) has changed since it was a hot topic for me back in the early 20teens, eg. Windows now allows DPI to be set on a per monitor basis.
-
Dec 3rd, 2021, 07:32 PM
#18
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
So, does anyone know if what i am after can be done. Some obscure API that can get the powerstate of a specific monitor. tks
-
Dec 3rd, 2021, 07:46 PM
#19
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
I found this posted. Does anyone know how to use this in VB6 if at all possible. tks
Code:
https://www.generacodice.com/en/articolo/45110/Is-there-any-way-to-detect-the-monitor-state-in-Windows-(on-or-off)
This is a really old post but if it can help someone, I have found a solution to detect a screen being available or not : the Connecting and Configuring Displays (CCD) API of Windows.
It's part of User32.ddl and the interesting functions are GetDisplayConfigBufferSizes and QueryDisplayConfig. It give us all informations that can be viewed in the Configuration Panel of windows.
In particular the PathInfo contains a TargetInfo property that have a targetAvailable flag. This flag seems to be correctly updated on all the configurations I have tried so far.
This allow you to know the state of every screens connected to the PC and set their configurations.
Code:
https://github.com/regueiro/CCDWrapper/tree/master/CCDWrapper
Last edited by k_zeon; Dec 3rd, 2021 at 07:50 PM.
-
Dec 4th, 2021, 08:15 AM
#20
Thread Starter
Hyperactive Member
Get Monitor Powerstate
Hi. I have asked this question before and thought i had the answer. I dont.
I need to check the powerstate of a 2nd Monitor when i start an app exe
If 2nd Monitor On then position as before on 2nd Monitor.
If 2nd Monitor Off then Position on Primary Monitor.
Is there an API ir some other method to check the power state of monitors
tks
-
Dec 4th, 2021, 03:01 PM
#21
Re: [RESOLVED] Test if second Monitor switched on
Could be I am missing something but I wonder if this issue is a serious one.
The typical situation is a laptop which is used at home with the built-in display only, and used at work where an additional monitor is available.
So you are at work using the application on the additional monitor and close it down while it is on that monitor.
Then you go home having disconnected/unplugged the additional monitor. With reference to the code discussed above it is possible to determine that Form placement must now be shifted to the primary and only display; and that done all is well.
Then you go back to work and the app opens on the built-in monitor, if you want it on the additional monitor you just drag it there. You may apply further code as the app opens to place it on an alternative if more than a single monitor is available; albeit the target monitor chosen must be switched on.
The only time the 'problem' is encountered is when using the app on a machine with multiple available (plugged-in) monitors and the monitor previously used is switched off, a possible circumstance but in practice an unlikely one. In such circumstances the user will probably be aware that if your app (or any other) app does not appear when run then the problem is likely to be because one of his monitors is switched off i.e. he will be having the same issue with other apps.
-
Dec 4th, 2021, 03:03 PM
#22
Hyperactive Member
Re: Get Monitor Powerstate
I remember years ago there had been two VB classes by Microsoft pertaining to monitors, I am sure you can find them somewhere. I forgot what were the exact class names; let us assume one is called Monitor.cls and the other MultiMon.cls, then using a ListBox you will get a list of all the monitors in such a manner "\\.\Display1, \\.\Display2 ..." - the disconnected one(s) will not show up.
Code:
Set cMonitor = New clsMonitor
lisMonitors.Clear
For Each cMonitor In cMultiMon.colMonitors
lisMonitors.AddItem cMonitor.name
Next
Please see if you would like to pursue this path.
Last edited by Brenker; Dec 4th, 2021 at 03:16 PM.
-
Dec 4th, 2021, 03:42 PM
#23
Thread Starter
Hyperactive Member
Re: [RESOLVED] Test if second Monitor switched on
Originally Posted by Magic Ink
Could be I am missing something but I wonder if this issue is a serious one.
The typical situation is a laptop which is used at home with the built-in display only, and used at work where an additional monitor is available.
So you are at work using the application on the additional monitor and close it down while it is on that monitor.
Then you go home having disconnected/unplugged the additional monitor. With reference to the code discussed above it is possible to determine that Form placement must now be shifted to the primary and only display; and that done all is well.
Then you go back to work and the app opens on the built-in monitor, if you want it on the additional monitor you just drag it there. You may apply further code as the app opens to place it on an alternative if more than a single monitor is available; albeit the target monitor chosen must be switched on.
The only time the 'problem' is encountered is when using the app on a machine with multiple available (plugged-in) monitors and the monitor previously used is switched off, a possible circumstance but in practice an unlikely one. In such circumstances the user will probably be aware that if your app (or any other) app does not appear when run then the problem is likely to be because one of his monitors is switched off i.e. he will be having the same issue with other apps.
Yes that is correct. I recently came across this issue. we have 2 monitors at work and my colleague had my app on monitor 2
We leave the PC running but switch off both monitors.
At home we only have 1 monitor and connect through remote desktop. so when he ran the app, he could see it in the task bar but not the form.
I had to go into the registry and set the form left position to Zero.
Then when he ran the app again, it appeared on the 1st (primary monitor)
this is a big problem that i dont think there is a solution to.
As i have seen mentioned many times , a monitor is only one way communication.
So there is no status that checks if a monitor is switched on or off.
not sure where to go from here
-
Dec 4th, 2021, 03:56 PM
#24
Thread Starter
Hyperactive Member
Re: Get Monitor Powerstate
Originally Posted by Brenker
I remember years ago there had been two VB classes by Microsoft pertaining to monitors, I am sure you can find them somewhere. I forgot what were the exact class names; let us assume one is called Monitor.cls and the other MultiMon.cls, then using a ListBox you will get a list of all the monitors in such a manner "\\.\Display1, \\.\Display2 ..." - the disconnected one(s) will not show up.
Code:
Set cMonitor = New clsMonitor
lisMonitors.Clear
For Each cMonitor In cMultiMon.colMonitors
lisMonitors.AddItem cMonitor.name
Next
Please see if you would like to pursue this path.
I have tried the VBHelp method. see link
http://vbnet.mvps.org/code/enums/enumdisplaydevices.htm
Attachment 183201
I have run app and wether the 2nd Monitor is On or Off , nothing changes.
If anyone knows where to get the VB classes by Microsoft as mentioned above, would love to give it a go.
-
Dec 4th, 2021, 04:27 PM
#25
Re: Get Monitor Powerstate
Apparently a call to GetDevicePowerState "sometimes" works. Apparently, it depends on how much "feedback" the monitor gives to the Windows driver.
Also, apparently a call to the get_Power method of the IMSVidDevice Interface has possibilities. That IMSVidDevice Interface is in the Quartz.dll. I was just playing around with that for other reasons. It's an ActiveX DLL, so just reference it and start instantiating objects with it (no API declarations needed).
EDIT: Just an FYI ... that Quartz.dll is internally named "ActiveMovie", so that's what you'll see in your VB6 references.
Good Luck. I'll be interested to see what you get going.
And just to say it, I don't think any of the calls to "manipulate" or "enumerate" monitors are going to do this.
Last edited by Elroy; Dec 4th, 2021 at 04:33 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 4th, 2021, 04:35 PM
#26
Re: [RESOLVED] Test if second Monitor switched on
See my comments in your other thread. I haven't tested any of it though.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 4th, 2021, 04:41 PM
#27
Re: [RESOLVED] Test if second Monitor switched on
Seems strange that your RDP client is in someway aware of monitors plugged into the RDP server, I'm lost...
-
Dec 4th, 2021, 04:50 PM
#28
Addicted Member
Re: Get Monitor Powerstate
If it can be done by MultiMonitorTool (Nirsoft), then VB6 should be able to do it.
Note well that MultiMonitorTool.exe can be run from the command line, with various switches to Enable/disable/configure multiple monitors on Windows.
If it cannot be done with VB6, one could always SHELL MultiMonitorTool.exe from VB6, with the appropriate command line switches.
YMMV
Joe
-
Dec 4th, 2021, 04:55 PM
#29
Hyperactive Member
Re: Get Monitor Powerstate
@k_zeon: CodeGuru has the two classes. Their names are actually Monitor.cls and Monitors.cls
https://forums.codeguru.com/showthre...7-Multimonitor
-
Dec 4th, 2021, 06:51 PM
#30
Re: Get Monitor Powerstate
I only have one monitor this weekend so I can't do much testing, but it seems that everyone just keeps circling back to the GetMonitorInfo and GetMonitorInfoEx API calls, which didn't work for me earlier this week.
I did just download and take a look at the NirSoft MultiMonitorTool. I'll admit that it does have a "Disconnected" column, but it doesn't have an On/Off column. I'm guessing that it's also mostly using GetMonitorInfo and GetMonitorInfoEx as well. Joe, I see your screen shot, but I'm guessing that that actually should read "enable/disable" monitor, which really doesn't have anything to do with the monitor's power-state.
It'd be nice if I was wrong, but we'll see.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 4th, 2021, 07:44 PM
#31
Re: Test if second Monitor switched on
I merged the two threads to reduce any possible confusion and cross posting. It seems like an interesting question.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|