回答編集履歴

6

リンク切れ アーカイブもなし

2024/11/13 22:13

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -248,7 +248,6 @@
248
248
  ![アプリ画像](6d991bd3ad0ddd1000be0508c439cf4e.png)
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

見直しキャンペーン中

2023/07/29 11:12

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -1,531 +1,266 @@
1
1
  > Rectangle、EllipseをInkCanvasEditingMode.EraseByPointで消せるようにしたいと考えているのですが可能なのでしょうか?
2
2
 
3
-
4
-
5
3
  `EditingMode`を変えるだけなんですから、やってみればいいんでは?(10秒で試せると思うのですが)
6
4
 
7
-
8
-
9
5
  出来たらスゲーなと思いつつ試してみましたが、案の定できませんでした。。。
10
-
11
6
  `Children`に入れたものはあくまで背景?(というかInkの後ろ側に描画するだけ)であって、ストロークには含まれません。
12
-
13
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)
14
8
 
15
9
 
16
-
17
-
18
-
19
10
  > 別の方法でも構いませんので、なにかいい案がある方はぜひお力添えお願いします。
20
11
 
21
-
22
-
23
12
  どうにかして`Stroke`を作るしかなさそうですね。
24
13
 
25
-
26
-
27
14
  ググって出てきたのを参考にざっと作ってみました。
28
-
29
15
  ペン・消しゴム・四角・楕円・選択・ジェスチャ(四角・楕円のみ)モードがあります。
30
-
31
- ```xaml
16
+ ```xml
32
-
33
17
  <Window
34
-
35
18
  x:Class="Questions372920.MainWindow"
36
-
37
19
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
-
39
20
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
40
-
41
21
  xmlns:local="clr-namespace:Questions372920"
42
-
43
22
  Width="800"
44
-
45
23
  Height="450">
46
-
47
24
  <DockPanel>
48
-
49
25
  <UniformGrid HorizontalAlignment="Left" DockPanel.Dock="Top" Rows="1">
50
-
51
26
  <UniformGrid.Resources>
52
-
53
27
  <Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton">
54
-
55
28
  <EventSetter Event="Checked" Handler="RadioButton_Checked" />
56
-
57
29
  </Style>
58
-
59
30
  </UniformGrid.Resources>
60
-
61
31
  <RadioButton Content="{x:Static local:MyInkMode.Pen}" IsChecked="True" />
62
-
63
32
  <RadioButton Content="{x:Static local:MyInkMode.Rect}" />
64
-
65
33
  <RadioButton Content="{x:Static local:MyInkMode.Ellipse}" />
66
-
67
34
  <RadioButton Content="{x:Static local:MyInkMode.Gesture}" />
68
-
69
35
  <RadioButton Content="{x:Static local:MyInkMode.Select}" />
70
-
71
36
  <RadioButton Content="{x:Static local:MyInkMode.Erase}" />
72
-
73
37
  </UniformGrid>
74
38
 
75
-
76
-
77
39
  <Grid>
78
-
79
40
  <InkCanvas
80
-
81
41
  Name="inkCanvas"
82
-
83
42
  Background="WhiteSmoke"
84
-
85
43
  EditingMode="Ink"
86
-
87
44
  Gesture="InkCanvas_Gesture"
88
-
89
45
  MouseLeftButtonDown="InkCanvas_MouseLeftButtonDown"
90
-
91
46
  MouseLeftButtonUp="InkCanvas_MouseLeftButtonUp"
92
-
93
47
  MouseMove="InkCanvas_MouseMove">
94
-
95
48
  <Rectangle
96
-
97
49
  Width="50"
98
-
99
50
  Height="50"
100
-
101
51
  InkCanvas.Left="50"
102
-
103
52
  InkCanvas.Top="50"
104
-
105
53
  Stroke="Red" />
106
-
107
54
  <Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
108
-
109
55
  <TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
110
-
111
56
  </InkCanvas>
112
57
 
113
-
114
-
115
58
  <!-- 四角・楕円ツール使用中の仮表示 本来はAdornerなんかでやるべきだが本題でないので手抜き -->
116
-
117
59
  <Canvas Background="{x:Null}" ClipToBounds="True">
118
-
119
60
  <Ellipse
120
-
121
61
  x:Name="tmpEllipse"
