Introduction

This brief recipe shows how to ensure your window is completely on-screen, with multiple monitor support.

You could either make your frame a subclass of this utility class, or copy the EnsureOnScreen method into your own Frame subclass.

Code Sample

import wx
class MyFrame(wx.Frame):
   """
      Provides a utility method that keeps the window on-screen
   """
   def EnsureOnScreen(self):
      """
         Move the window onscreen, if it's not already.
      """
      try: myDisplayIdx = wx.Display.GetFromWindow(self)
      except NotImplementedError: myDisplayIdx = 0
      if myDisplayIdx == -1: myDisplayIdx = 0
      display = wx.Display(myDisplayIdx)
      geometry = display.GetGeometry()
      myPos = self.GetPosition()
      if myPos.x < geometry.x:
         myPos.x = geometry.x
      if myPos.y < geometry.y:
         myPos.y = geometry.y
      mySize = self.GetSize()
      if mySize.width > geometry.width:
         mySize.width = geometry.width
         myPos.x = geometry.x
      elif myPos.x + mySize.width > geometry.x + geometry.width:
         myPos.x = geometry.x + geometry.width - mySize.width
         
      if mySize.height > geometry.height:
         mySize.height = geometry.height
         myPos.y = geometry.y
      elif myPos.y + mySize.height > geometry.y + geometry.height:
         myPos.y = geometry.y + geometry.height - mySize.height
      
      self.SetPosition(myPos)
      self.SetSize(mySize)

EnsureFrameIsOnScreen (last edited 2008-03-11 10:50:30 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.