Click here to Skip to main content
15,506,599 members
Articles / Desktop Programming / Windows Forms
Tip/Trick
Posted 18 Nov 2010

Tagged as

Stats

17.6K views
4 bookmarked

Ensure visibility of your windows when switching computerscreens

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
18 Nov 2010CPOL
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

C#
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.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr22-Nov-10 5:44
professionallinuxjr22-Nov-10 5:44 
GeneralHello O. Schneider, I'm not sure if this is possible with th... Pin
ola halvorsen22-Nov-10 5:42
ola halvorsen22-Nov-10 5:42 
Hello O. Schneider,
I'm not sure if this is possible with this approach. I'm setting the Position of windows in my own app, To get control of all other apps running would reach outside the scope of the app which I'm not sure if is possible. But if you or other find a solution to this, please write it here.
GeneralThis is a good approch. Can you make it a small XP applicati... Pin
O. Schneider22-Nov-10 5:36
O. 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.