realbasic-nug
[Top] [All Lists]

Re: Keeping Windows on Screen Class

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Keeping Windows on Screen Class
From: Joe Strout <joe at inspiringapps dot com>
Date: Fri, 29 Feb 2008 08:06:32 -0700
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <mailman dot 7335 dot 1204056361 dot 9216 dot realbasic-nug at lists dot realsoftware dot com> <p06240800c3ecf4f9629e at [192 dot 168 dot 0 dot 111]> <CA6BA25C-5C57-4B11-9BC1-162A734BCF99 at teachesme dot com>
On Feb 28, 2008, at 9:44 PM, Samuel DeVore wrote:

> On Feb 28, 2008, at 4:41 PM, Stephen Dodd wrote:
>
>> Anyone have/know of a good cross-platform class to keep a window on
>> screen that works well across multiple monitors?
>
> Joe was tossing around some sample code for something like this about
> a year ago I think.  Joe you remember it?

Well, it wasn't exactly this; it was for resizing the window to fit  
whatever monitor it's on.

You need to first determine what monitor your window is 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 < 0 then continue for
     Dim area As Integer = w * h
     if area > bestArea then
       bestScreen = i
       bestArea = area
     end if
   next
   if bestScreen < 0 then return 0  // if not on any screen, report  
the main monitor
   return bestScreen
End Function

Then, shrink the window to fit:

Protected Sub FitToScreen()
   // Fit this window onto its screen, by shortening it as needed.
   Dim scrn As Integer = ScreenNumber
   Dim sBottom As Integer = Screen( scrn ).AvailableTop + Screen 
( scrn ).AvailableHeight
   Dim sRight As Integer = Screen( scrn ).AvailableLeft + Screen 
( scrn ).AvailableWidth

   if Top + Height > sBottom then
     Height = Max( MinHeight, sBottom - Top )
   end if

   if Left + Width > sRight then
     Width = Max( MinWidth, sRight - Left )
   end if
End Sub

HTH,
- Joe


--
Joe Strout
Inspiring Applications, Inc.
http://www.InspiringApps.com



_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>