I came around with this post. Which is almost perfect, but when you drag the left or upper bounds you get the window moving... Here is the fixed code:
#region Keep video window aspect ratio
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public class MyRect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public const int WM_SIZING = 0x0214;
int _lastWidth = 0;
MyRect _lastRect = null;
double _aspectRatio = 0.5;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SIZING)
{
RECT rc;
rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
if (_lastRect == null)
{
_lastRect = new MyRect();
_lastRect.Top = rc.Top;
_lastRect.Bottom = rc.Bottom;
_lastRect.Left = rc.Left ;
_lastRect.Right = rc.Right;
}
if (rc.Right - rc.Left != _lastWidth)
{
double newHeight = ((double)(rc.Right - rc.Left))
/ _aspectRatio;
rc.Bottom = (int)newHeight + _lastRect.Top;
Marshal.StructureToPtr(rc, m.LParam, true);
}
else
{
double newWidth = ((double)(rc.Bottom - rc.Top))
* _aspectRatio;
rc.Right = (int)newWidth + _lastRect.Left;
Marshal.StructureToPtr(rc, m.LParam, true);
}
_lastWidth = rc.Right - rc.Left;
_lastRect.Top = rc.Top;
_lastRect.Bottom = rc.Bottom;
_lastRect.Left = rc.Left;
_lastRect.Right = rc.Right;
}
base.WndProc(ref m);
}
#endregion
No comments:
Post a Comment