I failed to fill MainWindow,xaml.cs in the answer, I filled it in the comment.
MainWindow,xaml.cs:
bool ResizeInProcess = false;
private void Resize_Init(object sender, MouseButtonEventArgs e)
{
Rectangle senderRect = sender as Rectangle;
if (senderRect != null)
{
ResizeInProcess = true;
senderRect.CaptureMouse();
}
}
private void Resize_End(object sender, MouseButtonEventArgs e)
{
Rectangle senderRect = sender as Rectangle;
if (senderRect != null)
{
ResizeInProcess = false; ;
senderRect.ReleaseMouseCapture();
}
}
private void Resizeing_Window(object sender, MouseEventArgs e)
{
if (ResizeInProcess)
{
Rectangle senderRect = sender as Rectangle;
Window mainWindow = senderRect.Tag as Window;
if (senderRect != null)
{
double width = e.GetPosition(mainWindow).X;
double height = e.GetPosition(mainWindow).Y;
senderRect.CaptureMouse();
if (senderRect.Name.ToLower().Contains("right"))
{
width += 5;
if (width > 0)
mainWindow.Width = width;
}
if (senderRect.Name.ToLower().Contains("left"))
{
width -= 5;
mainWindow.Left += width;
width = mainWindow.Width - width;
if (width > 0)
{
mainWindow.Width = width;
}
}
if (senderRect.Name.ToLower().Contains("bottom"))
{
height += 5;
if (height > 0)
mainWindow.Height = height;
}
if (senderRect.Name.ToLower().Contains("top"))
{
height -= 5;
mainWindow.Top += height;
height = mainWindow.Height - height;
if (height > 0)
{
mainWindow.Height = height;
}
}
}
}
}
update:
Base on my search, you could use a transparent window to avoid this problem. First maximize the transparent window, and then put the content in the Canvas.
Users will not know that what they see on the screen is not a real "window."
MainWindow.xaml:
<Window x:Class="windowResizeProblem.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:windowResizeProblem"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
WindowStyle="None" WindowState="Maximized"
Background="Transparent" AllowsTransparency="True">
<Canvas>
<Border x:Name="Content" Canvas.Left="500" Canvas.Top="500" Width="500" Height="500"
MinWidth="100" MinHeight="100" CornerRadius="2" Background="AliceBlue">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="10"/>
</Border.Effect>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="4" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="4" />
</Grid.RowDefinitions>
<Thumb x:Name="topSizeGrip" Opacity="0" Grid.Row="0" Grid.Column="1" Cursor="SizeNS" DragDelta="Thumb_DragDelta"/>
<Thumb x:Name="bottomSizeGrip" Opacity="0" Grid.Row="3" Grid.Column="1" Cursor="SizeNS" DragDelta="Thumb_DragDelta"/>
<Thumb x:Name="leftSizeGrip" Opacity="0" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Cursor="SizeWE" DragDelta="Thumb_DragDelta"/>
<Thumb x:Name="rightSizeGrip" Opacity="0" Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Cursor="SizeWE" DragDelta="Thumb_DragDelta"/>
<Thumb x:Name="bottomRightSizeGrip" Opacity="0" Grid.Row="3" Grid.Column="2" Cursor="SizeNWSE" Tag="BR" DragDelta="Thumb_DragDelta"/>
<Border Grid.Row="2" Grid.Column="1" CornerRadius="3" BorderThickness="1" BorderBrush="LightSeaGreen" Background="LightGoldenrodYellow">
<TextBlock Text="my window" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
</Border>
</Canvas>
</Window>
MainWindow.xaml.cs:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace windowResizeProblem
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
var str = (string)((Thumb)sender).Name.ToLower();
if (str.Contains("top"))
{
Content.Height = Math.Min(Math.Max(Content.MinHeight, Content.ActualHeight - e.VerticalChange), Content.MaxHeight);
Canvas.SetTop(Content, Canvas.GetTop(Content) - Content.Height + Content.ActualHeight);
}
if (str.Contains("left"))
{
Content.Width = Math.Min(Math.Max(Content.MinWidth, Content.ActualWidth - e.HorizontalChange), Content.MaxWidth);
Canvas.SetLeft(Content, Canvas.GetLeft(Content) - Content.Width + Content.ActualWidth);
}
if (str.Contains("bottom"))
{
Content.Height = Math.Min(Math.Max(Content.MinHeight, Content.ActualHeight + e.VerticalChange), Content.MaxHeight);
}
if (str.Contains("right"))
{
Content.Width = Math.Min(Math.Max(Content.MinWidth, Content.ActualWidth + e.HorizontalChange), Content.MaxWidth);
}
e.Handled = true;
}
}
}
The result: