1
1
using System ;
2
+ using System . Windows . Controls ;
3
+ using System . Windows . Ink ;
2
4
using System . Windows . Input ;
5
+ using System . Windows . Media ;
3
6
using System . Windows . Shapes ;
4
7
using Brushes = System . Windows . Media . Brushes ;
5
8
using Canvas = System . Windows . Controls . Canvas ;
@@ -9,15 +12,9 @@ namespace Whiteboard
9
12
class WhiteboardCanvas : Canvas
10
13
{
11
14
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 ;
21
18
22
19
public WhiteboardCanvas ( )
23
20
{
@@ -26,104 +23,62 @@ public WhiteboardCanvas()
26
23
27
24
private void InitializeComponent ( )
28
25
{
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 ;
60
29
61
- this . Children . Add ( line ) ;
30
+ inkCanvas . UseCustomCursor = true ;
31
+ inkCanvas . Cursor = this . Cursor ;
62
32
63
- InitX = X ;
64
- InitY = Y ;
65
- }
33
+ this . Children . Add ( inkCanvas ) ;
66
34
}
67
35
68
- public void BeginPaint ( object sender , MouseButtonEventArgs e )
36
+ private void WhiteboardCanvas_SizeChanged ( object sender , System . Windows . SizeChangedEventArgs e )
69
37
{
70
- PaintOn = true ;
38
+ // make sure the ink canvas also changes
39
+ inkCanvas . Width = this . Width ;
40
+ inkCanvas . Height = this . Height ;
71
41
}
72
42
73
- public void StopPaint ( object sender , MouseButtonEventArgs e )
43
+ private void setPenAttributes ( Color color , double size )
74
44
{
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 ;
79
50
}
80
51
81
-
82
- private void WhiteboardCanvas_MouseDown ( object sender , MouseButtonEventArgs e )
52
+ public void SetPenColor ( Color color )
83
53
{
84
- BeginPaint ( sender , e ) ;
54
+ brushColor = color ;
55
+ setPenAttributes ( brushColor , brushThickness ) ;
85
56
}
86
57
87
- private void WhiteboardCanvas_MouseMove ( object sender , MouseEventArgs e )
58
+ public void SetPenColor ( Brush color )
88
59
{
89
- Paint ( sender , e ) ;
60
+ var scb = ( SolidColorBrush ) color ;
61
+ SetPenColor ( scb . Color ) ;
90
62
}
91
63
92
- private void WhiteboardCanvas_MouseUp ( object sender , MouseButtonEventArgs e )
64
+ public void SetPenThickness ( double size )
93
65
{
94
- StopPaint ( sender , e ) ;
66
+ brushThickness = size ;
67
+ setPenAttributes ( brushColor , size ) ;
95
68
}
96
69
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
- }
115
70
116
71
public void Undo ( )
117
72
{
118
- if ( this . Children . Count > 0 )
73
+ if ( inkCanvas . Strokes . Count > 0 )
119
74
{
120
- this . Children . RemoveAt ( this . Children . Count - 1 ) ;
75
+ inkCanvas . Strokes . RemoveAt ( inkCanvas . Strokes . Count - 1 ) ;
121
76
}
122
77
}
123
78
124
79
public void Clear ( )
125
80
{
126
- this . Children . Clear ( ) ;
81
+ inkCanvas . Strokes . Clear ( ) ;
127
82
}
128
83
129
84
}
0 commit comments