-
May 31st, 2023, 12:22 PM
#1
Thread Starter
Junior Member
Form opens in removed monitor!!?
Q: How do I detect if a connected (?) monitor is still there when my application starts?
Background:
I'm so clever and writes the position and size of a window when application ends. The problem is imminent next time the application starts - if the monitor is not connected anymore, the form opens up off screen.
How can I detect if a coordinate (left and top) is still valid when the application starts? How can I detect that a coordinate is on a visible screen area?
Current workaround:
Startup form has KeyPreview=True and if Ctrl+Alt+x is pressed, the form is moved to 0,0 - which is in the upper left corner of the main window.
-
May 31st, 2023, 12:44 PM
#2
Re: Form opens in removed monitor!!?
well do a comparison.
hMonitor = MonitorFromWindow(Main.hwnd, 1&)
GetMonitorInfo hMonitor, tagMONITORINFO
should give u everything u need to know the monitor where the APP is about to show.
compare and see if its OK, otherwise RESET.
-
May 31st, 2023, 12:45 PM
#3
Re: Form opens in removed monitor!!?
I just set this up for a Form1 test:
Code:
Option Explicit
Private 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
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) 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
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 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
Private Sub Form_Load()
If Not FormIsFullyOnMonitor(Me) Then
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
End If
End Sub
That should get you going.
I actually have a more sophisticated way of centering a form, but the above is a quick-and-dirty way to get it done (using the Screen object). If/since you're saving your form's last location, the above code will keep you out of trouble.
I have similar code in ALL of my forms that are for production/distribution.
Last edited by Elroy; May 31st, 2023 at 12:58 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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.
-
May 31st, 2023, 01:00 PM
#4
Re: Form opens in removed monitor!!?
Also, if this is all running on a network, be sure to save your monitor locations locally (individually for each computer using the application). I typically use the registry for this. SaveSetting/GetSetting works perfectly, and they stay local to the machine.
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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.
-
May 31st, 2023, 01:03 PM
#5
Thread Starter
Junior Member
Re: Form opens in removed monitor!!?
Thanks baka & Elroy. Elroy's code works in initial test :- ) Let me bake it into some of my other projects and test.
-
May 31st, 2023, 01:59 PM
#6
Re: Form opens in removed monitor!!?
You know, some major applications continue to operate and open on the removed monitor - they have no such code in them that allows them to run on the currently showing screen, firefox for example. Maybe it is a pointer to the new direction... GUI-less applications.
PS. At this point in the conversation, Niya would normally appear.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
May 31st, 2023, 02:41 PM
#7
Re: Form opens in removed monitor!!?
 Originally Posted by JohnPotier
Thanks baka & Elroy. Elroy's code works in initial test :- ) Let me bake it into some of my other projects and test.
John, hopefully you know that you can pull those procedure out of that Form1, and put them in some general purpose BAS module somewhere. That's why I left them as "Public" procedures. And the API declarations would have to be moved too.
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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.
-
May 31st, 2023, 02:58 PM
#8
Re: Form opens in removed monitor!!?
This is why we have GetWindowPlacement function and SetWindowPlacement function.
If the information specified in WINDOWPLACEMENT would result in a window that is completely off the screen, the system will automatically adjust the coordinates so that the window is visible, taking into account changes in screen resolution and multiple monitor configuration.
Using anything else is a mistake.
-
May 31st, 2023, 03:11 PM
#9
Re: Form opens in removed monitor!!?
of all API I have tried so far the best one is the 2 I posted.
can handle everything u need. both to check the monitor your app runs from and all needed data to center it, maximize, fullscreen as it also read the taskbar.
I have also "restore" specific functions but I only store the size and if maxi or full, not left/top position, that I dont care. works everytime.
-
May 31st, 2023, 04:19 PM
#10
Re: Form opens in removed monitor!!?
I have developed a VB plug-in for multi-display management before, CTRL+1, CTRL+2 can switch to different displays
Before I developed a multi-display management VB plug-in,CTRL+1,CTRL+2 can switch to different monitors, and then the mouse can only move within the scope of the monitor.
Tags for this Thread
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
|