Click here to Skip to main content
13,287,856 members (52,029 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 18 Nov 2010

Ensure visibility of your windows when switching computerscreens

, 18 Nov 2010
Rate this:
Please Sign up or sign in to vote.
When going from a multimonitor environment to a single monitor, windows get stuck on the other screens
I find when switching between different resoultions on my computer screen, or switching from a dual monitor setup to a single, the windows of my application can dissapear.

For applications that consists of several "gadget" windows where you save the individual position for each window, its very important to get a hold of the gadgets when they dissapear.

So I wrote a small helper function to recover those windows if they are stuck on another monitor that is not connected

public static void ensureWindowVisibility(Window pWindow)
   var screens = System.Windows.Forms.Screen.AllScreens;
   var currentWorkArea = System.Windows.SystemParameters.WorkArea;
   var windowRect = new Rect(pWindow.Left, pWindow.Top, pWindow.Width, pWindow.Height);
   if (screens.Count() == 1)
      if (!currentWorkArea.IntersectsWith(windowRect))
          pWindow.Left = 100;
          pWindow.Top = 100;

Call this function the first time after you have initialized a window, or whenever you need to ensure the visibility. You could also change the end position which is now set to 100 to something more clever. For example finding out where the window was closest to the primary screen and put in just inside the bounds of the WorkArea.

Works with winforms applications aswell as WPF apps.

Hope this will help someone else too, for me it was absolutely essential for my app.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ola halvorsen
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr22-Nov-10 5:44
memberlinuxjr22-Nov-10 5:44 
GeneralHello O. Schneider, I'm not sure if this is possible with th... Pin
ola halvorsen22-Nov-10 5:42
memberola halvorsen22-Nov-10 5:42 
GeneralThis is a good approch. Can you make it a small XP applicati... Pin
O. Schneider22-Nov-10 5:36
memberO. Schneider22-Nov-10 5:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 18 Nov 2010
Article Copyright 2010 by ola halvorsen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid