Improved resizing.

This commit is contained in:
Lorenz Cuno Klopfenstein 2009-11-04 18:00:26 +00:00
parent 958d3891d7
commit 019541c3bc
4 changed files with 159 additions and 43 deletions

View file

@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using VistaControls.Dwm.Helpers;
using System.Drawing;
namespace OnTopReplica {
/// <summary>
/// Form that automatically keeps a certain aspect ratio and resizes withour flickering.
/// </summary>
public class AspectRatioForm : GlassForm {
public AspectRatioForm() {
AspectRatio = 1.0;
}
bool _maintainAspectRatio = true;
public bool MaintainAspectRatio {
get {
return _maintainAspectRatio;
}
set {
_maintainAspectRatio = value;
if (value)
RefreshAspectRatio();
}
}
double _aspectRatio = 1.0;
public double AspectRatio {
get {
return _aspectRatio;
}
set {
if (value <= 0.0 || Double.IsInfinity(value))
return;
_aspectRatio = value;
}
}
/// <summary>
/// Forces the form to update its height based on the current aspect ratio setting.
/// </summary>
public void RefreshAspectRatio() {
this.Height = (int)(this.Width / AspectRatio);
}
/// <summary>
/// Updates the aspect ratio of the form and refreshes it.
/// </summary>
public void SetAspectRatio(Size aspectRatioSource) {
AspectRatio = (aspectRatioSource.Width / (double)aspectRatioSource.Height);
RefreshAspectRatio();
}
/// <summary>
/// Override WM_SIZING message.
/// Taken from: http://www.vcskicks.com/maintain-aspect-ratio.php
/// </summary>
protected override void WndProc(ref Message m) {
if (MaintainAspectRatio && m.Msg == NativeMethods.WM_SIZING) {
var rc = (NativeMethods.Rectangle)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.Rectangle));
int res = m.WParam.ToInt32();
if (res == NativeMethods.WMSZ_LEFT || res == NativeMethods.WMSZ_RIGHT) {
//Left or right resize -> adjust height (bottom)
int targetHeight = (int)(this.Width / AspectRatio);
int originalHeight = rc.Bottom - rc.Top;
int diffHeight = originalHeight - targetHeight;
rc.Top += (diffHeight / 2);
rc.Bottom = rc.Top + targetHeight;
}
else if (res == NativeMethods.WMSZ_TOP || res == NativeMethods.WMSZ_BOTTOM) {
//Up or down resize -> adjust width (right)
int targetWidth = (int)(this.Height * AspectRatio);
int originalWidth = rc.Right - rc.Left;
int diffWidth = originalWidth - targetWidth;
rc.Left += (diffWidth / 2);
rc.Right = rc.Left + targetWidth;
}
else if (res == NativeMethods.WMSZ_RIGHT + NativeMethods.WMSZ_BOTTOM) {
//Lower-right corner resize -> adjust height (could have been width)
rc.Bottom = rc.Top + (int)(this.Width / AspectRatio);
}
else if (res == NativeMethods.WMSZ_LEFT + NativeMethods.WMSZ_BOTTOM) {
//Lower-left corner resize -> adjust height (could have been width)
rc.Bottom = rc.Top + (int)(this.Width / AspectRatio);
}
else if (res == NativeMethods.WMSZ_LEFT + NativeMethods.WMSZ_TOP) {
//Upper-left corner -> adjust width (could have been height)
rc.Left = rc.Right - (int)(this.Height * AspectRatio);
}
else if (res == NativeMethods.WMSZ_RIGHT + NativeMethods.WMSZ_TOP) {
//Upper-right corner -> adjust width (could have been height)
rc.Right = rc.Left + (int)(this.Height * AspectRatio);
}
Marshal.StructureToPtr(rc, m.LParam, true);
}
base.WndProc(ref m);
}
}
}

View file

