WPF Borderless window resize

Cris McLaughlin picture Cris McLaughlin · Jul 15, 2013 · Viewed 9.8k times · Source

I am designing my own custom window in WPF and I have been trying to implement the resizing functionality I have used previously in WinForms. For some reason the return value of my WndProc isn't giving me the proper result.

I have a NativeMethods class for all my WndProc messages and results:

public class NativeMethods
{
    public const int WM_NCHITTEST  = 0x84;
    public const int HTCAPTION     = 2;
    public const int HTLEFT        = 10;
    public const int HTRIGHT       = 11;
    public const int HTTOP         = 12;
    public const int HTTOPLEFT     = 13;
    public const int HTTOPRIGHT    = 14;
    public const int HTBOTTOM      = 15;
    public const int HTBOTTOMLEFT  = 16;
    public const int HTBOTTOMRIGHT = 17;
}

And here is the code behind for my window:

public partial class MainWindow : Window
{
    const int GripSize   = 16;
    const int BorderSize = 7;

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        IntPtr windowHandle = new WindowInteropHelper(this).Handle;
        HwndSource windowSource = HwndSource.FromHwnd(windowHandle);
        windowSource.AddHook(WndProc);
    }

    private IntPtr WndProc(IntPtr hwnd, int msg,
        IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == NativeMethods.WM_NCHITTEST)
        {
            int x = lParam.ToInt32() << 16 >> 16, y = lParam.ToInt32() >> 16;
            Point pos = PointFromScreen(new Point(x, y));

            if (pos.X > GripSize && 
                pos.X < ActualWidth - GripSize &&
                pos.Y >= ActualHeight - BorderSize)
            {
                return (IntPtr)NativeMethods.HTBOTTOM; // This doesn't work?
            }

            // Top, Left, Right, Corners, Etc.
        }

        return IntPtr.Zero;
    }
}

I expected the cursor to change to the "resize down arrow" and the resizing functionality to work as it did in my WinForms project. I have set breakpoints and the HTBOTTOM return is firing when the cursor is in the expected location. In XAML I have ResizeMode set to CanResize and the WindowStyle set to None. What am I doing wrong?

Answer

kiran picture kiran · Dec 10, 2013

Maybe it is simpler to assign WindowChrome.As per your comment you must be able to resize from all the sides as well as using grip.You can do all this by setting the WindowStyle to None and ResizeMode to CanResizeWithGrip or CanResize (whatever you wish to acheive)

<Window x:Class="MVVMProtoType.View.Test.Test"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Test" Height="300" Width="300" WindowStyle="None" AllowsTransparency="False" ResizeMode="CanResizeWithGrip">

In the code behid you must set the Window Chrome for the window . You can do it like this :

WindowChrome.SetWindowChrome(this, new WindowChrome());

OR You can use setter for window style like :

<Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome CornerRadius="0" GlassFrameThickness="1" UseAeroCaptionButtons="False"/>
        </Setter.Value>
</Setter>

MSDN link for more information

Please note WindowChrome class is a part of .NET 4.5 Framework. For.NET 4.0 users check out archive.msdn.microsoft.com/WPFShell