Centering Form on Given Monitor-VBForums
Results 1 to 4 of 4

Thread: Centering Form on Given Monitor

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2014
    Posts
    42

    Centering Form on Given Monitor

    Hi,

    Does anyone have any thoughts as to how I could go about centering a form on a monitor which may not necessarily be the Primary Monitor?

    For example, suppose I have frmMain open on my secondary monitor. If I open another form from frmMain I want to be able to center it on the monitor that frmMain is on.

    Further, if I move the form in question to another monitor and a call is made to re-center it I want to center it on the monitor it's currently on.

    Thanks in advance for any assistance!

    Best Regards
    Brad

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

    Re: Centering Form on Given Monitor

    Codebank.

    I haven't looked at those codebank routines in a while. If they don't do what you want, let me know, and I've got lots of multi-monitor procedures.

    Good Luck,
    Elroy
    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2014
    Posts
    42

    Re: Centering Form on Given Monitor

    Hi Elroy,

    Thanks for the link and the contribution to the community, greatly appreciated! A couple questions:

    1) With the CenterWindowOnMonitor sub I'm required to supply the monitor number. Some of our end users may have one monitor, some have four, others have two. I'd like to be able to figure out what monitor the form in question is on and use that as the monitor number. How would I go about getting that value in that context?

    2) I hesitate to ask, but...any chance you've used these functions in a RemoteApp context before??

    Thanks!

    Best Regards
    Brad

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

    Re: Centering Form on Given Monitor

    Hi Brad,

    Certainly no problem asking anything you like. I may not have the answer though. And, to answer #2 first, no, I've never used them in any kind of remote way other than some of the desktop-share utilities where you make a presentation and others watch. My primary program has been used that way, but I believe everything was just put on the primary ("Make this my main display") monitor. So, I'm not sure what you're getting at.

    Now, regarding monitor numbers and handles (your question #1), they have both. The numbers are just ordinal numbers starting with ONE. The handles can be any Long. The API calls really prefer the handles (and not the ordinal numbers). I'm not sure what your exact question is, but you've got a MonitorCount function, as well as the following:

    MonitorHandle - converts a number to a handle.
    MonitorNum - converts a handle to a number.
    PrimaryMonitorNum - the number of the primary monitor.
    PrimaryMonitorHandle - the handle of the primary monitor.

    ...and there's also...

    WindowIsOnMonitor - which tells you whether or not a window is on a particular monitor. It'd be easy to build a loop to determine the precise monitor (using the handle) that any window was on.

    It would seem that some combination of those would allow you to work out the answer to your question #1. However, if not, please explain in more detail and maybe I can assist with finding an acceptable answer.

    Best Regards,
    Elroy

    EDIT1: Here's a bit of test code for a Form1 (assuming you have my monitor module in a BAS module in the same project):

    Code:
    
    Option Explicit
    
    Private Sub Form_Click()
        Dim i As Long
        '
        For i = 1 To MonitorCount
            If WindowIsOnMonitor(MonitorHandle(i), Me.hWnd) Then
                MsgBox "I'm on monitor #" & Format$(i) & " with handle " & Format$(MonitorHandle(i))
                Exit For
            End If
        Next i
    End Sub
    
    
    
    I'm currently on a three-monitor system, and it seemed to work flawlessly. I could have centered it easily if I wanted.
    Last edited by Elroy; Feb 13th, 2018 at 07:12 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width