Skip to content

Commit 70aab89

Browse files
committed
Use InkCanvas
1 parent b936adb commit 70aab89

File tree

3 files changed

+42
-87
lines changed

3 files changed

+42
-87
lines changed

MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
Title="Whiteboard"
99
WindowStartupLocation="CenterScreen"
1010
FontFamily="Montserrat"
11-
BorderBrush="#EA4333"
11+
BorderBrush="DarkSlateGray"
1212
BorderThickness="1"
1313
Width="800"
14-
Height="523.921"
14+
Height="523.921"
1515
KeyDown="MainWindow_KeyDown">
1616
<Window.Resources>
1717
</Window.Resources>

MainWindow.xaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Windows;
33
using System.Windows.Controls;
44
using System.Windows.Input;
5-
5+
using System.Windows.Media;
66
using Brushes = System.Windows.Media.Brushes;
77

88
namespace Whiteboard
@@ -65,27 +65,27 @@ private void Btn_Delete_Click(object sender, RoutedEventArgs e)
6565
}
6666
private void Btn_Size1_Click(object sender, RoutedEventArgs e)
6767
{
68-
theWhiteboard.BrushThickness = 1.0;
68+
theWhiteboard.SetPenThickness(1.0);
6969
}
7070

7171
private void Btn_Size2_Click(object sender, RoutedEventArgs e)
7272
{
73-
theWhiteboard.BrushThickness = 2.0;
73+
theWhiteboard.SetPenThickness(2.0);
7474
}
7575

7676
private void Btn_Size3_Click(object sender, RoutedEventArgs e)
7777
{
78-
theWhiteboard.BrushThickness = 3.0;
78+
theWhiteboard.SetPenThickness(3.0);
7979
}
8080

8181
private void Btn_Size4_Click(object sender, RoutedEventArgs e)
8282
{
83-
theWhiteboard.BrushThickness = 4.0;
83+
theWhiteboard.SetPenThickness(4.0);
8484
}
8585

8686
private void Btn_Size5_Click(object sender, RoutedEventArgs e)
8787
{
88-
theWhiteboard.BrushThickness = 5.0;
88+
theWhiteboard.SetPenThickness(5.0);
8989
}
9090
}
9191
}

WhiteboardCanvas.cs