@ -8,7 +8,7 @@ using VistaControls.TaskDialog;
namespace OnTopReplica
{
public partial class MainForm : VistaControls.Dwm.Helpers.GlassForm
public partial class MainForm : AspectRatioForm
{
//Visualization status
byte _lastOpacity = 255;
@ -39,9 +39,6 @@ namespace OnTopReplica
}
public MainForm() {
//Wheel handler
//this.MouseWheel += new MouseEventHandler(Thumbnail_MouseWheel);
InitializeComponent();
//Thumbnail panel
@ -75,7 +72,9 @@ namespace OnTopReplica
});
}
void FullscreenForm_CloseRequest(object sender, CloseRequestEventArgs e) {
#region Child forms & controls events
void FullscreenForm_CloseRequest(object sender, CloseRequestEventArgs e) {
if (_isFullscreen) {
//Update handle to match the one selected in fullscreen mode
if (_lastWindowHandle != e.CurrentWindowHandle) {
@ -87,30 +86,6 @@ namespace OnTopReplica
}
}
void Thumbnail_MouseWheel(object sender, MouseEventArgs e) {
/*int delta = (int)((double)e.Delta / (double)SystemInformation.MouseWheelScrollDelta) * SystemInformation.MouseWheelScrollLines;
byte nuValue = (byte)Math.Max(0, Math.Min(255, _lastOpacity + delta));
_lastOpacity = nuValue;
this.Opacity = (double)_lastOpacity / 255.0;*/
/*
* ALTERNATIVE "Zoom" MouseWheel
*
Rectangle original;
if (_thumbnailPanel.ShowRegion)
original = _thumbnailPanel.ShownRegion;
else
original = new Rectangle(Point.Empty, _thumbnailPanel.ThumbnailOriginalSize);
Rectangle nuRegion = new Rectangle(original.Left + delta, original.Top + delta, original.Width - (delta * 2), original.Height - (delta * 2));
_thumbnailPanel.ShownRegion = nuRegion;
*/
}
void RegionBox_RegionChanged(object sender, Rectangle region) {
_thumbnailPanel.ShownRegion = region;
}
@ -135,11 +110,13 @@ namespace OnTopReplica
void Thumbnail_IdealSizeChange(object sender, Size e) {
ClientSize = e;
}
}
#region Side Panels
#endregion
const int cWindowBoundary = 10;
#region Side "Region box" events
const int cWindowBoundary = 10;
bool _regionBoxShowing = false;
bool _regionBoxDidMoveForm = false;
@ -163,7 +140,10 @@ namespace OnTopReplica
//Enable region drawing on thumbnail
_thumbnailPanel.DrawMouseRegions = value;
//Resize and move
//Disable aspect ratio keeping
MaintainAspectRatio = !value;
//Resize and move to fit region panel
ClientSize = new Size {
Width = ClientSize.Width + ((value) ? _regionBox.Width : -_regionBox.Width),
Height = Math.Max(ClientSize.Height, _regionBox.Height)
@ -202,8 +182,8 @@ namespace OnTopReplica
}
//Resize automatically on region box closing
if (Settings.Default.AutoFitOnResize)
FitToThumbnail();
/*if (Settings.Default.AutoFitOnResize)
FitToThumbnail();*/
}
}
}
@ -233,8 +213,18 @@ namespace OnTopReplica
base.OnClosing(e);
}
protected override void OnResize(EventArgs e) {
protected override void OnResizeBegin(EventArgs e) {
base.OnResizeBegin(e);
//Update aspect ratio if needed
if (_thumbnailPanel.IsShowingThumbnail) {
SetAspectRatio(_thumbnailPanel.ThumbnailOriginalSize);
}
}
/*protected override void OnResize(EventArgs e) {
if (RegionBoxShowing) {
//Adapt glass margins on region box
this.GlassMargins = new Margins(ClientSize.Width - _regionBox.Width, 0, 0, 0);
}
@ -252,16 +242,16 @@ namespace OnTopReplica
this.GlassMargins = new Margins(-1);
}*/
/*
base.OnResize(e);
}
}*/
protected override void OnResizeEnd(EventArgs e) {
/*protected override void OnResizeEnd(EventArgs e) {
base.OnResizeEnd(e);
if (Settings.Default.AutoFitOnResize && !RegionBoxShowing)
FitToThumbnail();
}
}*/
protected override void OnShown(EventArgs e) {
base.OnShown(e);
@ -740,9 +730,10 @@ namespace OnTopReplica
ThumbnailError(ex, false, Strings.ErrorUnableToCreateThumbnail);
}
if(Settings.Default.AutoFitOnResize)
FitToThumbnail();
//Set aspect ratio (this will resize the form)
SetAspectRatio(_thumbnailPanel.ThumbnailOriginalSize);
//Reset regions
_regionBox.Reset();
//GUI
@ -765,6 +756,7 @@ namespace OnTopReplica
ThumbnailUnset();
}
/// <summary>Automatically sizes the window in order to accomodate the thumbnail p times.</summary>
/// <param name="p">Scale of the thumbnail to consider.</param>
private void FitToThumbnail(double p) {
@ -780,6 +772,7 @@ namespace OnTopReplica
}
}
/*
/// <summary>Automatically fits the window to the current thumbnail.</summary>
/// <remarks>Only adjusts height and corrects the window's position.</remarks>
private void FitToThumbnail() {
@ -795,6 +788,7 @@ namespace OnTopReplica
Location = new Point(Location.X, Location.Y + diffHeight / 2);
}
}
* */
#endregion

View file

@ -244,6 +244,12 @@ namespace OnTopReplica
public static extern IntPtr SendMessageTimeout(IntPtr hwnd, uint message, IntPtr wparam, IntPtr lparam, SendMessageTimeoutFlags flags, uint timeout, out IntPtr result);
public const uint WM_GETICON = 0x7f;
public const int WM_SIZING = 0x214;
public const int WMSZ_LEFT = 1;
public const int WMSZ_RIGHT = 2;
public const int WMSZ_TOP = 3;
public const int WMSZ_BOTTOM = 6;
[Flags]
public enum SendMessageTimeoutFlags : uint {

View file

@ -98,6 +98,9 @@
<Compile Include="AboutForm.Designer.cs">
<DependentUpon>AboutForm.cs</DependentUpon>
</Compile>
<Compile Include="AspectRatioForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ClickThroughLabel.cs">
<SubType>Component</SubType>
</Compile>
@ -255,7 +258,6 @@
<None Include="Assets\flag_ita.png" />
<Content Include="Assets\icon.ico" />
<None Include="OnTopReplica_TemporaryKey.pfx" />
<None Include="Resources\25.png" />
<None Include="Assets\x-oblique.png" />
<None Include="Assets\xiao_up.png" />
<None Include="Assets\xiao_down.png" />