none
Resize proportionaly window

    Question

  • Hello to all! I need to realize a window that at the same time resizes width and height, maintaining therefore the proportion of the window.

    How can I do it?
    Thanks!
    Wednesday, January 03, 2007 11:37 PM

Answers

All replies

  • Hi did you find the solution?

     

    Thx

    Wednesday, January 17, 2007 9:22 PM
  • Yes, but unfortunately the graphical effect is much ugly one. It would be necessary disable design the window during the dragging., but I dont know like making
    Thursday, January 18, 2007 12:14 AM
  • Unfortunately, there is no built in support for proportionaly sizing the window.  The layout effect you're seeing is because SizeChanged is fired by the layout system and then you're resize the window from that event handler.  This causes the layout system to go in a loop.  One thing to try would be to resize the window from a Dispatcher work item using Dispatcher.BeginInvoke but that might not give you the desired behavior.

    A slightly more involved way of doing this involves using win32 interop.  You can use interop and listen to WM_SIZING message and control the rect of the window.  WM_SIZING is sent by win32 when a user is dragging the window to resize it.  Restricting the RECT inside WM_SIZING message restricts the size of the window on the screen.

    Following are the steps you can take to start listening to WM_SIZING message:

    1. Hook up a handler for Window.SourceInitialized event.  From the handler, get the HwndSource using PresentationSource.FromVisual method.  Once you have the HwndSource object (for that Window), use AddHook to start listening to window messages. 
    2. Handle WM_SIZING message and adjust the RECT pointed to by lParam accordingly.
    Thursday, January 18, 2007 2:06 AM
  • Api way? In wpf? Oh no :-) Thx for a reply.
    Thursday, January 18, 2007 6:31 AM
  • Thanks. You have a link for one example of use?
    Thursday, January 18, 2007 11:27 AM
  • For WPF i thing dont exist :-(
    Thursday, January 18, 2007 12:02 PM
  • The question was for Hamid, but thanks for the answer
    Thursday, January 18, 2007 12:09 PM
  • I have it, now how could i resize window? Send a size api message?

    void Window1_SourceInitialized(object sender, EventArgs e)

    {

    HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;

    hwndSource.AddHook(new HwndSourceHook(WndProc));

    }

    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)

    {

    if (msg == WM_NCHITTEST)

    {

    /*handled = true;*/

    }

    /*if (msg == WM_SIZING)

    {

    System.Diagnostics.Debug.WriteLine("a");

    lParam = (IntPtr)HitTest.HTBOTTOMRIGHT;

    handled = true;

    }*/

    return IntPtr.Zero;

    }

    private const int WM_SIZING = 0x0214;

    private const int WM_NCHITTEST = 0x0084;

    Thursday, January 18, 2007 7:53 PM
  • First of all, you will have to define a managed struct corresponding to native RECT struct that lParam points to.  Let's call this "managed RECT." Then you will have to use Marshall.PrtToStructure to marshall data from the native RECT to the one defined by you.  Then you can makes changes to the values of the managed RECT based on your proportions.  Finally, you will have to use Marshall.StructureToPtr to marshall data back to the native RECT pointed to by lParam.  Furthermore, don't handle the message i.e. don't set handled to true.  All you need to do in WM_SIZING is to update the bounding rectangle of the Window.
    Friday, January 19, 2007 3:22 AM
  • "All you need to do in WM_SIZING" no you dont understand. I need WM_SIZING  message invoke in WM_NCHITTEST, thats is my problem. How to change or send new Message in WndProc. Ivant to do a custom resizing in custom rectangle.

    i need something like this:

    http://www.sturmnet.org/blog/archives/2006/12/07/custom-handling-of-the-non-client-area/

    in wpf

    Thx a lot

     

    Friday, January 19, 2007 6:50 AM
  • Does my post in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1121759&SiteID=1 answer your question?  That talks about enabling resizing of transparent windows.  It it does not, can you please explain your scenario in more detail.

    Thanks!

    Friday, January 19, 2007 9:40 PM
  • Oh its working :-) Thx a lot. The WndProc in wpf is diffent form net 2.0. I didnt now thet i muss return a value:

    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)

    {

    if (msg == WM_NCHITTEST)

    {

    handled = true;

    return new IntPtr((int)HitTest.HTRIGHT);

    }

    return IntPtr.Zero;

    }

     

    THX THX THX THX THX A LOT

    Saturday, January 20, 2007 9:55 AM
  • Here is a result: (any idea, why is the window "shaking") Resizing is implemented on the left side. The shaking is horrible :-(

    <Window x:Class="WpfTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowTransparency" Height="483" Width="300" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="Transparent" AllowsTransparency="True" x:Name="WindowName" BorderBrush="#FFEE1515" BorderThickness="1,1,1,1">
     
     <DockPanel x:Name="PanelMain">
      <Border Width="Auto" Height="Auto" CornerRadius="10,10,10,10" Canvas.Left="8" Canvas.Top="8" Background="Azure" x:Name="MainBorder"/>
     </DockPanel>
    </Window>

     

    public partial class Window1 : System.Windows.Window

    {

    public Window1()

    {

    this.SourceInitialized += new EventHandler(Window1_SourceInitialized);

    InitializeComponent();

    }

    void Window1_SourceInitialized(object sender, EventArgs e)

    {

    HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;

    hwndSource.AddHook(new HwndSourceHook(WndProc));

    }

    private const int WM_NCHITTEST = 0x0084;

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)

    {

    if (msg == WM_NCHITTEST)

    {

    int xPos = GET_X_LPARAM(lParam.ToInt32());

    int yPos = GET_Y_LPARAM(lParam.ToInt32());

    double xIn = xPos - this.Left;

    double yIn = yPos - this.Top;

    System.Diagnostics.Debug.WriteLine("x:" + xIn.ToString() + " y:" + yIn.ToString());

    if (xIn < 2)

    {

    handled = true;

    return new IntPtr((int)HitTest.HTLEFT);

    }

    }

    return IntPtr.Zero;

    }

    public static Int32 GET_X_LPARAM(int lParam)

    {

    return (lParam & 0xffff);

    }

    public static Int32 GET_Y_LPARAM(int lParam)

    {

    return (lParam >> 16);

    }

    public enum HitTest

    {

    HTERROR = -2,

    HTTRANSPARENT = -1,

    HTNOWHERE = 0,

    HTCLIENT = 1,

    HTCAPTION = 2,

    HTSYSMENU = 3,

    HTGROWBOX = 4,

    HTSIZE = HTGROWBOX,

    HTMENU = 5,

    HTHSCROLL = 6,

    HTVSCROLL = 7,

    HTMINBUTTON = 8,

    HTMAXBUTTON = 9,

    HTLEFT = 10,

    HTRIGHT = 11,

    HTTOP = 12,

    HTTOPLEFT = 13,

    HTTOPRIGHT = 14,

    HTBOTTOM = 15,

    HTBOTTOMLEFT = 16,

    HTBOTTOMRIGHT = 17,

    HTBORDER = 18,

    HTREDUCE = HTMINBUTTON,

    HTZOOM = HTMAXBUTTON,

    HTSIZEFIRST = HTLEFT,

    HTSIZELAST = HTBOTTOMRIGHT,

    HTOBJECT = 19,

    HTCLOSE = 20,

    HTHELP = 21,

    }

    }

    Saturday, January 20, 2007 11:11 AM
  • Hi guys,

    I was experimenting with keeping aspect ratio of the window while resizing.
    The problem is the WM_POSCHANGING message from API is not wrapped in WPF, so you may try to play around with setting MaxWidth/MinWidth and MaxHeight/MinHeight during window sizing.

    This will make flicker free resizing.
    Thursday, February 01, 2007 10:26 AM
  • This is a common problem to all windows on Windows operating system. when you resize the window from the left, top, bottom right, top right, top left side, the window will always get shaking.

    Sheva
    Thursday, February 01, 2007 12:07 PM
  • A property? not an error? Thats not a goood solution for problem :(

    I will try ceck WM_POSCHANGING.

    Thursday, February 01, 2007 12:32 PM
  • Sample code snippet at http://wpf.netfx3.com/files/folders/code_snippets/entry8572.aspx shows how proportional resizing of window can be achieved using the WM_SIZING message.

    Thursday, February 01, 2007 10:25 PM
  • Sorry, bud this example has more errors then a clasic handling. I will publish my example, perhaps next week.
    Friday, February 02, 2007 7:14 AM
  • Thanks work perfectly!!!!!!
    Friday, February 02, 2007 4:05 PM
  • Hello Hamid,

    the link http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1121759&SiteID=1 is broken. Could you please post again?

    Best regards,

    Martin

    Wednesday, August 04, 2010 10:37 AM