Skip to content

Commit 2dbfbdc

Browse files
committed
fix for CustomWindowTemplate
1 parent 4ae45d3 commit 2dbfbdc

File tree

3 files changed

+78
-62
lines changed

3 files changed

+78
-62
lines changed

SimpleStateMachineNodeEditor/Styles/MainWindow/CustomWindowTemplate.cs

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,81 @@
33
using System.Windows.Interop;
44
using System.Runtime.InteropServices;
55
using System.Windows.Input;
6+
using System.Windows.Shapes;
67

78
namespace SimpleStateMachineNodeEditor.Styles
89
{
910
public partial class CustomWindowTemplate
1011
{
11-
#region sizing event handlers
12-
13-
void OnSizeSouth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.South); }
14-
15-
void OnSizeNorth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.North); }
16-
17-
void OnSizeEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.East); }
18-
19-
void OnSizeWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.West); }
20-
21-
void OnSizeNorthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthWest); }
22-
23-
void OnSizeNorthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthEast); }
24-
25-
void OnSizeSouthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthEast); }
26-
27-
void OnSizeSouthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthWest); }
28-
29-
void OnSize(object sender, SizingAction action)
12+
bool ResizeInProcess = false;
13+
int shift = 0;
14+
private void ResizeStart(object sender, MouseButtonEventArgs e)
3015
{
31-
if (((FrameworkElement)sender).TemplatedParent is Window wnd)
16+
Rectangle senderRect = sender as Rectangle;
17+
if (senderRect != null)
3218
{
33-
WindowInteropHelper helper = new WindowInteropHelper(wnd);
34-
DragSize(helper.Handle, action);
19+
ResizeInProcess = true;
20+
senderRect.CaptureMouse();
3521
}
3622
}
3723

38-
#endregion
39-
40-
#region P/Invoke and helper method
41-
42-
const int WM_SYSCOMMAND = 0x112;
43-
const int SC_SIZE = 0xF000;
44-
45-
46-
[DllImport("user32.dll", CharSet = CharSet.Auto)]
47-
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
48-
49-
void DragSize(IntPtr handle, SizingAction sizingAction)
24+
private void ResizeEnd(object sender, MouseButtonEventArgs e)
5025
{
51-
if (Mouse.LeftButton == MouseButtonState.Pressed)
26+
Rectangle senderRect = sender as Rectangle;
27+
if (senderRect != null)
5228
{
53-
SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero);
54-
SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero);
29+
ResizeInProcess = false; ;
30+
senderRect.ReleaseMouseCapture();
5531
}
5632
}
5733

58-
#endregion
59-
60-
#region helper enum
61-
62-
public enum SizingAction
34+
private void Resizeing_Form(object sender, MouseEventArgs e)
6335
{
64-
North = 3,
65-
South = 6,
66-
East = 2,
67-
West = 1,
68-
NorthEast = 5,
69-
NorthWest = 4,
70-
SouthEast = 8,
71-
SouthWest = 7
36+
if (ResizeInProcess)
37+
{
38+
double temp = 0;
39+
Rectangle senderRect = sender as Rectangle;
40+
Window mainWindow = senderRect.Tag as Window;
41+
if (senderRect != null)
42+
{
43+
double width = e.GetPosition(mainWindow).X;
44+
double height = e.GetPosition(mainWindow).Y;
45+
senderRect.CaptureMouse();
46+
if (senderRect.Name.Contains("right", StringComparison.OrdinalIgnoreCase))
47+
{
48+
width += shift;
49+
if (width > 0)
50+
mainWindow.Width = width;
51+
}
52+
if (senderRect.Name.Contains("left", StringComparison.OrdinalIgnoreCase))
53+
{
54+
width -= shift;
55+
temp = mainWindow.Width - width;
56+
if ((temp > mainWindow.MinWidth) && (temp < mainWindow.MaxWidth))
57+
{
58+
mainWindow.Width = temp;
59+
mainWindow.Left += width;
60+
}
61+
}
62+
if (senderRect.Name.Contains("bottom", StringComparison.OrdinalIgnoreCase))
63+
{
64+
height += shift;
65+
if (height > 0)
66+
mainWindow.Height = height;
67+
}
68+
if (senderRect.Name.ToLower().Contains("top", StringComparison.OrdinalIgnoreCase))
69+
{
70+
height -= shift;
71+
temp = mainWindow.Height - height;
72+
if ((temp > mainWindow.MinHeight) && (temp < mainWindow.MaxHeight))
73+
{
74+
mainWindow.Height = temp;
75+
mainWindow.Top += height;
76+
}
77+
}
78+
}
79+
}
7280
}
7381

74-
#endregion
7582
}
7683
}

SimpleStateMachineNodeEditor/Styles/MainWindow/CustomWindowTemplate.xaml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
x:Class="SimpleStateMachineNodeEditor.Styles.CustomWindowTemplate"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
5-
5+
6+
<Style x:Key="RectBorderStyle" TargetType="Rectangle">
7+
<Setter Property="Focusable" Value="False" />
8+
<Setter Property="Fill" Value="Transparent" />
9+
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
10+
<EventSetter Event="MouseLeftButtonDown" Handler="ResizeStart"/>
11+
<EventSetter Event="MouseLeftButtonUp" Handler="ResizeEnd"/>
12+
<EventSetter Event="MouseMove" Handler="Resizeing_Form"/>
13+
</Style>
14+
615
<ControlTemplate x:Key="CustomWindowTemplate" TargetType="{x:Type Window}">
716
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
817
<Grid >
@@ -11,17 +20,17 @@
1120
<ContentPresenter/>
1221
</AdornerDecorator>
1322
</Border>
14-
1523

