none
Increase the window hight and width whenever the height gets increased?

    Question

  • Hi all,

               i want to increase the height and width of the window whenever the height or width gets increased.

    Example: see the image

    if we resize the window from that corner then the height and width both gets increased.

    i want the same behaviour whenever i increase the height or width of the window.

    is that possible?

    Tuesday, January 07, 2014 7:09 AM

Answers

  • Hi,

    Here is small sample i developed from replies in different forums

    public partial class MainWindow : Window
        {
            private double _aspectRatio;
            private bool? _adjustingHeight = null;
    
            public MainWindow()
            {
                InitializeComponent();
                this.SourceInitialized += Window_SourceInitialized;
            }
    
            private void Window_SourceInitialized(object sender, EventArgs ea)
            {
                HwndSource hwndSource = (HwndSource)HwndSource.FromVisual((Window)sender);
                hwndSource.AddHook(DragHook);
    
                _aspectRatio = this.Width / this.Height;
            }
    
            
    
            internal enum SWP
            {
                NOMOVE = 0x0002
            }
            internal enum WM
            {
                WINDOWPOSCHANGING = 0x0046,
                EXITSIZEMOVE = 0x0232,
            }
    
            [StructLayout(LayoutKind.Sequential)]
            internal struct WINDOWPOS
            {
                public IntPtr hwnd;
                public IntPtr hwndInsertAfter;
                public int x;
                public int y;
                public int cx;
                public int cy;
                public int flags;
            }
    
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            internal static extern bool GetCursorPos(ref Win32Point pt);
    
            [StructLayout(LayoutKind.Sequential)]
            internal struct Win32Point
            {
                public Int32 X;
                public Int32 Y;
            };
    
            public static Point GetMousePosition()
            {
                Win32Point w32Mouse = new Win32Point();
                GetCursorPos(ref w32Mouse);
                return new Point(w32Mouse.X, w32Mouse.Y);
            }
    
            private IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                switch ((WM)msg)
                {
                    case WM.WINDOWPOSCHANGING:
                        {
                            WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
    
                            if ((pos.flags & (int)SWP.NOMOVE) != 0)
                                return IntPtr.Zero;
    
                            Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual;
                            if (wnd == null)
                                return IntPtr.Zero;
    
                            // determine what dimension is changed by detecting the mouse position relative to the 
                            // window bounds. if gripped in the corner, either will work.
                            if (!_adjustingHeight.HasValue)
                            {
                                Point p = GetMousePosition();
    
                                double diffWidth = Math.Min(Math.Abs(p.X - pos.x), Math.Abs(p.X - pos.x - pos.cx));
                                double diffHeight = Math.Min(Math.Abs(p.Y - pos.y), Math.Abs(p.Y - pos.y - pos.cy));
    
                                _adjustingHeight = diffHeight > diffWidth;
                            }
    
                            if (_adjustingHeight.Value)
                                pos.cy = (int)(pos.cx / _aspectRatio); // adjusting height to width change
                            else
                                pos.cx = (int)(pos.cy * _aspectRatio); // adjusting width to heigth change
    
                            Marshal.StructureToPtr(pos, lParam, true);
                            handled = true;
                        }
                        break;
                    case WM.EXITSIZEMOVE:
                        _adjustingHeight = null; // reset adjustment dimension and detect again next time window is resized
                        break;
                }
    
                return IntPtr.Zero;
            }
    
        }



    srithar


    • Edited by Srithar Friday, January 10, 2014 11:18 AM
    • Marked as answer by Gokul Sundharaj Monday, January 13, 2014 6:32 AM
    Friday, January 10, 2014 11:11 AM

