shrink a window down (while honoring MinWidth and minHeight) so that it fits on
whatever monitor it’s on.
First we have a function to determine which monitor the window’s on:
Protected Function ScreenNumber() As Integer
// Return the number of the screen (from 0 to ScreenCount-1)
// which this window is mostly on.
Dim bestScreen As Integer = -1
Dim bestArea As Integer
for i As Integer = 0 to ScreenCount-1
Dim w As Integer = _
Min( Screen(i).Left + Screen(i).Width, Left + Width ) _
– Max( Screen(i).Left, Left )
Dim h As Integer = _
Max( Screen(i).Top + Screen(i).Height, Top + Height ) _
– Max( Screen(i).Top, Top )
if w < 0 or h bestArea then
bestScreen = i
bestArea = area
end if
next
if bestScreen sBottom then
Height = Max( MinHeight, sBottom – Top )
end if
if Left + Width > sRight then
Width = Max( MinWidth, sRight – Left )
end if
End Sub
You could throw both of these methods into a Window, add a button that
calls FitToScreen, and then drag your window around to various funky
positions on both monitors and make sure it does what it should.

You must be logged in to post a comment.