views:

70

answers:

1

Hi, i have a problem in which i am hoping to find the best and easiest to implement solution.

I have written a WPF application which can be dragged around the desktop but i usually put it in the top right hand corner of the screen.

My problem is that when i remote dial in from another location - where the resolution is always lower - the application is offscreen and there is no way for me to get it back unless i kill the process and relaunch it.

I have thought of some ideas in order to always keep it visible.

1] Whenever it is running have it spawn a child thread that checks the visible resolution in a loop. When it is outside the detected bounds it will update its location.

2] Use some sort of messaging system to send it a message when i remote in - and that will trigger it to re-align itself.

Does anyone have any suggestions??

+2  A: 

You could have a DispatcherTimer that fires periodically (every few seconds) to ensure that the window is on one of the visible screens. If not, it would reposition so that it is.

You can use the System.Windows.Forms.Screen class to access information about the user's desktop and screen setup, even though you're using WPF.

Something like:

bool isWithin = false;
foreach (Screen screen in Screen.AllScreens)
{
    if (screen.Bounds.Contains(windowLocation))
        isWithin = true;
}

// if !isWithin, move to 0,0
Drew Noakes
thanks drew - how would a dispatchertimer differ from a new thread?
Grant
With the extra requirement of needing to align top right you'd need to access the screen bounds like this. +1
TreeUK
You're welcome. A DispatcherTimer will fire on the UI thread. This means you can modify UI properties without having cross thread exceptions. Avoiding new threads is often a good idea as they take up extra resources, and you have to remember to clean them up nicely when closing your app.
Drew Noakes
Look at what kind of weird stuff you have to do when you violate MS design guidelines. Hurf.
Will
what guidelines are you referring to..
Grant