All replies

  • Hi Gokul,

    yes it is. What you want to do is to keep the aspect ratio: http://stackoverflow.com/questions/2471867/resize-a-wpf-window-but-maintain-proportions


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest app: The "Womanizer" :-)
    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    Tuesday, January 07, 2014 9:30 AM
  • Hi Musakkhir ,

                          sorry, i am not asking this.

    let me explain the issue one more time,

    a). if you resize the window from corner(ResizeGrip) then both height and width will be increase or decrease right?

    b). same behaviour i want whenever i increase the height.

         that means if i increase the width then it should automatically increase the height also. got it?

    hi Thomas ,

                your solution is good , but window gets flickering.

    Tuesday, January 07, 2014 11:15 AM
  • Set the same height and width on window SizeChanged event. SizeChanged="Window_SizeChanged_1"

    private void Window_SizeChanged_1(object sender, SizeChangedEventArgs e)
            {
                if (e.WidthChanged)
                {
                    this.Height = this.Width;
                }
                else if (e.HeightChanged)
                {
                    this.Width = this.Height;
                } }


    Musakkhir Sayyed.


    Tuesday, January 07, 2014 11:56 AM
  • Hi Musakkhir ,

                   again sorry, our screen height and width are not same.

    also its getting flicker whenever resize

    Friday, January 10, 2014 7:20 AM
  • Hi,

    Here is small sample i developed from replies in different forums

    public partial class MainWindow : Window
        {
            private double _aspectRatio;
            private bool? _adjustingHeight = null;
    
            public MainWindow()
            {
                InitializeComponent();
                this.SourceInitialized += Window_SourceInitialized;
            }
    
            private void Window_SourceInitialized(object sender, EventArgs ea)
            {
                HwndSource hwndSource = (HwndSource)HwndSource.FromVisual((Window)sender);
                hwndSource.AddHook(DragHook);
    
                _aspectRatio = this.Width / this.Height;
            }
    
            
    
            internal enum SWP
            {
                NOMOVE = 0x0002
            }
            internal enum WM
            {
                WINDOWPOSCHANGING = 0x0046,
                EXITSIZEMOVE = 0x0232,
            }
    
            [StructLayout(LayoutKind.Sequential)]
            internal struct WINDOWPOS
            {
                public IntPtr hwnd;
                public IntPtr hwndInsertAfter;
                public int x;
                public int y;
                public int cx;
                public int cy;
                public int flags;
            }
    
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            internal static extern bool GetCursorPos(ref Win32Point pt);
    
            [StructLayout(LayoutKind.Sequential)]
            internal struct Win32Point
            {
                public Int32 X;
                public Int32 Y;
            };
    
            public static Point GetMousePosition()
            {
                Win32Point w32Mouse = new Win32Point();
                GetCursorPos(ref w32Mouse);
                return new Point(w32Mouse.X, w32Mouse.Y);
            }
    
            private IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                switch ((WM)msg)
                {
                    case WM.WINDOWPOSCHANGING:
                        {
                            WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
    
                            if ((pos.flags & (int)SWP.NOMOVE) != 0)
                                return IntPtr.Zero;
    
                            Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual;
                            if (wnd == null)
                                return IntPtr.Zero;
    
                            // determine what dimension is changed by detecting the mouse position relative to the 
                            // window bounds. if gripped in the corner, either will work.
                            if (!_adjustingHeight.HasValue)
                            {
                                Point p = GetMousePosition();
    
                                double diffWidth = Math.Min(Math.Abs(p.X - pos.x), Math.Abs(p.X - pos.x - pos.cx));
                                double diffHeight = Math.Min(Math.Abs(p.Y - pos.y), Math.Abs(p.Y - pos.y - pos.cy));
    
                                _adjustingHeight = diffHeight > diffWidth;
                            }
    
                            if (_adjustingHeight.Value)
                                pos.cy = (int)(pos.cx / _aspectRatio); // adjusting height to width change
                            else
                                pos.cx = (int)(pos.cy * _aspectRatio); // adjusting width to heigth change
    
                            Marshal.StructureToPtr(pos, lParam, true);
                            handled = true;
                        }
                        break;
                    case WM.EXITSIZEMOVE:
                        _adjustingHeight = null; // reset adjustment dimension and detect again next time window is resized
                        break;
                }
    
                return IntPtr.Zero;
            }
    
        }



    srithar


    • Edited by Srithar Friday, January 10, 2014 11:18 AM
    • Marked as answer by Gokul Sundharaj Monday, January 13, 2014 6:32 AM
    Friday, January 10, 2014 11:11 AM
  • hi sridhar

                   great solutions ,

    thanks(mikka nanri, i guess you are from tamilnadu).

    Monday, January 13, 2014 6:32 AM