Skip to content

Commit 384d299

Browse files
committed
Implemented window resize handlers in corners and sides
1 parent d2af3b4 commit 384d299

3 files changed

Lines changed: 145 additions & 6 deletions

File tree

Src/ScreenGrid/ViewModels/ScreenGridViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public Grid ContentControl
176176
#region Window position and size
177177

178178
private const double OuterBorderWidth = 1.0;
179-
private const double HeaderHeight = 24.0;
179+
180+
public const double HeaderHeight = 24.0;
180181

181182
private double windowWidth = 400.0 + 2 * OuterBorderWidth;
182183
public double WindowWidth

Src/ScreenGrid/Views/ScreenGridWindow.xaml

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
Height="{Binding WindowHeight, Mode=TwoWay}"
99
Topmost="True"
1010
WindowStyle="None"
11-
ResizeMode="CanResizeWithGrip"
11+
ResizeMode="CanResize"
1212
AllowsTransparency="True"
1313
MinWidth="150"
1414
MinHeight="50"
1515
WindowStartupLocation="CenterScreen"
1616
Background="Transparent"
1717
Loaded="Window_Loaded"
1818
Closing="Window_Closing">
19-
19+
2020
<Window.Resources>
2121
<ResourceDictionary>
2222
<ResourceDictionary.MergedDictionaries>
@@ -47,14 +47,24 @@
4747
<Setter Property="Stroke" Value="Black" />
4848
<Setter Property="Fill" Value="Black" />
4949
</Style>
50+
<Style x:Key="RectBorderStyle" TargetType="Rectangle">
51+
<Setter Property="Focusable" Value="False" />
52+
<Setter Property="Fill" Value="White" />
53+
<Setter Property="Opacity" Value="0.5" />
54+
<Setter Property="Stroke" Value="Magenta" />
55+
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
56+
<EventSetter Event="MouseLeftButtonDown" Handler="ResizeInit"/>
57+
<EventSetter Event="MouseLeftButtonUp" Handler="ResizeEnd"/>
58+
<EventSetter Event="MouseMove" Handler="ResizingForm"/>
59+
</Style>
5060
</Grid.Resources>
5161

5262
<StackPanel Orientation="Horizontal" Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
5363
<Button Name="btnMenu" Click="btnMenu_Click"
5464
ToolTip="Select grid type">
5565
<ContentControl>
5666
<Path Data="{Binding Source={StaticResource pathDataRow}}" />
57-
</ContentControl>
67+
</ContentControl>
5868
</Button>
5969
<Button ToolTip="Snap to image borders in topmost window"
6070
Command="{Binding SnapCommand}">
@@ -66,7 +76,7 @@
6676
Command="{Binding RotateCounterClockwiseCommand}">
6777
<ContentControl>
6878
<Path Data="{Binding Source={StaticResource pathDataRotate01}}" />
69-
</ContentControl>
79+
</ContentControl>
7080
</Button>
7181
<Button ToolTip="Rotate grid clockwise"
7282
Command="{Binding RotateClockwiseCommand}">
@@ -100,7 +110,7 @@
100110
ToolTip="Grid lines color"
101111
ItemsSource="{Binding LineColors, Mode=OneTime}"
102112
SelectedItem="{Binding SelectedLineColor, Mode=TwoWay}">
103-
<ComboBox.ItemTemplate>
113+
<ComboBox.ItemTemplate>
104114
<DataTemplate>
105115
<StackPanel Orientation="Horizontal" Margin="2">
106116
<Rectangle Stroke="Black"
@@ -142,5 +152,47 @@
142152
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
143153
Content="{Binding ContentControl}" />
144154