122
-
123
62
  Fill="Red"
124
-
125
63
  Stroke="Black"
126
-
127
64
  StrokeThickness="2" />
128
-
129
65
  <Rectangle
130
-
131
66
  x:Name="tmpRect"
132
-
133
67
  Fill="Red"
134
-
135
68
  Stroke="Black"
136
-
137
69
  StrokeThickness="2" />
138
-
139
70
  </Canvas>
140
-
141
71
  </Grid>
142
-
143
72
  </DockPanel>
144
-
145
73
  </Window>
146
-
147
74
  ```
148
75
 
149
-
150
-
151
- ```C#
76
+ ```cs
152
-
153
77
  using System;
154
-
155
78
  using System.Collections.Generic;
156
-
157
79
  using System.Windows;
158
-
159
80
  using System.Windows.Controls;
160
-
161
81
  using System.Windows.Ink;
162
-
163
82
  using System.Windows.Input;
164
-
165
83
  using System.Windows.Media;
166
-
167
84
  using System.Windows.Shapes;
168
85
 
169
86
 
170
-
171
-
172
-
173
87
  namespace Questions372920
174
-
175
88
  {
176
-
177
89
  public enum MyInkMode { Pen, Rect, Ellipse, Gesture, Select, Erase, };
178
90
 
179
-
180
-
181
91
  public partial class MainWindow : Window
182
-
183
92
  {
184
-
185
93
  private Point startPoint;
186
-
187
94
  private bool isDragging;
188
-
189
95
  private MyInkMode mode;
190
96
 
191
-
192
-
193
97
  public MainWindow() => InitializeComponent();
194
98
 
195
-
196
-
197
99
  private void RadioButton_Checked(object sender, RoutedEventArgs e)
198
-
199
- {
100
+ {
200
-
201
101
  if (sender is RadioButton rb && rb.Content is MyInkMode m)
202
-
203
- {
102
+ {
204
-
205
103
  mode = m;
206
-
207
104
  if (inkCanvas == null) return;
208
105
 
209
-
210
-
211
106
  switch (mode)
212
-
213
107
  {
214
-
215
108
  case MyInkMode.Pen: inkCanvas.EditingMode = InkCanvasEditingMode.Ink; break;
216
-
217
109
  case MyInkMode.Rect:
218
-
219
110
  case MyInkMode.Ellipse:
220
-
221
111
  inkCanvas.EditingMode = InkCanvasEditingMode.None;
222
-
223
112
  break;
224
-
225
113
  case MyInkMode.Gesture: inkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; break;
226
-
227
114
  case MyInkMode.Select: inkCanvas.EditingMode = InkCanvasEditingMode.Select; break;
228
-
229
115
  case MyInkMode.Erase: inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break;
230
-
231
116
  }
232
-
233
- }
117
+ }
234
-
235
- }
118
+ }
236
-
237
-
238
119
 
239
120
  private void InkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
240
-
241
- {
121
+ {
242
-
243
122
  if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
244
123
 
245
-
246
-
247
124
  isDragging = true;
248
-
249
125
  startPoint = e.GetPosition(inkCanvas);
250
-
251
126
  inkCanvas.CaptureMouse();
252
-
253
- }
127
+ }
254
-
255
-
256
128
 
257
129
  private void InkCanvas_MouseMove(object sender, MouseEventArgs e)
258
-
259
- {
130
+ {
260
-
261
131
  if (!isDragging) return;
262
132
 
263
-
264
-
265
133
  Shape shape;
266
-
267
134
  switch (mode)
268
-
269
- {
135
+ {
270
-
271
136
  case MyInkMode.Rect: shape = tmpRect; break;
272
-
273
137
  case MyInkMode.Ellipse: shape = tmpEllipse; break;
274
-
275
138
  default: return;
276
-
277
- }
139
+ }
278
-
279
-
280
140
 
281
141
  var r = new Rect(startPoint, e.GetPosition(inkCanvas));
282
-
283
142
  r.Inflate(1, 1);
284
143
 
285
-
286
-
287
144
  Canvas.SetLeft(shape, r.X);
288
-
289
145
  Canvas.SetTop(shape, r.Y);
290
-
291
146
  shape.Width = r.Width;
292
-
293
147
  shape.Height = r.Height;
294
-
295
- }
148
+ }
296
-
297
-
298
149
 
299
150
  private void InkCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
300
-
301
- {
151
+ {
302
-
303
152
  if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
304
153
 
305
-
306
-
307
154
  inkCanvas.ReleaseMouseCapture();
308
-
309
155
  isDragging = false;
310
156
 
311
-
312
-
313
157
  var r = new Rect(startPoint, e.GetPosition(inkCanvas));
314
-
315
158
  switch (mode)
316
-
317
- {
159
+ {
318
-
319
160
  case MyInkMode.Rect:
320
-
321
161
  AddRect(r.TopLeft, r.BottomRight);
322
-
323
162
  tmpRect.Width = tmpRect.Height = 0;
324
-
325
163
  break;
326
-
327
164
  case MyInkMode.Ellipse:
328
-
329
165
  AddEllipse(r.TopLeft, r.BottomRight);
330
-
331
166
  tmpEllipse.Width = tmpEllipse.Height = 0;
332
-
333
167
  break;
334
-
335
168
  default: return;
336
-
337
- }
169
+ }
338
-
339
- }
170
+ }
340
-
341
-
342
171
 
343
172
  private void InkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e)
344
-
345
- {
173
+ {
346
-
347
174
  var result = e.GetGestureRecognitionResults()[0];
348
-
349
175
  if (result.RecognitionConfidence != RecognitionConfidence.Strong) return;
350
176
 
351
-
352
-
353
177
  var r = e.Strokes.GetBounds();
354
-
355
178
  switch (result.ApplicationGesture)
356
-
357
- {
179
+ {
358
-
359
180
  case ApplicationGesture.Square: AddRect(r.TopLeft, r.BottomRight); break;
360
-
361
181
  case ApplicationGesture.Circle: AddEllipse(r.TopLeft, r.BottomRight); break;
362
-
363
- }
182
+ }
364
-
365
- }
183
+ }
366
-
367
-
368
184
 
369
185
  private void AddRect(Point start, Point end)
370
-
371
- {
186
+ {
372
-
373
187
  var n = new List<Point>();
374
-
375
188
  for (var i = start.Y; i < end.Y; i += 2) // えらい雑w
376
-
377
- {
189
+ {
378
-
379
190
  n.Add(new Point(start.X, i));
380
-
381
191
  n.Add(new Point(end.X, i));
382
-
383
- }
192
+ }
384
-
385
193
  if (0 < n.Count)
386
-
387
- {
194
+ {
388
-
389
195
  var s = new Stroke(new StylusPointCollection(n));
390
-
391
196
  s.DrawingAttributes.Color = Colors.Red;
392
-
393
197
  inkCanvas.Strokes.Add(s);
394
-
395
- }
198
+ }
396
-
397
-
398
199
 
399
200
  var points = new Point[]
400
-
401
- {
201
+ {
402
-
403
202
  new Point(start.X, start.Y),
404
-
405
203
  new Point(start.X, end.Y),
406
-
407
204
  new Point(end.X, end.Y),
408
-
409
205
  new Point(end.X, start.Y),
410
-
411
206
  new Point(start.X, start.Y),
412
-
413
207
  };
414
208
 
415
-
416
-
417
209
  var stroke = new Stroke(new StylusPointCollection(points));
418
-
419
210
  inkCanvas.Strokes.Add(stroke);
420
-
421
- }
211
+ }
422
-
423
-
424
212
 
425
213
  private void AddEllipse(Point start, Point end)
426
-
427
- {
214
+ {
428
-
429
215
  var a = (end.X - start.X) / 2;
430
-
431
216
  var b = (end.Y - start.Y) / 2;
432
-
433
217
  var offset = (Vector)((end - start) / 2 + start);
434
218
 
435
-
436
-
437
219
  var n = new List<Point>();
438
-
439
220
  for (var x = -a; x < a; x += 2) // さらに雑w
440
-
441
- {
221
+ {
442
-
443
222
  var y = b * Math.Sqrt(Math.Pow(a, 2) - Math.Pow(x, 2)) / a;
444
-
445
223
  n.Add(new Point(x, -y) + offset);
446
-
447
224
  n.Add(new Point(x, y) + offset);
448
-
449
- }
225
+ }
450
-
451
226
  if (0 < n.Count)
452
-
453
- {
227
+ {
454
-
455
228
  var s = new Stroke(new StylusPointCollection(n));
456
-
457
229
  s.DrawingAttributes.Color = Colors.Red;
458
-
459
230
  inkCanvas.Strokes.Add(s);
460
-
461
- }
231
+ }
462
-
463
-
464
232
 
465
233
  var points = new List<Point>();
466
-
467
234
  for (var i = 0; i <= 360; i += 5)
468
-
469
- {
235
+ {
470
-
471
236
  var x = a * Math.Cos(i / 180d * Math.PI);
472
-
473
237
  var y = b * Math.Sin(i / 180d * Math.PI);
474
-
475
238
  points.Add(new Point(x, y) + offset);
476
-
477
- }
239
+ }
478
-
479
-
480
240
 
481
241
  var stroke = new Stroke(new StylusPointCollection(points));
482
-
483
242
  stroke.DrawingAttributes.FitToCurve = true;
484
-
485
243
  inkCanvas.Strokes.Add(stroke);
486
-
487
- }
244
+ }
488
-
489
245
  }
490
-
491
246
  }
492
-
493
247
  ```
