mirror of
https://github.com/LorenzCK/OnTopReplica.git
synced 2024-05-10 15:32:41 +12:00
Issue #58: when resizing (WM_SIZING) and on wheel scroll the aspect ratio of the source window is refreshed.
AspectRatioForm now stores initial aspect ratio on window handle creation (until new aspect ratio is set).
This commit is contained in:
parent
56e47fb7e5
commit
1c253bdcb1
|
@ -71,6 +71,13 @@ namespace OnTopReplica {
|
|||
}
|
||||
}
|
||||
|
||||
protected override void OnHandleCreated(EventArgs e) {
|
||||
base.OnHandleCreated(e);
|
||||
|
||||
//When created, use first size of the window as aspect ratio
|
||||
SetAspectRatio(ClientSize, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces the form to update its height based on the current aspect ratio setting.
|
||||
/// </summary>
|
||||
|
@ -155,47 +162,55 @@ namespace OnTopReplica {
|
|||
}
|
||||
}
|
||||
|
||||
protected virtual void OnResizing(EventArgs e) {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override WM_SIZING message to restrict resizing.
|
||||
/// Taken from: http://www.vcskicks.com/maintain-aspect-ratio.php
|
||||
/// Improved with code from: http://stoyanoff.info/blog/2010/06/27/resizing-forms-while-keeping-aspect-ratio/
|
||||
/// </summary>
|
||||
protected override void WndProc(ref Message m) {
|
||||
if (KeepAspectRatio && m.Msg == WM.SIZING) {
|
||||
var clientSizeConversion = ClientWindowDifference;
|
||||
if (m.Msg == WM.SIZING) {
|
||||
this.OnResizing(EventArgs.Empty);
|
||||
|
||||
var rc = (Native.NRectangle)Marshal.PtrToStructure(m.LParam, typeof(Native.NRectangle));
|
||||
int res = m.WParam.ToInt32();
|
||||
if (KeepAspectRatio) {
|
||||
var clientSizeConversion = ClientWindowDifference;
|
||||
|
||||
int width = (rc.Right - rc.Left) - clientSizeConversion.Width - ExtraPadding.Horizontal;
|
||||
int height = (rc.Bottom - rc.Top) - clientSizeConversion.Height - ExtraPadding.Vertical;
|
||||
var rc = (Native.NRectangle)Marshal.PtrToStructure(m.LParam, typeof(Native.NRectangle));
|
||||
int res = m.WParam.ToInt32();
|
||||
|
||||
if (res == WMSZ.LEFT || res == WMSZ.RIGHT) {
|
||||
//Left or right resize, adjust top and bottom
|
||||
int targetHeight = (int)(width / AspectRatio);
|
||||
int diffHeight = height - targetHeight;
|
||||
int width = (rc.Right - rc.Left) - clientSizeConversion.Width - ExtraPadding.Horizontal;
|
||||
int height = (rc.Bottom - rc.Top) - clientSizeConversion.Height - ExtraPadding.Vertical;
|
||||
|
||||
rc.Top += (int)(diffHeight / 2.0);
|
||||
rc.Bottom = rc.Top + targetHeight + ExtraPadding.Vertical + clientSizeConversion.Height;
|
||||
if (res == WMSZ.LEFT || res == WMSZ.RIGHT) {
|
||||
//Left or right resize, adjust top and bottom
|
||||
int targetHeight = (int)(width / AspectRatio);
|
||||
int diffHeight = height - targetHeight;
|
||||
|
||||
rc.Top += (int)(diffHeight / 2.0);
|
||||
rc.Bottom = rc.Top + targetHeight + ExtraPadding.Vertical + clientSizeConversion.Height;
|
||||
}
|
||||
else if (res == WMSZ.TOP || res == WMSZ.BOTTOM) {
|
||||
//Up or down resize, adjust left and right
|
||||
int targetWidth = (int)(height * AspectRatio);
|
||||
int diffWidth = width - targetWidth;
|
||||
|
||||
rc.Left += (int)(diffWidth / 2.0);
|
||||
rc.Right = rc.Left + targetWidth + ExtraPadding.Horizontal + clientSizeConversion.Width;
|
||||
}
|
||||
else if (res == WMSZ.RIGHT + WMSZ.BOTTOM || res == WMSZ.LEFT + WMSZ.BOTTOM) {
|
||||
//Lower corner resize, adjust bottom
|
||||
rc.Bottom = rc.Top + (int)(width / AspectRatio) + ExtraPadding.Vertical + clientSizeConversion.Height;
|
||||
}
|
||||
else if (res == WMSZ.LEFT + WMSZ.TOP || res == WMSZ.RIGHT + WMSZ.TOP) {
|
||||
//Upper corner resize, adjust top
|
||||
rc.Top = rc.Bottom - (int)(width / AspectRatio) - ExtraPadding.Vertical - clientSizeConversion.Height;
|
||||
}
|
||||
|
||||
Marshal.StructureToPtr(rc, m.LParam, false);
|
||||
}
|
||||
else if (res == WMSZ.TOP || res == WMSZ.BOTTOM) {
|
||||
//Up or down resize, adjust left and right
|
||||
int targetWidth = (int)(height * AspectRatio);
|
||||
int diffWidth = width - targetWidth;
|
||||
|
||||
rc.Left += (int)(diffWidth / 2.0);
|
||||
rc.Right = rc.Left + targetWidth + ExtraPadding.Horizontal + clientSizeConversion.Width;
|
||||
}
|
||||
else if (res == WMSZ.RIGHT + WMSZ.BOTTOM || res == WMSZ.LEFT + WMSZ.BOTTOM) {
|
||||
//Lower corner resize, adjust bottom
|
||||
rc.Bottom = rc.Top + (int)(width / AspectRatio) + ExtraPadding.Vertical + clientSizeConversion.Height;
|
||||
}
|
||||
else if (res == WMSZ.LEFT + WMSZ.TOP || res == WMSZ.RIGHT + WMSZ.TOP) {
|
||||
//Upper corner resize, adjust top
|
||||
rc.Top = rc.Bottom - (int)(width / AspectRatio) - ExtraPadding.Vertical - clientSizeConversion.Height;
|
||||
}
|
||||
|
||||
Marshal.StructureToPtr(rc, m.LParam, false);
|
||||
}
|
||||
|
||||
base.WndProc(ref m);
|
||||
|
|
|
@ -101,6 +101,13 @@ namespace OnTopReplica {
|
|||
RefreshScreenLock();
|
||||
}
|
||||
|
||||
protected override void OnResizing(EventArgs e) {
|
||||
//Update aspect ratio from thumbnail while resizing (but do not refresh, resizing does that anyway)
|
||||
if (_thumbnailPanel.IsShowingThumbnail) {
|
||||
SetAspectRatio(_thumbnailPanel.ThumbnailPixelSize, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnActivated(EventArgs e) {
|
||||
base.OnActivated(e);
|
||||
|
||||
|
@ -127,8 +134,13 @@ namespace OnTopReplica {
|
|||
base.OnMouseWheel(e);
|
||||
|
||||
if (!FullscreenManager.IsFullscreen) {
|
||||
if (_thumbnailPanel.IsShowingThumbnail) {
|
||||
SetAspectRatio(_thumbnailPanel.ThumbnailPixelSize, false);
|
||||
}
|
||||
|
||||
int change = (int)(e.Delta / 6.0); //assumes a mouse wheel "tick" is in the 80-120 range
|
||||
AdjustSize(change);
|
||||
|
||||
RefreshScreenLock();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue