Resizing borderless WPF window

I create my own custom window in WPF, and I am trying to implement the resize function that I used earlier in WinForms. For some reason, the return value of my WndProc does not give me the correct result.

I have a NativeMethods class for all 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 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 "resize down arrow" and resize functionality to work the same way as in my WinForms project. I set breakpoints and the HTBOTTOM return fires when the cursor is in the expected location. In XAML, my ResizeMode is set to CanResize, and WindowStyle is None. What am I doing wrong?

+6
c # wpf cursor window-resize
Jul 15 '13 at 0:57
source share
4 answers

Well, that was a stupid mistake. I forgot to add handled = true; before I get the result back. Now the window is functioning properly. As a side note, if you set ResizeMode to NoResize, this code will not work at all.

+2
Jul 15 '13 at 11:57
source share

It might be easier to assign WindowChrome. According to your comment, you should be able to resize from all sides, as well as using grip.You can do all this by setting WindowStyle to None and ResizeMode to CanResizeWithGrip or CanResize (whatever you want to succeed)

 <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, you need to set the Chrome window for the window. You can do it as follows:

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

OR You can use setter to style the window, for example:

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

MSDN link for more information

Note that the WindowChrome class is part of the .NET 4.5 Framework. For .NET 4.0 users archive.msdn.microsoft.com/WPFShell

+11
Dec 10 '13 at 15:08
source share

I wrote the solution in another post, you can resize the window, you need to use .NET 4.5 or WPFShell

You can also put WindowChrome code directly on your MainWindow.xaml like this, and it works fine without putting a setter.

 <Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Concursos" mc:Ignorable="d" Title="Concuros" Height="350" Width="525" WindowStyle="None" WindowState="Normal" ResizeMode="CanResize" > <WindowChrome.WindowChrome> <WindowChrome CaptionHeight="0" ResizeBorderThickness="5" /> </WindowChrome.WindowChrome> <Grid> <YOUR CODE HERE </Grid> 

You can go here to view the full entry.

Decision

Here before and after

The challengeThe solution

+2
Mar 28 '16 at 9:39
source share

I also provided the source code for a fully working (meaningful, etc.) WPF fieldless window that might interest you. It can be found here.

Problems with WPF Borderless Window: Aero Snap and Maximising

0
Feb 05 '18 at 15:51
source share



All Articles