From 1a6197d892951b707aee973fd2be77d73331989c Mon Sep 17 00:00:00 2001 From: Lorenz Cuno Klopfenstein Date: Tue, 27 Nov 2012 03:33:09 +0100 Subject: [PATCH] Fix for issue #33: window position/size now restores correctly even when chrome is disabled on closing. --- OnTopReplica/AspectRatioForm.cs | 5 ++-- OnTopReplica/Program.cs | 5 ++++ OnTopReplica/StartupOptions/Factory.cs | 6 +++-- OnTopReplica/StartupOptions/Options.cs | 33 ++++++++++++++++---------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/OnTopReplica/AspectRatioForm.cs b/OnTopReplica/AspectRatioForm.cs index f3ba895..9701e3c 100644 --- a/OnTopReplica/AspectRatioForm.cs +++ b/OnTopReplica/AspectRatioForm.cs @@ -96,10 +96,11 @@ namespace OnTopReplica { ClientSize = new Size(newWidth, newHeight); //Move form vertically to adapt to new size - if (Location.Y + Size.Height > workingArea.Y + workingArea.Height) { + //REMOVED: allows the window to correctly be restored slightly off screen + /*if (Location.Y + Size.Height > workingArea.Y + workingArea.Height) { int offsetY = (workingArea.Y + workingArea.Height) - (Location.Y + Size.Height); Location = new Point(Location.X, Location.Y - offsetY); - } + }*/ } /// diff --git a/OnTopReplica/Program.cs b/OnTopReplica/Program.cs index 4d0cd98..12971ee 100644 --- a/OnTopReplica/Program.cs +++ b/OnTopReplica/Program.cs @@ -60,7 +60,12 @@ namespace OnTopReplica { //Enter GUI loop Application.Run(_mainForm); + //HACK: re-enable chrome to fix position persistence (ideally, chrome status should be stored and restored - but this is not always possible) + if (!_mainForm.IsChromeVisible) + _mainForm.IsChromeVisible = true; + //Persist settings + System.Diagnostics.Trace.WriteLine(string.Format("Persisting {0} size {1} to settings.", _mainForm.Location, _mainForm.ClientSize)); Settings.Default.RestoreLastPosition = _mainForm.Location; Settings.Default.RestoreLastSize = _mainForm.ClientSize; Settings.Default.Save(); diff --git a/OnTopReplica/StartupOptions/Factory.cs b/OnTopReplica/StartupOptions/Factory.cs index 9bc3ec5..ee44ca2 100644 --- a/OnTopReplica/StartupOptions/Factory.cs +++ b/OnTopReplica/StartupOptions/Factory.cs @@ -33,6 +33,8 @@ namespace OnTopReplica.StartupOptions { if (Settings.Default.RestoreSizeAndPosition) { options.StartLocation = Settings.Default.RestoreLastPosition; options.StartSize = Settings.Default.RestoreLastSize; + + System.Diagnostics.Trace.WriteLine(string.Format("Restoring window at {0} size {1}.", Settings.Default.RestoreLastPosition, Settings.Default.RestoreLastSize)); } if (Settings.Default.RestoreLastWindow) { @@ -74,11 +76,11 @@ namespace OnTopReplica.StartupOptions { }) .Add("position=", "Target {X,Y} of the OnTopReplica window.", s => { options.StartLocation = new Point(s.Width, s.Height); - options.StartScreenPosition = null; + options.StartPositionLock = null; }) .Add("screenPosition=", "Resolution independent window position on current screen, with locking. Values: {TR|TL|C|BR|BL}.", pos => { options.StartLocation = null; - options.StartScreenPosition = pos; + options.StartPositionLock = pos; }) .Add("r|region=", "Region {X,Y,W,H} of the cloned window.", region => { options.Region = new ThumbnailRegion(region); diff --git a/OnTopReplica/StartupOptions/Options.cs b/OnTopReplica/StartupOptions/Options.cs index d741f21..d36e2bb 100644 --- a/OnTopReplica/StartupOptions/Options.cs +++ b/OnTopReplica/StartupOptions/Options.cs @@ -25,7 +25,7 @@ namespace OnTopReplica.StartupOptions { public Point? StartLocation { get; set; } - public ScreenPosition? StartScreenPosition { get; set; } + public ScreenPosition? StartPositionLock { get; set; } public Size? StartSize { get; set; } @@ -96,7 +96,7 @@ namespace OnTopReplica.StartupOptions { form.IsChromeVisible = !DisableChrome; form.Opacity = (double)Opacity / 255.0; - //Thumbnail cloning + //Seek handle for thumbnail cloning WindowHandle handle = null; if (WindowId.HasValue) { handle = WindowHandle.FromHandle(WindowId.Value); @@ -119,23 +119,30 @@ namespace OnTopReplica.StartupOptions { handle = seeker.Windows.FirstOrDefault(); } - - //Set any found handle - if (handle != null) { - form.SetThumbnail(handle, Region); + + //Position lock + if (StartPositionLock.HasValue) { + form.PositionLock = StartPositionLock.Value; } - //Size - if (StartSize.HasValue) { + //Size and location start values + if (StartLocation.HasValue && StartSize.HasValue) { + form.StartPosition = System.Windows.Forms.FormStartPosition.Manual; + form.Location = StartLocation.Value; form.ClientSize = StartSize.Value; } - - //Position - if (StartLocation.HasValue) { + else if (StartLocation.HasValue) { + form.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds; form.Location = StartLocation.Value; } - else if (StartScreenPosition.HasValue) { - form.PositionLock = StartScreenPosition.Value; + else if (StartSize.HasValue) { + form.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation; + form.ClientSize = StartSize.Value; + } + + //Clone any found handle + if (handle != null) { + form.SetThumbnail(handle, Region); } //Other features