private void OnDisplaySettingsChanged(object sender, EventArgs e)
{
System.Drawing.Size screenSize = Screen.FromRectangle(new Rectangle(_parent.Location, _parent.Size)).Bounds.Size;
Debug.WriteLine("DisplaySettingsChanged: " + screenSize.ToString());
if(_stateByResolution.ContainsKey(screenSize))
{
Debug.WriteLine("Back in familiar territory.");
_thisState = (WindowSizeLocation)_stateByResolution[screenSize];
}
else
{
Debug.WriteLine("This is a new frontier.");
WindowSizeLocation newState = new WindowSizeLocation(_parent.Size, _parent.Location, screenSize);
if(_parent.WindowState != FormWindowState.Normal)
{
newState.Location = _thisState.Location;
newState.Size = _thisState.Size;
}
_thisState = newState;
_stateByResolution[_thisState.ScreenSize] = _thisState;
}
//If the form is offscreen, move it back on.
Rectangle windowRect = new Rectangle(_thisState.Location, _thisState.Size);
Rectangle workingArea = Screen.FromRectangle(windowRect).WorkingArea;
if(!workingArea.IntersectsWith(windowRect))
{
Point newLoc = new Point(_thisState.Location.X, _thisState.Location.Y);
if(_thisState.Location.X < workingArea.X)
newLoc.X = workingArea.X;
else if(_thisState.Location.X > workingArea.Right)
newLoc.X = workingArea.Right - _thisState.Size.Width;
if(_thisState.Location.Y < workingArea.Y)
newLoc.Y = workingArea.Y;
else if(_thisState.Location.Y > workingArea.Bottom)
newLoc.Y = workingArea.Bottom - _thisState.Size.Height;
_thisState.Location = newLoc;
}
SetWindowState();
}