16-
<Line MouseDown="OnSizeNorth" Name="lnSizeNorth" Stroke="Transparent" Cursor="SizeNS" X1="10" X2="{TemplateBinding ActualWidth}" Y1="1" Y2="1" StrokeThickness="5" />
17-
<Line MouseDown="OnSizeSouth" Name="lnSizeSouth" Stroke="Transparent" VerticalAlignment="Bottom" Cursor="SizeNS" X1="1" X2="{TemplateBinding ActualWidth}" Y1="{TemplateBinding ActualHeight}" Y2="{TemplateBinding ActualHeight}" StrokeThickness="5" />
18-
<Line MouseDown="OnSizeWest" Name="lnSizeWest" Stroke="Transparent" Cursor="SizeWE" X1="1" X2="1" Y1="1" Y2="{TemplateBinding ActualHeight}" StrokeThickness="5" />
19-
<Line MouseDown="OnSizeEast" Name="lnSizeEast" Stroke="Transparent" HorizontalAlignment="Right" Cursor="SizeWE" X1="{TemplateBinding ActualWidth}" X2="{TemplateBinding ActualWidth}" Y1="1" Y2="{TemplateBinding ActualHeight}" StrokeThickness="5" />
2024

21-
<Rectangle MouseDown="OnSizeNorthWest" Name="rectSizeNorthWest" Cursor="SizeNWSE" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Top" HorizontalAlignment="Left" />
22-
<Rectangle MouseDown="OnSizeNorthEast" Name="rectSizeNorthEast" Cursor="SizeNESW" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Top" HorizontalAlignment="Right" />
23-
<Rectangle MouseDown="OnSizeSouthWest" Name="rectSizeSouthWest" Cursor="SizeNESW" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
24-
<!--<Rectangle MouseDown="OnSizeSouthEast" Name="rectSizeSouthEast" Cursor="SizeNWSE" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Bottom" HorizontalAlignment="Right" />-->
25+
<Rectangle x:Name="left" Width="7" Margin="0,7,0,7" HorizontalAlignment="Left" Cursor="SizeWE" Style="{StaticResource RectBorderStyle}" />
26+
<Rectangle x:Name="right" Width="7" Margin="0,7,0,7" HorizontalAlignment="Right" Cursor="SizeWE" Style="{StaticResource RectBorderStyle}" />
27+
<Rectangle x:Name="top" Height="7" Margin="7,0,7,0" VerticalAlignment="Top" Cursor="SizeNS" Style="{StaticResource RectBorderStyle}" />
28+
<Rectangle x:Name="bottom" Height="7" Margin="7,0,7,0" VerticalAlignment="Bottom" Cursor="SizeNS" Style="{StaticResource RectBorderStyle}" />
29+
<!-- Corners -->
30+
<Rectangle Name="topLeft" Width="7" Height="7" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="SizeNWSE" Style="{StaticResource RectBorderStyle}" />
31+
<Rectangle Name="topRight" Width="7" Height="7" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="SizeNESW" Style="{StaticResource RectBorderStyle}" />
32+
<Rectangle Name="bottomLeft" Width="7" Height="7" HorizontalAlignment="Left" VerticalAlignment="Bottom" Cursor="SizeNESW" Style="{StaticResource RectBorderStyle}" />
33+
<!--<Rectangle Name="bottomRight" Width="5" Height="5" HorizontalAlignment="Right" VerticalAlignment="Bottom" Cursor="SizeNWSE" Style="{StaticResource RectBorderStyle}" />-->
2534

2635

2736
<ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" IsTabStop="false" Visibility="Collapsed" VerticalAlignment="Bottom"/>

SimpleStateMachineNodeEditor/View/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:view="clr-namespace:SimpleStateMachineNodeEditor.View"
77
x:Class="SimpleStateMachineNodeEditor.View.MainWindow"
88
mc:Ignorable="d"
9-
Title="SimpleStateMachineNodeEditor" d:DesignWidth="808.96" Height="738.592" BorderThickness="1" Padding="7,0,7,7" ResizeMode="CanResizeWithGrip" BorderBrush="{DynamicResource ColorWindowBorder}" Background="{DynamicResource ColorWindowHeader}" Foreground="{x:Null}" WindowStyle="None" AllowsTransparency="True" Style="{DynamicResource CustomWindowStyle}">
9+
Title="SimpleStateMachineNodeEditor" d:DesignWidth="550" Height="738.592" MinWidth="570" MinHeight="400" BorderThickness="1" Padding="7,0,7,7" ResizeMode="CanResizeWithGrip" BorderBrush="{DynamicResource ColorWindowBorder}" Background="{DynamicResource ColorWindowHeader}" Foreground="{x:Null}" WindowStyle="None" AllowsTransparency="True" Style="{DynamicResource CustomWindowStyle}">
1010
<Grid Background="{DynamicResource ColorWindowHeader}" >
1111
<Grid.RowDefinitions>
1212
<RowDefinition Height="auto" />

0 commit comments

Comments
 (0)