1

I have found a lot of answers researching this, but non address my question. Assume that user has 2 monitors, say a laptop screen with 1600×1200 res, and external monitor of 2560×1440 res. Regardless how the 2 displays are set up, if a client moves the main-form of the program to the external monitor, I would like it to report that its on 2560×1440 res. When the main-form is moved on to the laptop it should report the 1600x1200.

Is this possible? I know how to report the res, I just do not know how to identify which monitor the main-form is sitting on.

user1500403
  • 551
  • 8
  • 32
  • [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103). Read the notes about the DpiAwareness of the application. – Jimi Jul 08 '20 at 01:53

1 Answers1

2

You need to first identify what you mean by the form being on a screen, because it's possible for a single form to span multiple screens. You're going to be using the Screen class regardless, but the calculations will be different. The simplest option would be to use the Location property, e.g.

For Each scrn In Screen.AllScreens
    If scrn.Bounds.Contains(Location) Then
        MessageBox.Show($"Resolution: {scrn.Bounds.Width} x {scrn.Bounds.Height}")
        Exit For
    End If
Next

Another option would be to use the screen that contains the largest proportion of the form, e.g.

Dim maxArea = 0
Dim resolution = Size.Empty

For Each scrn In Screen.AllScreens
    Dim intersection = Rectangle.Intersect(scrn.Bounds, Bounds)
    Dim area = intersection.Width * intersection.Height

    If area > maxArea Then
        maxArea = area
        resolution = scrn.Bounds.Size
    End If
Next

MessageBox.Show($"Resolution: {resolution.Width} x {resolution.Height}")

Note that this code will display the resolution of the first encountered if the equal parts of the form are on multiple screens.

There may be other options too, although these seem the most likely.

EDIT:

It's also worth noting that the first code won't work for a maximised form because the actual Location value will be outside the bounds of the screen it's on, so you'd need a bit of jiggery-pokery to handle that. The second code will handle that without issue.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46