155+
<Rectangle x:Name="LeftSizeGrip" Grid.Row="2"
156+
Width="3" Height="50"
157+
HorizontalAlignment="Left" VerticalAlignment="Center"
158+
Cursor="SizeWE"
159+
Style="{StaticResource RectBorderStyle}" />
160+
161+
<Rectangle x:Name="RightSizeGrip" Grid.Row="2"
162+
Width="3" Height="50"
163+
HorizontalAlignment="Right" VerticalAlignment="Center"
164+
Cursor="SizeWE"
165+
Style="{StaticResource RectBorderStyle}" />
166+
167+
<Rectangle x:Name="BottomSizeGrip" Grid.Row="2"
168+
Width="50" Height="3"
169+
HorizontalAlignment="Center" VerticalAlignment="Bottom"
170+
Cursor="SizeNS"
171+
Style="{StaticResource RectBorderStyle}" />
172+
173+
<Rectangle Name="BottomRightSizeGrip" Grid.Row="2"
174+
Width="5" Height="5"
175+
HorizontalAlignment="Right" VerticalAlignment="Bottom"
176+
Cursor="SizeNWSE"
177+
Style="{StaticResource RectBorderStyle}" />
178+
179+
<Rectangle Name="BottomLeftSizeGrip" Grid.Row="2"
180+
Width="5" Height="5"
181+
HorizontalAlignment="Left" VerticalAlignment="Bottom"
182+
Cursor="SizeNESW"
183+
Style="{StaticResource RectBorderStyle}" />
184+
185+
<Rectangle Name="TopLeftSizeGrip" Grid.Row="2"
186+
Width="5" Height="5"
187+
HorizontalAlignment="Left" VerticalAlignment="Top"
188+
Cursor="SizeNWSE"
189+
Style="{StaticResource RectBorderStyle}" />
190+
191+
<Rectangle Name="TopRightSizeGrip" Grid.Row="2"
192+
Width="5" Height="5"
193+
HorizontalAlignment="Right" VerticalAlignment="Top"
194+
Cursor="SizeNESW"
195+
Style="{StaticResource RectBorderStyle}" />
196+
145197
</Grid>
146198
</Window>

Src/ScreenGrid/Views/ScreenGridWindow.xaml.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows.Controls;
55
using System.Windows.Input;
66
using System.Windows.Media;
7+
using System.Windows.Shapes;
78

89
public partial class ScreenGridWindow : Window
910
{
@@ -117,5 +118,90 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
117118
this.Visibility = Visibility.Hidden;
118119
}
119120
}
121+
122+
#region ResizeWindows
123+
124+
private bool isResizeInProcess = false;
125+
126+
private void ResizeInit(object sender, MouseButtonEventArgs e)
127+
{
128+
var senderRect = sender as Rectangle;
129+
if (senderRect != null)
130+
{
131+
this.isResizeInProcess = true;
132+
senderRect.CaptureMouse();
133+
}
134+
}
135+
136+
private void ResizeEnd(object sender, MouseButtonEventArgs e)
137+
{
138+
var senderRect = sender as Rectangle;
139+
if (senderRect != null)
140+
{
141+
this.isResizeInProcess = false; ;
142+
senderRect.ReleaseMouseCapture();
143+
}
144+
}
145+
146+
private void ResizingForm(object sender, MouseEventArgs e)
147+
{
148+
if (this.isResizeInProcess)
149+
{
150+
var senderRect = sender as Rectangle;
151+
if (senderRect != null)
152+
{
153+
var window = senderRect.Tag as Window;
154+
var width = e.GetPosition(window).X;
155+
var height = e.GetPosition(window).Y;
156+
senderRect.CaptureMouse();
157+
158+
var senderName = senderRect.Name.ToLower();
159+
160+
if (senderName.Contains("right"))
161+
{
162+
width++;
163+
if (width > window.MinWidth)
164+
{
165+
window.Width = width;
166+
}
167+
}
168+
169+
if (senderName.Contains("left"))
170+
{
171+
width--;
172+
173+
window.Left += width;
174+
width = window.Width - width;
175+
if (width > window.MinWidth)
176+
{
177+
window.Width = width;
178+
}
179+
}
180+
181+
if (senderName.Contains("bottom"))
182+
{
183+
height++;
184+
if (height > window.MinHeight)
185+
{
186+
window.Height = height;
187+
}
188+
}
189+
190+
if (senderName.Contains("top"))
191+
{
192+
height -= ViewModels.ScreenGridViewModel.HeaderHeight;
193+
height--;
194+
window.Top += height;
195+
height = window.Height - height;
196+
if (height > window.MinHeight)
197+
{
198+
window.Height = height;
199+
}
200+
}
201+
}
202+
}
203+
}
204+
205+
#endregion
120206
}
121207
}

0 commit comments

Comments
 (0)