Results 1 to 10 of 10

Thread: Form opens in removed monitor!!?

  1. #1

    Thread Starter
    Member JohnPotier's Avatar
    Join Date
    Sep 2007
    Location
    Norway
    Posts
    32

    Question 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.

  2. #2
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    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.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    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. To all, peace and happiness.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    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. To all, peace and happiness.

  5. #5

    Thread Starter
    Member JohnPotier's Avatar
    Join Date
    Sep 2007
    Location
    Norway
    Posts
    32

    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.

  6. #6
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,250

    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.
    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.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: Form opens in removed monitor!!?

    Quote Originally Posted by JohnPotier View Post
    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. To all, peace and happiness.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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.

  9. #9
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    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.

  10. #10
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    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
  •  



Click Here to Expand Forum to Full Width