Lines changed: 34 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Windows.Controls;
3+
using System.Windows.Ink;
24
using System.Windows.Input;
5+
using System.Windows.Media;
36
using System.Windows.Shapes;
47
using Brushes = System.Windows.Media.Brushes;
58
using Canvas = System.Windows.Controls.Canvas;
@@ -9,15 +12,9 @@ namespace Whiteboard
912
class WhiteboardCanvas : Canvas
1013
{
1114

12-
private double InitX = -1;
13-
private double InitY = -1;
14-
15-
private bool paintOn;
16-
private double brushThickness;
17-
public System.Windows.Media.Brush BrushColor = Brushes.Black;
18-
19-
public double BrushThickness { get => brushThickness; set => brushThickness = value; }
20-
public bool PaintOn { get => paintOn; set => paintOn = value; }
15+
private double brushThickness = 1.0;
16+
private Color brushColor = Colors.Black;
17+
private InkCanvas inkCanvas;
2118

2219
public WhiteboardCanvas()
2320
{
@@ -26,104 +23,62 @@ public WhiteboardCanvas()
2623

2724
private void InitializeComponent()
2825
{
29-
this.MouseUp += WhiteboardCanvas_MouseUp;
30-
this.MouseDown += WhiteboardCanvas_MouseDown;
31-
this.MouseEnter += WhiteboardCanvas_MouseEnter;
32-
this.MouseLeave += WhiteboardCanvas_MouseLeave;
33-
this.MouseMove += WhiteboardCanvas_MouseMove;
34-
35-
brushThickness = 1.0;
36-
}
37-
38-
public void Paint(object sender, MouseEventArgs e)
39-
{
40-
if (PaintOn)
41-
{
42-
double X = e.GetPosition(this).X;
43-
double Y = e.GetPosition(this).Y;
44-
45-
Line line = new Line()
46-
{
47-
Stroke = BrushColor,
48-
StrokeThickness = BrushThickness,
49-
X1 = InitX,
50-
Y1 = InitY,
51-
X2 = X,
52-
Y2 = Y
53-
};
54-
55-
if (InitX == -1 || InitY == -1)
56-
{
57-
line.X1 = X;
58-
line.Y1 = Y;
59-
}
26+
inkCanvas = new InkCanvas();
27+
inkCanvas.Background = Brushes.Transparent;
28+
SizeChanged += WhiteboardCanvas_SizeChanged;
6029

61-
this.Children.Add(line);
30+
inkCanvas.UseCustomCursor = true;
31+
inkCanvas.Cursor = this.Cursor;
6232

63-
InitX = X;
64-
InitY = Y;
65-
}
33+
this.Children.Add(inkCanvas);
6634
}
6735

68-
public void BeginPaint(object sender, MouseButtonEventArgs e)
36+
private void WhiteboardCanvas_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
6937
{
70-
PaintOn = true;
38+
// make sure the ink canvas also changes
39+
inkCanvas.Width = this.Width;
40+
inkCanvas.Height = this.Height;
7141
}
7242

73-
public void StopPaint(object sender, MouseButtonEventArgs e)
43+
private void setPenAttributes(Color color, double size)
7444
{
75-
PaintOn = false;
76-
77-
InitX = -1;
78-
InitY = -1;
45+
DrawingAttributes inkDA = new DrawingAttributes();
46+
inkDA.Width = size;
47+
inkDA.Height = size;
48+
inkDA.Color = color;
49+
inkCanvas.DefaultDrawingAttributes = inkDA;
7950
}
8051

81-
82-
private void WhiteboardCanvas_MouseDown(object sender, MouseButtonEventArgs e)
52+
public void SetPenColor(Color color)
8353
{
84-
BeginPaint(sender, e);
54+
brushColor = color;
55+
setPenAttributes(brushColor, brushThickness);
8556
}
8657

87-
private void WhiteboardCanvas_MouseMove(object sender, MouseEventArgs e)
58+
public void SetPenColor(Brush color)
8859
{
89-
Paint(sender, e);
60+
var scb = (SolidColorBrush)color;
61+
SetPenColor(scb.Color);
9062
}
9163

92-
private void WhiteboardCanvas_MouseUp(object sender, MouseButtonEventArgs e)
64+
public void SetPenThickness(double size)
9365
{
94-
StopPaint(sender, e);
66+
brushThickness = size;
67+
setPenAttributes(brushColor, size);
9568
}
9669

97-
private void WhiteboardCanvas_MouseLeave(object sender, MouseEventArgs e)
98-
{
99-
StopPaint(sender, null);
100-
}
101-
102-
private void WhiteboardCanvas_MouseEnter(object sender, MouseEventArgs e)
103-
{
104-
if (e.LeftButton == MouseButtonState.Pressed)
105-
{
106-
BeginPaint(sender, null);
107-
}
108-
}
109-
110-
111-
public void SetPenColor(System.Windows.Media.Brush color)
112-
{
113-
BrushColor = color;
114-
}
11570

11671
public void Undo()
11772
{
118-
if (this.Children.Count > 0)
73+
if (inkCanvas.Strokes.Count > 0)
11974
{
120-
this.Children.RemoveAt(this.Children.Count - 1);
75+
inkCanvas.Strokes.RemoveAt(inkCanvas.Strokes.Count - 1);
12176
}
12277
}
12378

12479
public void Clear()
12580
{
126-
this.Children.Clear();
81+
inkCanvas.Strokes.Clear();
12782
}
12883

12984
}

0 commit comments

Comments
 (0)