回答編集履歴
6
リンク切れ アーカイブもなし
answer
CHANGED
@@ -248,7 +248,6 @@
|
|
248
248
|

|
249
249
|
参考
|
250
250
|
[InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
|
251
|
-
[WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
|
252
251
|
|
253
252
|
---
|
254
253
|
|
5
見直しキャンペーン中
answer
CHANGED
@@ -1,266 +1,266 @@
|
|
1
|
-
> Rectangle、EllipseをInkCanvasEditingMode.EraseByPointで消せるようにしたいと考えているのですが可能なのでしょうか?
|
2
|
-
|
3
|
-
`EditingMode`を変えるだけなんですから、やってみればいいんでは?(10秒で試せると思うのですが)
|
4
|
-
|
5
|
-
出来たらスゲーなと思いつつ試してみましたが、案の定できませんでした。。。
|
6
|
-
`Children`に入れたものはあくまで背景?(というかInkの後ろ側に描画するだけ)であって、ストロークには含まれません。
|
7
|
-
[InkCanvas.Strokes プロパティ (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.inkcanvas.strokes?view=net-6.0)
|
8
|
-
|
9
|
-
|
10
|
-
> 別の方法でも構いませんので、なにかいい案がある方はぜひお力添えお願いします。
|
11
|
-
|
12
|
-
どうにかして`Stroke`を作るしかなさそうですね。
|
13
|
-
|
14
|
-
ググって出てきたのを参考にざっと作ってみました。
|
15
|
-
ペン・消しゴム・四角・楕円・選択・ジェスチャ(四角・楕円のみ)モードがあります。
|
16
|
-
```
|
17
|
-
<Window
|
18
|
-
x:Class="Questions372920.MainWindow"
|
19
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
-
xmlns:local="clr-namespace:Questions372920"
|
22
|
-
Width="800"
|
23
|
-
Height="450">
|
24
|
-
<DockPanel>
|
25
|
-
<UniformGrid HorizontalAlignment="Left" DockPanel.Dock="Top" Rows="1">
|
26
|
-
<UniformGrid.Resources>
|
27
|
-
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton">
|
28
|
-
<EventSetter Event="Checked" Handler="RadioButton_Checked" />
|
29
|
-
</Style>
|
30
|
-
</UniformGrid.Resources>
|
31
|
-
<RadioButton Content="{x:Static local:MyInkMode.Pen}" IsChecked="True" />
|
32
|
-
<RadioButton Content="{x:Static local:MyInkMode.Rect}" />
|
33
|
-
<RadioButton Content="{x:Static local:MyInkMode.Ellipse}" />
|
34
|
-
<RadioButton Content="{x:Static local:MyInkMode.Gesture}" />
|
35
|
-
<RadioButton Content="{x:Static local:MyInkMode.Select}" />
|
36
|
-
<RadioButton Content="{x:Static local:MyInkMode.Erase}" />
|
37
|
-
</UniformGrid>
|
38
|
-
|
39
|
-
<Grid>
|
40
|
-
<InkCanvas
|
41
|
-
Name="inkCanvas"
|
42
|
-
Background="WhiteSmoke"
|
43
|
-
EditingMode="Ink"
|
44
|
-
Gesture="InkCanvas_Gesture"
|
45
|
-
MouseLeftButtonDown="InkCanvas_MouseLeftButtonDown"
|
46
|
-
MouseLeftButtonUp="InkCanvas_MouseLeftButtonUp"
|
47
|
-
MouseMove="InkCanvas_MouseMove">
|
48
|
-
<Rectangle
|
49
|
-
Width="50"
|
50
|
-
Height="50"
|
51
|
-
InkCanvas.Left="50"
|
52
|
-
InkCanvas.Top="50"
|
53
|
-
Stroke="Red" />
|
54
|
-
<Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
|
55
|
-
<TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
|
56
|
-
</InkCanvas>
|
57
|
-
|
58
|
-
<!-- 四角・楕円ツール使用中の仮表示 本来はAdornerなんかでやるべきだが本題でないので手抜き -->
|
59
|
-
<Canvas Background="{x:Null}" ClipToBounds="True">
|
60
|
-
<Ellipse
|
61
|
-
x:Name="tmpEllipse"
|
62
|
-
Fill="Red"
|
63
|
-
Stroke="Black"
|
64
|
-
StrokeThickness="2" />
|
65
|
-
<Rectangle
|
66
|
-
x:Name="tmpRect"
|
67
|
-
Fill="Red"
|
68
|
-
Stroke="Black"
|
69
|
-
StrokeThickness="2" />
|
70
|
-
</Canvas>
|
71
|
-
</Grid>
|
72
|
-
</DockPanel>
|
73
|
-
</Window>
|
74
|
-
```
|
75
|
-
|
76
|
-
```
|
77
|
-
using System;
|
78
|
-
using System.Collections.Generic;
|
79
|
-
using System.Windows;
|
80
|
-
using System.Windows.Controls;
|
81
|
-
using System.Windows.Ink;
|
82
|
-
using System.Windows.Input;
|
83
|
-
using System.Windows.Media;
|
84
|
-
using System.Windows.Shapes;
|
85
|
-
|
86
|
-
|
87
|
-
namespace Questions372920
|
88
|
-
{
|
89
|
-
public enum MyInkMode { Pen, Rect, Ellipse, Gesture, Select, Erase, };
|
90
|
-
|
91
|
-
public partial class MainWindow : Window
|
92
|
-
{
|
93
|
-
private Point startPoint;
|
94
|
-
private bool isDragging;
|
95
|
-
private MyInkMode mode;
|
96
|
-
|
97
|
-
public MainWindow() => InitializeComponent();
|
98
|
-
|
99
|
-
private void RadioButton_Checked(object sender, RoutedEventArgs e)
|
100
|
-
{
|
101
|
-
if (sender is RadioButton rb && rb.Content is MyInkMode m)
|
102
|
-
{
|
103
|
-
mode = m;
|
104
|
-
if (inkCanvas == null) return;
|
105
|
-
|
106
|
-
switch (mode)
|
107
|
-
{
|
108
|
-
case MyInkMode.Pen: inkCanvas.EditingMode = InkCanvasEditingMode.Ink; break;
|
109
|
-
case MyInkMode.Rect:
|
110
|
-
case MyInkMode.Ellipse:
|
111
|
-
inkCanvas.EditingMode = InkCanvasEditingMode.None;
|
112
|
-
break;
|
113
|
-
case MyInkMode.Gesture: inkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; break;
|
114
|
-
case MyInkMode.Select: inkCanvas.EditingMode = InkCanvasEditingMode.Select; break;
|
115
|
-
case MyInkMode.Erase: inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break;
|
116
|
-
}
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
private void InkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
121
|
-
{
|
122
|
-
if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
|
123
|
-
|
124
|
-
isDragging = true;
|
125
|
-
startPoint = e.GetPosition(inkCanvas);
|
126
|
-
inkCanvas.CaptureMouse();
|
127
|
-
}
|
128
|
-
|
129
|
-
private void InkCanvas_MouseMove(object sender, MouseEventArgs e)
|
130
|
-
{
|
131
|
-
if (!isDragging) return;
|
132
|
-
|
133
|
-
Shape shape;
|
134
|
-
switch (mode)
|
135
|
-
{
|
136
|
-
case MyInkMode.Rect: shape = tmpRect; break;
|
137
|
-
case MyInkMode.Ellipse: shape = tmpEllipse; break;
|
138
|
-
default: return;
|
139
|
-
}
|
140
|
-
|
141
|
-
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
142
|
-
r.Inflate(1, 1);
|
143
|
-
|
144
|
-
Canvas.SetLeft(shape, r.X);
|
145
|
-
Canvas.SetTop(shape, r.Y);
|
146
|
-
shape.Width = r.Width;
|
147
|
-
shape.Height = r.Height;
|
148
|
-
}
|
149
|
-
|
150
|
-
private void InkCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
151
|
-
{
|
152
|
-
if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
|
153
|
-
|
154
|
-
inkCanvas.ReleaseMouseCapture();
|
155
|
-
isDragging = false;
|
156
|
-
|
157
|
-
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
158
|
-
switch (mode)
|
159
|
-
{
|
160
|
-
case MyInkMode.Rect:
|
161
|
-
AddRect(r.TopLeft, r.BottomRight);
|
162
|
-
tmpRect.Width = tmpRect.Height = 0;
|
163
|
-
break;
|
164
|
-
case MyInkMode.Ellipse:
|
165
|
-
AddEllipse(r.TopLeft, r.BottomRight);
|
166
|
-
tmpEllipse.Width = tmpEllipse.Height = 0;
|
167
|
-
break;
|
168
|
-
default: return;
|
169
|
-
}
|
170
|
-
}
|
171
|
-
|
172
|
-
private void InkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e)
|
173
|
-
{
|
174
|
-
var result = e.GetGestureRecognitionResults()[0];
|
175
|
-
if (result.RecognitionConfidence != RecognitionConfidence.Strong) return;
|
176
|
-
|
177
|
-
var r = e.Strokes.GetBounds();
|
178
|
-
switch (result.ApplicationGesture)
|
179
|
-
{
|
180
|
-
case ApplicationGesture.Square: AddRect(r.TopLeft, r.BottomRight); break;
|
181
|
-
case ApplicationGesture.Circle: AddEllipse(r.TopLeft, r.BottomRight); break;
|
182
|
-
}
|
183
|
-
}
|
184
|
-
|
185
|
-
private void AddRect(Point start, Point end)
|
186
|
-
{
|
187
|
-
var n = new List<Point>();
|
188
|
-
for (var i = start.Y; i < end.Y; i += 2) // えらい雑w
|
189
|
-
{
|
190
|
-
n.Add(new Point(start.X, i));
|
191
|
-
n.Add(new Point(end.X, i));
|
192
|
-
}
|
193
|
-
if (0 < n.Count)
|
194
|
-
{
|
195
|
-
var s = new Stroke(new StylusPointCollection(n));
|
196
|
-
s.DrawingAttributes.Color = Colors.Red;
|
197
|
-
inkCanvas.Strokes.Add(s);
|
198
|
-
}
|
199
|
-
|
200
|
-
var points = new Point[]
|
201
|
-
{
|
202
|
-
new Point(start.X, start.Y),
|
203
|
-
new Point(start.X, end.Y),
|
204
|
-
new Point(end.X, end.Y),
|
205
|
-
new Point(end.X, start.Y),
|
206
|
-
new Point(start.X, start.Y),
|
207
|
-
};
|
208
|
-
|
209
|
-
var stroke = new Stroke(new StylusPointCollection(points));
|
210
|
-
inkCanvas.Strokes.Add(stroke);
|
211
|
-
}
|
212
|
-
|
213
|
-
private void AddEllipse(Point start, Point end)
|
214
|
-
{
|
215
|
-
var a = (end.X - start.X) / 2;
|
216
|
-
var b = (end.Y - start.Y) / 2;
|
217
|
-
var offset = (Vector)((end - start) / 2 + start);
|
218
|
-
|
219
|
-
var n = new List<Point>();
|
220
|
-
for (var x = -a; x < a; x += 2) // さらに雑w
|
221
|
-
{
|
222
|
-
var y = b * Math.Sqrt(Math.Pow(a, 2) - Math.Pow(x, 2)) / a;
|
223
|
-
n.Add(new Point(x, -y) + offset);
|
224
|
-
n.Add(new Point(x, y) + offset);
|
225
|
-
}
|
226
|
-
if (0 < n.Count)
|
227
|
-
{
|
228
|
-
var s = new Stroke(new StylusPointCollection(n));
|
229
|
-
s.DrawingAttributes.Color = Colors.Red;
|
230
|
-
inkCanvas.Strokes.Add(s);
|
231
|
-
}
|
232
|
-
|
233
|
-
var points = new List<Point>();
|
234
|
-
for (var i = 0; i <= 360; i += 5)
|
235
|
-
{
|
236
|
-
var x = a * Math.Cos(i / 180d * Math.PI);
|
237
|
-
var y = b * Math.Sin(i / 180d * Math.PI);
|
238
|
-
points.Add(new Point(x, y) + offset);
|
239
|
-
}
|
240
|
-
|
241
|
-
var stroke = new Stroke(new StylusPointCollection(points));
|
242
|
-
stroke.DrawingAttributes.FitToCurve = true;
|
243
|
-
inkCanvas.Strokes.Add(stroke);
|
244
|
-
}
|
245
|
-
}
|
246
|
-
}
|
247
|
-
```
|
248
|
-

|
249
|
-
参考
|
250
|
-
[InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
|
251
|
-
[WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
|
252
|
-
|
253
|
-
---
|
254
|
-
|
255
|
-
> C# wpfにてペイントソフトを作ろうと思っています。
|
256
|
-
|
257
|
-
`InkCanvas`は手書き・ジェスチャ認識等すごい機能もあるんですが、あくまでペン用であって例えば塗りつぶし機能はありません。
|
258
|
-
ペイントソフトに向いているかというと微妙な気もします。
|
259
|
-
|
260
|
-
「じゃあどうすりゃいいの?」ってことですが、わたしはこの手のものは全く興味がないので他の方を参考にしてください^^;
|
261
|
-
[Search · wpf paint application](https://github.com/search?q=wpf+paint+application&type=Repositories)
|
262
|
-
|
263
|
-
---
|
264
|
-
|
265
|
-
コメントで言及したChildrenに入れたコントロールが、(何もしなくても)移動・リサイズできちゃうの図
|
1
|
+
> Rectangle、EllipseをInkCanvasEditingMode.EraseByPointで消せるようにしたいと考えているのですが可能なのでしょうか?
|
2
|
+
|
3
|
+
`EditingMode`を変えるだけなんですから、やってみればいいんでは?(10秒で試せると思うのですが)
|
4
|
+
|
5
|
+
出来たらスゲーなと思いつつ試してみましたが、案の定できませんでした。。。
|
6
|
+
`Children`に入れたものはあくまで背景?(というかInkの後ろ側に描画するだけ)であって、ストロークには含まれません。
|
7
|
+
[InkCanvas.Strokes プロパティ (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.inkcanvas.strokes?view=net-6.0)
|
8
|
+
|
9
|
+
|
10
|
+
> 別の方法でも構いませんので、なにかいい案がある方はぜひお力添えお願いします。
|
11
|
+
|
12
|
+
どうにかして`Stroke`を作るしかなさそうですね。
|
13
|
+
|
14
|
+
ググって出てきたのを参考にざっと作ってみました。
|
15
|
+
ペン・消しゴム・四角・楕円・選択・ジェスチャ(四角・楕円のみ)モードがあります。
|
16
|
+
```xml
|
17
|
+
<Window
|
18
|
+
x:Class="Questions372920.MainWindow"
|
19
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
+
xmlns:local="clr-namespace:Questions372920"
|
22
|
+
Width="800"
|
23
|
+
Height="450">
|
24
|
+
<DockPanel>
|
25
|
+
<UniformGrid HorizontalAlignment="Left" DockPanel.Dock="Top" Rows="1">
|
26
|
+
<UniformGrid.Resources>
|
27
|
+
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton">
|
28
|
+
<EventSetter Event="Checked" Handler="RadioButton_Checked" />
|
29
|
+
</Style>
|
30
|
+
</UniformGrid.Resources>
|
31
|
+
<RadioButton Content="{x:Static local:MyInkMode.Pen}" IsChecked="True" />
|
32
|
+
<RadioButton Content="{x:Static local:MyInkMode.Rect}" />
|
33
|
+
<RadioButton Content="{x:Static local:MyInkMode.Ellipse}" />
|
34
|
+
<RadioButton Content="{x:Static local:MyInkMode.Gesture}" />
|
35
|
+
<RadioButton Content="{x:Static local:MyInkMode.Select}" />
|
36
|
+
<RadioButton Content="{x:Static local:MyInkMode.Erase}" />
|
37
|
+
</UniformGrid>
|
38
|
+
|
39
|
+
<Grid>
|
40
|
+
<InkCanvas
|
41
|
+
Name="inkCanvas"
|
42
|
+
Background="WhiteSmoke"
|
43
|
+
EditingMode="Ink"
|
44
|
+
Gesture="InkCanvas_Gesture"
|
45
|
+
MouseLeftButtonDown="InkCanvas_MouseLeftButtonDown"
|
46
|
+
MouseLeftButtonUp="InkCanvas_MouseLeftButtonUp"
|
47
|
+
MouseMove="InkCanvas_MouseMove">
|
48
|
+
<Rectangle
|
49
|
+
Width="50"
|
50
|
+
Height="50"
|
51
|
+
InkCanvas.Left="50"
|
52
|
+
InkCanvas.Top="50"
|
53
|
+
Stroke="Red" />
|
54
|
+
<Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
|
55
|
+
<TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
|
56
|
+
</InkCanvas>
|
57
|
+
|
58
|
+
<!-- 四角・楕円ツール使用中の仮表示 本来はAdornerなんかでやるべきだが本題でないので手抜き -->
|
59
|
+
<Canvas Background="{x:Null}" ClipToBounds="True">
|
60
|
+
<Ellipse
|
61
|
+
x:Name="tmpEllipse"
|
62
|
+
Fill="Red"
|
63
|
+
Stroke="Black"
|
64
|
+
StrokeThickness="2" />
|
65
|
+
<Rectangle
|
66
|
+
x:Name="tmpRect"
|
67
|
+
Fill="Red"
|
68
|
+
Stroke="Black"
|
69
|
+
StrokeThickness="2" />
|
70
|
+
</Canvas>
|
71
|
+
</Grid>
|
72
|
+
</DockPanel>
|
73
|
+
</Window>
|
74
|
+
```
|
75
|
+
|
76
|
+
```cs
|
77
|
+
using System;
|
78
|
+
using System.Collections.Generic;
|
79
|
+
using System.Windows;
|
80
|
+
using System.Windows.Controls;
|
81
|
+
using System.Windows.Ink;
|
82
|
+
using System.Windows.Input;
|
83
|
+
using System.Windows.Media;
|
84
|
+
using System.Windows.Shapes;
|
85
|
+
|
86
|
+
|
87
|
+
namespace Questions372920
|
88
|
+
{
|
89
|
+
public enum MyInkMode { Pen, Rect, Ellipse, Gesture, Select, Erase, };
|
90
|
+
|
91
|
+
public partial class MainWindow : Window
|
92
|
+
{
|
93
|
+
private Point startPoint;
|
94
|
+
private bool isDragging;
|
95
|
+
private MyInkMode mode;
|
96
|
+
|
97
|
+
public MainWindow() => InitializeComponent();
|
98
|
+
|
99
|
+
private void RadioButton_Checked(object sender, RoutedEventArgs e)
|
100
|
+
{
|
101
|
+
if (sender is RadioButton rb && rb.Content is MyInkMode m)
|
102
|
+
{
|
103
|
+
mode = m;
|
104
|
+
if (inkCanvas == null) return;
|
105
|
+
|
106
|
+
switch (mode)
|
107
|
+
{
|
108
|
+
case MyInkMode.Pen: inkCanvas.EditingMode = InkCanvasEditingMode.Ink; break;
|
109
|
+
case MyInkMode.Rect:
|
110
|
+
case MyInkMode.Ellipse:
|
111
|
+
inkCanvas.EditingMode = InkCanvasEditingMode.None;
|
112
|
+
break;
|
113
|
+
case MyInkMode.Gesture: inkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; break;
|
114
|
+
case MyInkMode.Select: inkCanvas.EditingMode = InkCanvasEditingMode.Select; break;
|
115
|
+
case MyInkMode.Erase: inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
private void InkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
121
|
+
{
|
122
|
+
if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
|
123
|
+
|
124
|
+
isDragging = true;
|
125
|
+
startPoint = e.GetPosition(inkCanvas);
|
126
|
+
inkCanvas.CaptureMouse();
|
127
|
+
}
|
128
|
+
|
129
|
+
private void InkCanvas_MouseMove(object sender, MouseEventArgs e)
|
130
|
+
{
|
131
|
+
if (!isDragging) return;
|
132
|
+
|
133
|
+
Shape shape;
|
134
|
+
switch (mode)
|
135
|
+
{
|
136
|
+
case MyInkMode.Rect: shape = tmpRect; break;
|
137
|
+
case MyInkMode.Ellipse: shape = tmpEllipse; break;
|
138
|
+
default: return;
|
139
|
+
}
|
140
|
+
|
141
|
+
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
142
|
+
r.Inflate(1, 1);
|
143
|
+
|
144
|
+
Canvas.SetLeft(shape, r.X);
|
145
|
+
Canvas.SetTop(shape, r.Y);
|
146
|
+
shape.Width = r.Width;
|
147
|
+
shape.Height = r.Height;
|
148
|
+
}
|
149
|
+
|
150
|
+
private void InkCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
151
|
+
{
|
152
|
+
if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
|
153
|
+
|
154
|
+
inkCanvas.ReleaseMouseCapture();
|
155
|
+
isDragging = false;
|
156
|
+
|
157
|
+
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
158
|
+
switch (mode)
|
159
|
+
{
|
160
|
+
case MyInkMode.Rect:
|
161
|
+
AddRect(r.TopLeft, r.BottomRight);
|
162
|
+
tmpRect.Width = tmpRect.Height = 0;
|
163
|
+
break;
|
164
|
+
case MyInkMode.Ellipse:
|
165
|
+
AddEllipse(r.TopLeft, r.BottomRight);
|
166
|
+
tmpEllipse.Width = tmpEllipse.Height = 0;
|
167
|
+
break;
|
168
|
+
default: return;
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
private void InkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e)
|
173
|
+
{
|
174
|
+
var result = e.GetGestureRecognitionResults()[0];
|
175
|
+
if (result.RecognitionConfidence != RecognitionConfidence.Strong) return;
|
176
|
+
|
177
|
+
var r = e.Strokes.GetBounds();
|
178
|
+
switch (result.ApplicationGesture)
|
179
|
+
{
|
180
|
+
case ApplicationGesture.Square: AddRect(r.TopLeft, r.BottomRight); break;
|
181
|
+
case ApplicationGesture.Circle: AddEllipse(r.TopLeft, r.BottomRight); break;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
private void AddRect(Point start, Point end)
|
186
|
+
{
|
187
|
+
var n = new List<Point>();
|
188
|
+
for (var i = start.Y; i < end.Y; i += 2) // えらい雑w
|
189
|
+
{
|
190
|
+
n.Add(new Point(start.X, i));
|
191
|
+
n.Add(new Point(end.X, i));
|
192
|
+
}
|
193
|
+
if (0 < n.Count)
|
194
|
+
{
|
195
|
+
var s = new Stroke(new StylusPointCollection(n));
|
196
|
+
s.DrawingAttributes.Color = Colors.Red;
|
197
|
+
inkCanvas.Strokes.Add(s);
|
198
|
+
}
|
199
|
+
|
200
|
+
var points = new Point[]
|
201
|
+
{
|
202
|
+
new Point(start.X, start.Y),
|
203
|
+
new Point(start.X, end.Y),
|
204
|
+
new Point(end.X, end.Y),
|
205
|
+
new Point(end.X, start.Y),
|
206
|
+
new Point(start.X, start.Y),
|
207
|
+
};
|
208
|
+
|
209
|
+
var stroke = new Stroke(new StylusPointCollection(points));
|
210
|
+
inkCanvas.Strokes.Add(stroke);
|
211
|
+
}
|
212
|
+
|
213
|
+
private void AddEllipse(Point start, Point end)
|
214
|
+
{
|
215
|
+
var a = (end.X - start.X) / 2;
|
216
|
+
var b = (end.Y - start.Y) / 2;
|
217
|
+
var offset = (Vector)((end - start) / 2 + start);
|
218
|
+
|
219
|
+
var n = new List<Point>();
|
220
|
+
for (var x = -a; x < a; x += 2) // さらに雑w
|
221
|
+
{
|
222
|
+
var y = b * Math.Sqrt(Math.Pow(a, 2) - Math.Pow(x, 2)) / a;
|
223
|
+
n.Add(new Point(x, -y) + offset);
|
224
|
+
n.Add(new Point(x, y) + offset);
|
225
|
+
}
|
226
|
+
if (0 < n.Count)
|
227
|
+
{
|
228
|
+
var s = new Stroke(new StylusPointCollection(n));
|
229
|
+
s.DrawingAttributes.Color = Colors.Red;
|
230
|
+
inkCanvas.Strokes.Add(s);
|
231
|
+
}
|
232
|
+
|
233
|
+
var points = new List<Point>();
|
234
|
+
for (var i = 0; i <= 360; i += 5)
|
235
|
+
{
|
236
|
+
var x = a * Math.Cos(i / 180d * Math.PI);
|
237
|
+
var y = b * Math.Sin(i / 180d * Math.PI);
|
238
|
+
points.Add(new Point(x, y) + offset);
|
239
|
+
}
|
240
|
+
|
241
|
+
var stroke = new Stroke(new StylusPointCollection(points));
|
242
|
+
stroke.DrawingAttributes.FitToCurve = true;
|
243
|
+
inkCanvas.Strokes.Add(stroke);
|
244
|
+
}
|
245
|
+
}
|
246
|
+
}
|
247
|
+
```
|
248
|
+

|
249
|
+
参考
|
250
|
+
[InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
|
251
|
+
[WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
|
252
|
+
|
253
|
+
---
|
254
|
+
|
255
|
+
> C# wpfにてペイントソフトを作ろうと思っています。
|
256
|
+
|
257
|
+
`InkCanvas`は手書き・ジェスチャ認識等すごい機能もあるんですが、あくまでペン用であって例えば塗りつぶし機能はありません。
|
258
|
+
ペイントソフトに向いているかというと微妙な気もします。
|
259
|
+
|
260
|
+
「じゃあどうすりゃいいの?」ってことですが、わたしはこの手のものは全く興味がないので他の方を参考にしてください^^;
|
261
|
+
[Search · wpf paint application](https://github.com/search?q=wpf+paint+application&type=Repositories)
|
262
|
+
|
263
|
+
---
|
264
|
+
|
265
|
+
コメントで言及したChildrenに入れたコントロールが、(何もしなくても)移動・リサイズできちゃうの図
|
266
266
|

|
4
楕円対応
answer
CHANGED
@@ -22,38 +22,53 @@
|
|
22
22
|
Width="800"
|
23
23
|
Height="450">
|
24
24
|
<DockPanel>
|
25
|
-
<
|
25
|
+
<UniformGrid HorizontalAlignment="Left" DockPanel.Dock="Top" Rows="1">
|
26
|
+
<UniformGrid.Resources>
|
26
|
-
|
27
|
+
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton">
|
27
|
-
<x:Static Member="local:MyInkMode.Pen" />
|
28
|
-
<x:Static Member="local:MyInkMode.Rect" />
|
29
|
-
<x:Static Member="local:MyInkMode.Ellipse" />
|
30
|
-
|
28
|
+
<EventSetter Event="Checked" Handler="RadioButton_Checked" />
|
31
|
-
<x:Static Member="local:MyInkMode.Select" />
|
32
|
-
<x:Static Member="local:MyInkMode.Erase" />
|
33
|
-
</ComboBox>
|
34
|
-
|
29
|
+
</Style>
|
30
|
+
</UniformGrid.Resources>
|
31
|
+
<RadioButton Content="{x:Static local:MyInkMode.Pen}" IsChecked="True" />
|
32
|
+
<RadioButton Content="{x:Static local:MyInkMode.Rect}" />
|
33
|
+
<RadioButton Content="{x:Static local:MyInkMode.Ellipse}" />
|
34
|
+
<RadioButton Content="{x:Static local:MyInkMode.Gesture}" />
|
35
|
+
<RadioButton Content="{x:Static local:MyInkMode.Select}" />
|
36
|
+
<RadioButton Content="{x:Static local:MyInkMode.Erase}" />
|
37
|
+
</UniformGrid>
|
35
38
|
|
39
|
+
<Grid>
|
36
|
-
|
40
|
+
<InkCanvas
|
37
|
-
|
41
|
+
Name="inkCanvas"
|
38
|
-
|
42
|
+
Background="WhiteSmoke"
|
39
|
-
|
43
|
+
EditingMode="Ink"
|
40
|
-
|
44
|
+
Gesture="InkCanvas_Gesture"
|
41
|
-
|
45
|
+
MouseLeftButtonDown="InkCanvas_MouseLeftButtonDown"
|
42
|
-
|
46
|
+
MouseLeftButtonUp="InkCanvas_MouseLeftButtonUp"
|
43
|
-
|
47
|
+
MouseMove="InkCanvas_MouseMove">
|
48
|
+
<Rectangle
|
49
|
+
Width="50"
|
50
|
+
Height="50"
|
51
|
+
InkCanvas.Left="50"
|
52
|
+
InkCanvas.Top="50"
|
53
|
+
Stroke="Red" />
|
54
|
+
<Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
|
55
|
+
<TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
|
56
|
+
</InkCanvas>
|
44
57
|
|
58
|
+
<!-- 四角・楕円ツール使用中の仮表示 本来はAdornerなんかでやるべきだが本題でないので手抜き -->
|
59
|
+
<Canvas Background="{x:Null}" ClipToBounds="True">
|
60
|
+
<Ellipse
|
61
|
+
x:Name="tmpEllipse"
|
62
|
+
Fill="Red"
|
63
|
+
Stroke="Black"
|
64
|
+
StrokeThickness="2" />
|
45
|
-
|
65
|
+
<Rectangle
|
66
|
+
x:Name="tmpRect"
|
46
|
-
|
67
|
+
Fill="Red"
|
47
|
-
|
68
|
+
Stroke="Black"
|
48
|
-
InkCanvas.Left="50"
|
49
|
-
InkCanvas.Top="50"
|
50
|
-
|
69
|
+
StrokeThickness="2" />
|
51
|
-
<Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
|
52
|
-
<TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
|
53
|
-
|
54
|
-
<Ellipse x:Name="elli" Stroke="Black" StrokeThickness="2" />
|
55
|
-
<Rectangle x:Name="rect" Stroke="Black" StrokeThickness="2" />
|
56
|
-
|
70
|
+
</Canvas>
|
71
|
+
</Grid>
|
57
72
|
</DockPanel>
|
58
73
|
</Window>
|
59
74
|
```
|
@@ -81,21 +96,24 @@
|
|
81
96
|
|
82
97
|
public MainWindow() => InitializeComponent();
|
83
98
|
|
84
|
-
private void
|
99
|
+
private void RadioButton_Checked(object sender, RoutedEventArgs e)
|
85
100
|
{
|
86
|
-
|
101
|
+
if (sender is RadioButton rb && rb.Content is MyInkMode m)
|
102
|
+
{
|
103
|
+
mode = m;
|
87
|
-
|
104
|
+
if (inkCanvas == null) return;
|
88
105
|
|
89
|
-
|
106
|
+
switch (mode)
|
90
|
-
|
107
|
+
{
|
91
|
-
|
108
|
+
case MyInkMode.Pen: inkCanvas.EditingMode = InkCanvasEditingMode.Ink; break;
|
92
|
-
|
109
|
+
case MyInkMode.Rect:
|
93
|
-
|
110
|
+
case MyInkMode.Ellipse:
|
94
|
-
|
111
|
+
inkCanvas.EditingMode = InkCanvasEditingMode.None;
|
95
|
-
|
112
|
+
break;
|
96
|
-
|
113
|
+
case MyInkMode.Gesture: inkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; break;
|
97
|
-
|
114
|
+
case MyInkMode.Select: inkCanvas.EditingMode = InkCanvasEditingMode.Select; break;
|
98
|
-
|
115
|
+
case MyInkMode.Erase: inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break;
|
116
|
+
}
|
99
117
|
}
|
100
118
|
}
|
101
119
|
|
@@ -105,6 +123,7 @@
|
|
105
123
|
|
106
124
|
isDragging = true;
|
107
125
|
startPoint = e.GetPosition(inkCanvas);
|
126
|
+
inkCanvas.CaptureMouse();
|
108
127
|
}
|
109
128
|
|
110
129
|
private void InkCanvas_MouseMove(object sender, MouseEventArgs e)
|
@@ -114,16 +133,16 @@
|
|
114
133
|
Shape shape;
|
115
134
|
switch (mode)
|
116
135
|
{
|
117
|
-
case MyInkMode.Rect: shape =
|
136
|
+
case MyInkMode.Rect: shape = tmpRect; break;
|
118
|
-
case MyInkMode.Ellipse: shape =
|
137
|
+
case MyInkMode.Ellipse: shape = tmpEllipse; break;
|
119
138
|
default: return;
|
120
139
|
}
|
121
140
|
|
122
141
|
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
123
142
|
r.Inflate(1, 1);
|
124
143
|
|
125
|
-
|
144
|
+
Canvas.SetLeft(shape, r.X);
|
126
|
-
|
145
|
+
Canvas.SetTop(shape, r.Y);
|
127
146
|
shape.Width = r.Width;
|
128
147
|
shape.Height = r.Height;
|
129
148
|
}
|
@@ -132,18 +151,19 @@
|
|
132
151
|
{
|
133
152
|
if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
|
134
153
|
|
154
|
+
inkCanvas.ReleaseMouseCapture();
|
135
155
|
isDragging = false;
|
136
|
-
|
156
|
+
|
137
157
|
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
138
158
|
switch (mode)
|
139
159
|
{
|
140
160
|
case MyInkMode.Rect:
|
141
161
|
AddRect(r.TopLeft, r.BottomRight);
|
142
|
-
|
162
|
+
tmpRect.Width = tmpRect.Height = 0;
|
143
163
|
break;
|
144
164
|
case MyInkMode.Ellipse:
|
145
165
|
AddEllipse(r.TopLeft, r.BottomRight);
|
146
|
-
|
166
|
+
tmpEllipse.Width = tmpEllipse.Height = 0;
|
147
167
|
break;
|
148
168
|
default: return;
|
149
169
|
}
|
@@ -170,9 +190,12 @@
|
|
170
190
|
n.Add(new Point(start.X, i));
|
171
191
|
n.Add(new Point(end.X, i));
|
172
192
|
}
|
193
|
+
if (0 < n.Count)
|
194
|
+
{
|
173
|
-
|
195
|
+
var s = new Stroke(new StylusPointCollection(n));
|
174
|
-
|
196
|
+
s.DrawingAttributes.Color = Colors.Red;
|
175
|
-
|
197
|
+
inkCanvas.Strokes.Add(s);
|
198
|
+
}
|
176
199
|
|
177
200
|
var points = new Point[]
|
178
201
|
{
|
@@ -187,27 +210,32 @@
|
|
187
210
|
inkCanvas.Strokes.Add(stroke);
|
188
211
|
}
|
189
212
|
|
190
|
-
// もっと短くできそうですが、こういう計算が苦手なので^^;
|
191
|
-
// 元コードよりは大幅にPointが減ってはいます
|
192
213
|
private void AddEllipse(Point start, Point end)
|
193
214
|
{
|
194
|
-
var a =
|
215
|
+
var a = (end.X - start.X) / 2;
|
195
|
-
var b =
|
216
|
+
var b = (end.Y - start.Y) / 2;
|
196
|
-
var
|
217
|
+
var offset = (Vector)((end - start) / 2 + start);
|
197
|
-
var d = 0.5 * (end.Y + start.Y);
|
198
218
|
|
199
|
-
|
219
|
+
var n = new List<Point>();
|
200
|
-
//
|
220
|
+
for (var x = -a; x < a; x += 2) // さらに雑w
|
221
|
+
{
|
222
|
+
var y = b * Math.Sqrt(Math.Pow(a, 2) - Math.Pow(x, 2)) / a;
|
223
|
+
n.Add(new Point(x, -y) + offset);
|
224
|
+
n.Add(new Point(x, y) + offset);
|
225
|
+
}
|
226
|
+
if (0 < n.Count)
|
227
|
+
{
|
201
|
-
|
228
|
+
var s = new Stroke(new StylusPointCollection(n));
|
202
|
-
|
229
|
+
s.DrawingAttributes.Color = Colors.Red;
|
203
|
-
|
230
|
+
inkCanvas.Strokes.Add(s);
|
231
|
+
}
|
204
232
|
|
205
233
|
var points = new List<Point>();
|
206
234
|
for (var i = 0; i <= 360; i += 5)
|
207
235
|
{
|
208
|
-
var x =
|
236
|
+
var x = a * Math.Cos(i / 180d * Math.PI);
|
209
|
-
var y =
|
237
|
+
var y = b * Math.Sin(i / 180d * Math.PI);
|
210
|
-
points.Add(new Point(x, y));
|
238
|
+
points.Add(new Point(x, y) + offset);
|
211
239
|
}
|
212
240
|
|
213
241
|
var stroke = new Stroke(new StylusPointCollection(points));
|
@@ -217,7 +245,7 @@
|
|
217
245
|
}
|
218
246
|
}
|
219
247
|
```
|
220
|
-

|
221
249
|
参考
|
222
250
|
[InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
|
223
251
|
[WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
|
3
超雑に塗りつぶし
answer
CHANGED
@@ -65,8 +65,10 @@
|
|
65
65
|
using System.Windows.Controls;
|
66
66
|
using System.Windows.Ink;
|
67
67
|
using System.Windows.Input;
|
68
|
+
using System.Windows.Media;
|
68
69
|
using System.Windows.Shapes;
|
69
70
|
|
71
|
+
|
70
72
|
namespace Questions372920
|
71
73
|
{
|
72
74
|
public enum MyInkMode { Pen, Rect, Ellipse, Gesture, Select, Erase, };
|
@@ -131,15 +133,16 @@
|
|
131
133
|
if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
|
132
134
|
|
133
135
|
isDragging = false;
|
136
|
+
|
134
|
-
|
137
|
+
var r = new Rect(startPoint, e.GetPosition(inkCanvas));
|
135
138
|
switch (mode)
|
136
139
|
{
|
137
140
|
case MyInkMode.Rect:
|
138
|
-
AddRect(
|
141
|
+
AddRect(r.TopLeft, r.BottomRight);
|
139
142
|
rect.Width = rect.Height = 0;
|
140
143
|
break;
|
141
144
|
case MyInkMode.Ellipse:
|
142
|
-
AddEllipse(
|
145
|
+
AddEllipse(r.TopLeft, r.BottomRight);
|
143
146
|
elli.Width = elli.Height = 0;
|
144
147
|
break;
|
145
148
|
default: return;
|
@@ -161,6 +164,16 @@
|
|
161
164
|
|
162
165
|
private void AddRect(Point start, Point end)
|
163
166
|
{
|
167
|
+
var n = new List<Point>();
|
168
|
+
for (var i = start.Y; i < end.Y; i += 2) // えらい雑w
|
169
|
+
{
|
170
|
+
n.Add(new Point(start.X, i));
|
171
|
+
n.Add(new Point(end.X, i));
|
172
|
+
}
|
173
|
+
var s = new Stroke(new StylusPointCollection(n));
|
174
|
+
s.DrawingAttributes.Color = Colors.Red;
|
175
|
+
inkCanvas.Strokes.Add(s);
|
176
|
+
|
164
177
|
var points = new Point[]
|
165
178
|
{
|
166
179
|
new Point(start.X, start.Y),
|
@@ -183,6 +196,12 @@
|
|
183
196
|
var c = 0.5 * (end.X + start.X);
|
184
197
|
var d = 0.5 * (end.Y + start.Y);
|
185
198
|
|
199
|
+
//var n = new List<Point>();
|
200
|
+
//// ここの計算が雑にすらできないw
|
201
|
+
//var s = new Stroke(new StylusPointCollection(n));
|
202
|
+
//s.DrawingAttributes.Color = Colors.Red;
|
203
|
+
//inkCanvas.Strokes.Add(s);
|
204
|
+
|
186
205
|
var points = new List<Point>();
|
187
206
|
for (var i = 0; i <= 360; i += 5)
|
188
207
|
{
|
@@ -198,7 +217,7 @@
|
|
198
217
|
}
|
199
218
|
}
|
200
219
|
```
|
201
|
-

|
202
221
|
参考
|
203
222
|
[InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
|
204
223
|
[WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
|
2
移動・リサイズできちゃうの図
answer
CHANGED
@@ -211,4 +211,9 @@
|
|
211
211
|
ペイントソフトに向いているかというと微妙な気もします。
|
212
212
|
|
213
213
|
「じゃあどうすりゃいいの?」ってことですが、わたしはこの手のものは全く興味がないので他の方を参考にしてください^^;
|
214
|
-
[Search · wpf paint application](https://github.com/search?q=wpf+paint+application&type=Repositories)
|
214
|
+
[Search · wpf paint application](https://github.com/search?q=wpf+paint+application&type=Repositories)
|
215
|
+
|
216
|
+
---
|
217
|
+
|
218
|
+
コメントで言及したChildrenに入れたコントロールが、(何もしなくても)移動・リサイズできちゃうの図
|
219
|
+

|
1
なんか通じていなそうなので消せてるの図
answer
CHANGED
@@ -198,6 +198,7 @@
|
|
198
198
|
}
|
199
199
|
}
|
200
200
|
```
|
201
|
+

|
201
202
|
参考
|
202
203
|
[InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
|
203
204
|
[WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
|