494
-
495
248
  ![アプリ画像](6d991bd3ad0ddd1000be0508c439cf4e.png)
496
-
497
249
  参考
498
-
499
250
  [InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)
500
-
501
251
  [WPF inkcanvas draws rectangles and ellipses | Develop Paper](https://developpaper.com/wpf-inkcanvas-draws-rectangles-and-ellipses/)
502
252
 
503
-
504
-
505
253
  ---
506
254
 
507
-
508
-
509
255
  > C# wpfにてペイントソフトを作ろうと思っています。
510
256
 
511
-
512
-
513
257
  `InkCanvas`は手書き・ジェスチャ認識等すごい機能もあるんですが、あくまでペン用であって例えば塗りつぶし機能はありません。
514
-
515
258
  ペイントソフトに向いているかというと微妙な気もします。
516
259
 
517
-
518
-
519
260
  「じゃあどうすりゃいいの?」ってことですが、わたしはこの手のものは全く興味がないので他の方を参考にしてください^^;
520
-
521
261
  [Search · wpf paint application](https://github.com/search?q=wpf+paint+application&type=Repositories)
522
262
 
523
-
524
-
525
263
  ---
526
264
 
527
-
528
-
529
265
  コメントで言及したChildrenに入れたコントロールが、(何もしなくても)移動・リサイズできちゃうの図
530
-
531
266
  ![アプリ画像](a578aef9a514cdfac8a6ee02cd2e60d4.png)

4

楕円対応

2021/12/10 08:40

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -46,69 +46,99 @@
46
46
 
47
47
  <DockPanel>
48
48
 
49
- <StackPanel DockPanel.Dock="Top">
50
-
51
- <ComboBox x:Name="comboBox" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged">
52
-
53
- <x:Static Member="local:MyInkMode.Pen" />
54
-
55
- <x:Static Member="local:MyInkMode.Rect" />
56
-
57
- <x:Static Member="local:MyInkMode.Ellipse" />
58
-
59
- <x:Static Member="local:MyInkMode.Gesture" />
60
-
61
- <x:Static Member="local:MyInkMode.Select" />
62
-
63
- <x:Static Member="local:MyInkMode.Erase" />
64
-
65
- </ComboBox>
66
-
67
- </StackPanel>
68
-
69
-
70
-
71
- <InkCanvas
72
-
73
- Name="inkCanvas"
74
-
75
- Background="WhiteSmoke"
76
-
77
- EditingMode="Ink"
78
-
79
- Gesture="InkCanvas_Gesture"
80
-
81
- MouseLeftButtonDown="InkCanvas_MouseLeftButtonDown"
82
-
83
- MouseLeftButtonUp="InkCanvas_MouseLeftButtonUp"
84
-
85
- MouseMove="InkCanvas_MouseMove">
86
-
87
-
88
-
89
- <Rectangle
90
-
91
- Width="50"
92
-
93
- Height="50"
94
-
95
- InkCanvas.Left="50"
96
-
97
- InkCanvas.Top="50"
98
-
99
- Stroke="Red" />
100
-
101
- <Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
102
-
103
- <TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
104
-
105
-
106
-
107
- <Ellipse x:Name="elli" Stroke="Black" StrokeThickness="2" />
108
-
109
- <Rectangle x:Name="rect" Stroke="Black" StrokeThickness="2" />
110
-
111
- </InkCanvas>
49
+ <UniformGrid HorizontalAlignment="Left" DockPanel.Dock="Top" Rows="1">
50
+
51
+ <UniformGrid.Resources>
52
+
53
+ <Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton">
54
+
55
+ <EventSetter Event="Checked" Handler="RadioButton_Checked" />
56
+
57
+ </Style>
58
+
59
+ </UniformGrid.Resources>
60
+
61
+ <RadioButton Content="{x:Static local:MyInkMode.Pen}" IsChecked="True" />
62
+
63
+ <RadioButton Content="{x:Static local:MyInkMode.Rect}" />
64
+
65
+ <RadioButton Content="{x:Static local:MyInkMode.Ellipse}" />
66
+
67
+ <RadioButton Content="{x:Static local:MyInkMode.Gesture}" />
68
+
69
+ <RadioButton Content="{x:Static local:MyInkMode.Select}" />
70
+
71
+ <RadioButton Content="{x:Static local:MyInkMode.Erase}" />
72
+
73
+ </UniformGrid>
74
+
75
+
76
+
77
+ <Grid>
78
+
79
+ <InkCanvas
80
+
81
+ Name="inkCanvas"
82
+
83
+ Background="WhiteSmoke"
84
+
85
+ EditingMode="Ink"
86
+
87
+ Gesture="InkCanvas_Gesture"
88
+
89
+ MouseLeftButtonDown="InkCanvas_MouseLeftButtonDown"
90
+
91
+ MouseLeftButtonUp="InkCanvas_MouseLeftButtonUp"
92
+
93
+ MouseMove="InkCanvas_MouseMove">
94
+
95
+ <Rectangle
96
+
97
+ Width="50"
98
+
99
+ Height="50"
100
+
101
+ InkCanvas.Left="50"
102
+
103
+ InkCanvas.Top="50"
104
+
105
+ Stroke="Red" />
106
+
107
+ <Button Content="押せないよ" InkCanvas.Left="50" InkCanvas.Top="10" />
108
+
109
+ <TextBlock InkCanvas.Left="50" InkCanvas.Top="110" Text="↑消せないよ" />
110
+
111
+ </InkCanvas>
112
+
113
+
114
+
115
+ <!-- 四角・楕円ツール使用中の仮表示 本来はAdornerなんかでやるべきだが本題でないので手抜き -->
116
+
117
+ <Canvas Background="{x:Null}" ClipToBounds="True">
118
+
119
+ <Ellipse
120
+
121
+ x:Name="tmpEllipse"
122
+
123
+ Fill="Red"
124
+
125
+ Stroke="Black"
126
+
127
+ StrokeThickness="2" />
128
+
129
+ <Rectangle
130
+
131
+ x:Name="tmpRect"
132
+
133
+ Fill="Red"
134
+
135
+ Stroke="Black"
136
+
137
+ StrokeThickness="2" />
138
+
139
+ </Canvas>
140
+
141
+ </Grid>
112
142
 
113
143
  </DockPanel>
114
144
 
@@ -164,138 +194,148 @@
164
194
 
165
195
 
166
196
 
167
- private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
197
+ private void RadioButton_Checked(object sender, RoutedEventArgs e)
168
-
198
+
169
- {
199
+ {
170
-
200
+
171
- mode = (MyInkMode)comboBox.SelectedItem;
201
+ if (sender is RadioButton rb && rb.Content is MyInkMode m)
202
+
172
-
203
+ {
204
+
205
+ mode = m;
206
+
173
- if (inkCanvas == null) return;
207
+ if (inkCanvas == null) return;
208
+
209
+
210
+
174
-
211
+ switch (mode)
212
+
175
-
213
+ {
214
+
215
+ case MyInkMode.Pen: inkCanvas.EditingMode = InkCanvasEditingMode.Ink; break;
216
+
217
+ case MyInkMode.Rect:
218
+
219
+ case MyInkMode.Ellipse:
220
+
221
+ inkCanvas.EditingMode = InkCanvasEditingMode.None;
222
+
223
+ break;
224
+
225
+ case MyInkMode.Gesture: inkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; break;
226
+
227
+ case MyInkMode.Select: inkCanvas.EditingMode = InkCanvasEditingMode.Select; break;
228
+
229
+ case MyInkMode.Erase: inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break;
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ private void InkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
240
+
241
+ {
242
+
243
+ if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
244
+
245
+
246
+
247
+ isDragging = true;
248
+
249
+ startPoint = e.GetPosition(inkCanvas);
250
+
251
+ inkCanvas.CaptureMouse();
252
+
253
+ }
254
+
255
+
256
+
257
+ private void InkCanvas_MouseMove(object sender, MouseEventArgs e)
258
+
259
+ {
260
+
261
+ if (!isDragging) return;
262
+
263
+
264
+
265
+ Shape shape;
176
266
 
177
267
  switch (mode)
178
268
 
179
269
  {
180
270
 
271
+ case MyInkMode.Rect: shape = tmpRect; break;
272
+
181
- case MyInkMode.Pen: inkCanvas.EditingMode = InkCanvasEditingMode.Ink; break;
273
+ case MyInkMode.Ellipse: shape = tmpEllipse; break;
274
+
275
+ default: return;
276
+
277
+ }
278
+
279
+
280
+
281
+ var r = new Rect(startPoint, e.GetPosition(inkCanvas));
282
+
283
+ r.Inflate(1, 1);
284
+
285
+
286
+
287
+ Canvas.SetLeft(shape, r.X);
288
+
289
+ Canvas.SetTop(shape, r.Y);
290
+
291
+ shape.Width = r.Width;
292
+
293
+ shape.Height = r.Height;
294
+
295
+ }
296
+
297
+
298
+
299
+ private void InkCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
300
+
301
+ {
302
+
303
+ if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
304
+
305
+
306
+
307
+ inkCanvas.ReleaseMouseCapture();
308
+
309
+ isDragging = false;
310
+
311
+
312
+
313
+ var r = new Rect(startPoint, e.GetPosition(inkCanvas));
314
+
315
+ switch (mode)
316
+
317
+ {
182
318
 
183
319
  case MyInkMode.Rect:
184
320
 
321
+ AddRect(r.TopLeft, r.BottomRight);
322
+
323
+ tmpRect.Width = tmpRect.Height = 0;
324
+
325
+ break;
326
+
185
327
  case MyInkMode.Ellipse:
186
328
 
329
+ AddEllipse(r.TopLeft, r.BottomRight);
330
+
187
- inkCanvas.EditingMode = InkCanvasEditingMode.None;
331
+ tmpEllipse.Width = tmpEllipse.Height = 0;
188
332
 
189
333
  break;
190
334
 
191
- case MyInkMode.Gesture: inkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; break;
192
-
193
- case MyInkMode.Select: inkCanvas.EditingMode = InkCanvasEditingMode.Select; break;
194
-
195
- case MyInkMode.Erase: inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break;
196
-
197
- }
198
-
199
- }
200
-
201
-
202
-
203
- private void InkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
204
-
205
- {
206
-
207
- if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
208
-
209
-
210
-
211
- isDragging = true;
212
-
213
- startPoint = e.GetPosition(inkCanvas);
214
-
215
- }
216
-
217
-
218
-
219
- private void InkCanvas_MouseMove(object sender, MouseEventArgs e)
220
-
221
- {
222
-
223
- if (!isDragging) return;
224
-
225
-
226
-
227
- Shape shape;
228
-
229
- switch (mode)
230
-
231
- {
232
-
233
- case MyInkMode.Rect: shape = rect; break;
234
-
235
- case MyInkMode.Ellipse: shape = elli; break;
236
-
237
335
  default: return;
238
336
 
239
337
  }
240
338
 
241
-
242
-
243
- var r = new Rect(startPoint, e.GetPosition(inkCanvas));
244
-
245
- r.Inflate(1, 1);
246
-
247
-
248
-
249
- InkCanvas.SetLeft(shape, r.X);
250
-
251
- InkCanvas.SetTop(shape, r.Y);
252
-
253
- shape.Width = r.Width;
254
-
255
- shape.Height = r.Height;
256
-
257
- }
258
-
259
-
260
-
261
- private void InkCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
262
-
263
- {
264
-
265
- if (mode != MyInkMode.Rect && mode != MyInkMode.Ellipse) return;
266
-
267
-
268
-
269
- isDragging = false;
270
-
271
-
272
-
273
- var r = new Rect(startPoint, e.GetPosition(inkCanvas));
274
-
275
- switch (mode)
276
-
277
- {
278
-
279
- case MyInkMode.Rect:
280
-
281
- AddRect(r.TopLeft, r.BottomRight);
282
-
283
- rect.Width = rect.Height = 0;
284
-
285
- break;
286
-
287
- case MyInkMode.Ellipse:
288
-
289
- AddEllipse(r.TopLeft, r.BottomRight);
290
-
291
- elli.Width = elli.Height = 0;
292
-
293
- break;
294
-
295
- default: return;
296
-
297
- }
298
-
299
339
  }
300
340
 
301
341
 
@@ -342,11 +382,17 @@
342
382
 
343
383
  }
344
384
 
385
+ if (0 < n.Count)
386
+
387
+ {
388
+
345
- var s = new Stroke(new StylusPointCollection(n));
389
+ var s = new Stroke(new StylusPointCollection(n));
346
-
390
+
347
- s.DrawingAttributes.Color = Colors.Red;
391
+ s.DrawingAttributes.Color = Colors.Red;
348
-
392
+
349
- inkCanvas.Strokes.Add(s);
393
+ inkCanvas.Strokes.Add(s);
394
+
395
+ }
350
396
 
351
397
 
352
398
 
@@ -376,33 +422,43 @@
376
422
 
377
423
 
378
424
 
379
- // もっと短くできそうですが、こういう計算が苦手なので^^;
380
-
381
- // 元コードよりは大幅にPointが減ってはいます
382
-
383
425
  private void AddEllipse(Point start, Point end)
384
426
 
385
427
  {
386
428
 
387
- var a = 0.5 * (end.X - start.X);
429
+ var a = (end.X - start.X) / 2;
388
-
430
+
389
- var b = 0.5 * (end.Y - start.Y);
431
+ var b = (end.Y - start.Y) / 2;
390
-
432
+
391
- var c = 0.5 * (end.X + start.X);
433
+ var offset = (Vector)((end - start) / 2 + start);
392
-
393
- var d = 0.5 * (end.Y + start.Y);
434
+
394
-
395
-
396
-
435
+
436
+
397
- //var n = new List<Point>();
437
+ var n = new List<Point>();
398
-
438
+
399
- //// ここの計算がにすらできない
439
+ for (var x = -a; x < a; x += 2) // さらに雑w
440
+
400
-
441
+ {
442
+
443
+ var y = b * Math.Sqrt(Math.Pow(a, 2) - Math.Pow(x, 2)) / a;
444
+
445
+ n.Add(new Point(x, -y) + offset);
446
+
447
+ n.Add(new Point(x, y) + offset);
448
+
449
+ }
450
+
451
+ if (0 < n.Count)
452
+
453
+ {
454
+
401
- //var s = new Stroke(new StylusPointCollection(n));
455
+ var s = new Stroke(new StylusPointCollection(n));
402
-
456
+
403
- //s.DrawingAttributes.Color = Colors.Red;
457
+ s.DrawingAttributes.Color = Colors.Red;
404
-
458
+
405
- //inkCanvas.Strokes.Add(s);
459
+ inkCanvas.Strokes.Add(s);
460
+
461
+ }
406
462
 
407
463
 
408
464
 
@@ -412,11 +468,11 @@
412
468
 
413
469
  {
414
470
 
415
- var x = c + a * Math.Cos(i / 180d * Math.PI);
471
+ var x = a * Math.Cos(i / 180d * Math.PI);
416
-
472
+
417
- var y = d + b * Math.Sin(i / 180d * Math.PI);
473
+ var y = b * Math.Sin(i / 180d * Math.PI);
418
-
474
+
419
- points.Add(new Point(x, y));
475
+ points.Add(new Point(x, y) + offset);
420
476
 
421
477
  }
422
478
 
@@ -436,7 +492,7 @@
436
492
 
437
493
  ```
438
494
 
439
- ![アプリ画像](e9fb0504b7be5ad8a91d18096e33a090.png)
495
+ ![アプリ画像](6d991bd3ad0ddd1000be0508c439cf4e.png)
440
496
 
441
497
  参考
442
498
 

3

超雑に塗りつぶし

2021/12/10 08:40

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -132,10 +132,14 @@
132
132
 
133
133
  using System.Windows.Input;
134
134
 
135
+ using System.Windows.Media;
136
+
135
137
  using System.Windows.Shapes;
136
138
 
137
139
 
138
140
 
141
+
142
+
139
143
  namespace Questions372920
140
144
 
141
145
  {
@@ -264,7 +268,9 @@
264
268
 
265
269
  isDragging = false;
266
270
 
271
+
272
+
267
-
273
+ var r = new Rect(startPoint, e.GetPosition(inkCanvas));
268
274
 
269
275
  switch (mode)
270
276
 
@@ -272,7 +278,7 @@
272
278
 
273
279
  case MyInkMode.Rect:
274
280
 
275
- AddRect(startPoint, e.GetPosition(inkCanvas));
281
+ AddRect(r.TopLeft, r.BottomRight);
276
282
 
277
283
  rect.Width = rect.Height = 0;
278
284
 
@@ -280,7 +286,7 @@
280
286
 
281
287
  case MyInkMode.Ellipse:
282
288
 
283
- AddEllipse(startPoint, e.GetPosition(inkCanvas));
289
+ AddEllipse(r.TopLeft, r.BottomRight);
284
290
 
285
291
  elli.Width = elli.Height = 0;
286
292
 
@@ -324,6 +330,26 @@
324
330
 
325
331
  {
326
332
 
333
+ var n = new List<Point>();
334
+
335
+ for (var i = start.Y; i < end.Y; i += 2) // えらい雑w
336
+
337
+ {
338
+
339
+ n.Add(new Point(start.X, i));
340
+
341
+ n.Add(new Point(end.X, i));
342
+
343
+ }
344
+
345
+ var s = new Stroke(new StylusPointCollection(n));
346
+
347
+ s.DrawingAttributes.Color = Colors.Red;
348
+
349
+ inkCanvas.Strokes.Add(s);
350
+
351
+
352
+
327
353
  var points = new Point[]
328
354
 
329
355
  {
@@ -368,6 +394,18 @@
368
394
 
369
395
 
370
396
 
397
+ //var n = new List<Point>();
398
+
399
+ //// ここの計算が雑にすらできないw
400
+
401
+ //var s = new Stroke(new StylusPointCollection(n));
402
+
403
+ //s.DrawingAttributes.Color = Colors.Red;
404
+
405
+ //inkCanvas.Strokes.Add(s);
406
+
407
+
408
+
371
409
  var points = new List<Point>();
372
410
 
373
411
  for (var i = 0; i <= 360; i += 5)
@@ -398,7 +436,7 @@
398
436
 
399
437
  ```
400
438
 
401
- ![アプリ画像](5573a83818836ca5e5b5988461b6838a.png)
439
+ ![アプリ画像](e9fb0504b7be5ad8a91d18096e33a090.png)
402
440
 
403
441
  参考
404
442
 

2

移動・リサイズできちゃうの図

2021/12/09 11:39

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -425,3 +425,13 @@
425
425
  「じゃあどうすりゃいいの?」ってことですが、わたしはこの手のものは全く興味がないので他の方を参考にしてください^^;
426
426
 
427
427
  [Search · wpf paint application](https://github.com/search?q=wpf+paint+application&type=Repositories)
428
+
429
+
430
+
431
+ ---
432
+
433
+
434
+
435
+ コメントで言及したChildrenに入れたコントロールが、(何もしなくても)移動・リサイズできちゃうの図
436
+
437
+ ![アプリ画像](a578aef9a514cdfac8a6ee02cd2e60d4.png)

1

なんか通じていなそうなので消せてるの図

2021/12/09 09:17

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -398,6 +398,8 @@
398
398
 
399
399
  ```
400
400
 
401
+ ![アプリ画像](5573a83818836ca5e5b5988461b6838a.png)
402
+
401
403
  参考
402
404
 
403
405
  [InkCanvas | HIROs.NET Blog](http://blog.hiros-dot.net/?page_id=3413)