回答編集履歴

1

見直しキャンペーン中

2023/07/22 09:21

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,317 +1,159 @@
1
1
  100本の棒が回転しています(これ自体に意味はない。動いてるのがわかりやすい例ってだけ)
2
-
3
2
  棒の中央あたりをクリック連打してください。
4
-
5
3
  中央あたりは常に反応するはずですが、たまに抜けるのが確認できると思います(抜けない場合は`iteration`を増やしてください。カクカクになるくらいでちょうどいいです)
6
-
7
4
  タイマーの種類はあまり関係ありません。
8
-
9
5
  `canvas.Children.Clear()`している以上、反応するかはタイミング次第ということになります。
10
6
 
11
-
12
-
13
- ```xaml
7
+ ```xml
14
-
15
8
  <Window
16
-
17
9
  x:Class="Questions269801.MainWindow"
18
-
19
10
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
-
21
11
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
-
23
12
  Width="800"
24
-
25
13
  Height="450">
26
-
27
14
  <Canvas x:Name="canvas" MouseLeftButtonDown="canvas_MouseLeftDown" />
28
-
29
15
  </Window>
30
-
31
16
  ```
32
17
 
33
-
34
-
35
- ```C#
18
+ ```cs
36
-
37
19
  using System;
38
-
39
20
  using System.Diagnostics;
40
-
41
21
  using System.Linq;
42
-
43
22
  using System.Windows;
44
-
45
23
  using System.Windows.Input;
46
-
47
24
  using System.Windows.Media;
48
-
49
25
  using System.Windows.Shapes;
50
-
51
26
  using System.Windows.Threading;
52
27
 
28
+ namespace Questions269801
29
+ {
30
+ public partial class MainWindow : Window
31
+ {
32
+ System.Threading.Timer threadingTimer;
33
+ DispatcherTimer dispatcherTimer;
53
34
 
35
+ const int iteration = 100;
36
+ int angle;
37
+ public MainWindow()
38
+ {
39
+ InitializeComponent();
40
+
41
+ //threadingTimer = new System.Threading.Timer(new System.Threading.TimerCallback(TimerCallBack));
42
+ //threadingTimer.Change(0, 1);
43
+
44
+ dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1), };
45
+ dispatcherTimer.Tick += Timer_Tick;
46
+ dispatcherTimer.Start();
47
+ }
48
+
49
+ private void Timer_Tick(object sender, EventArgs e)
50
+ {
51
+ angle++;
52
+ var sin = Math.Sin(angle * (Math.PI / 180));
53
+ var cos = Math.Cos(angle * (Math.PI / 180));
54
+ canvas.Children.Clear();
55
+
56
+ foreach(var i in Enumerable.Range(0, iteration))
57
+ {
58
+ var line = new Polyline { Stroke = Brushes.Black, StrokeThickness = 50, };
59
+ line.Points.Add(new Point(cos * 100 + 200 + i * 100, sin * 100 + 200));
60
+ line.Points.Add(new Point(-cos * 100 + 200 + i * 100, -sin * 100 + 200));
61
+ canvas.Children.Add(line);
62
+ }
63
+ }
64
+
65
+ void TimerCallBack(object state)
66
+ {
67
+ angle++;
68
+ var sin = Math.Sin(angle * (Math.PI / 180));
69
+ var cos = Math.Cos(angle * (Math.PI / 180));
70
+ Dispatcher.BeginInvoke(new Action(() =>
71
+ {
72
+ canvas.Children.Clear();
73
+
74
+ foreach(var i in Enumerable.Range(0, iteration))
75
+ {
76
+ var line = new Polyline { Stroke = Brushes.Black, StrokeThickness = 50, };
77
+ line.Points.Add(new Point(cos * 100 + 200 + i * 100, sin * 100 + 200));
78
+ line.Points.Add(new Point(-cos * 100 + 200 + i * 100, -sin * 100 + 200));
79
+ canvas.Children.Add(line);
80
+ }
81
+ }));
82
+ }
83
+
84
+ private void canvas_MouseLeftDown(object sender, MouseButtonEventArgs e)
85
+ {
86
+ Debug.WriteLine("canvas_MouseLeftDown");
87
+ }
88
+ }
89
+ }
90
+ ```
91
+ ---
92
+
93
+ hihijijiさんが言っているように、**クリアしない手法**を考える必要があります。
94
+ どうするかは描画内容次第ですが、今回は`RotateTransform`を使ってみました。
95
+ クリック連打時の違いを確認してください。
96
+
97
+ xamlは同一
98
+ ```cs
99
+ using System;
100
+ using System.Diagnostics;
101
+ using System.Linq;
102
+ using System.Windows;
103
+ using System.Windows.Input;
104
+ using System.Windows.Media;
105
+ using System.Windows.Shapes;
106
+ using System.Windows.Threading;
54
107
 
55
108
  namespace Questions269801
56
-
57
109
  {
58
-
59
110
  public partial class MainWindow : Window
60
-
61
111
  {
62
-
63
- System.Threading.Timer threadingTimer;
64
-
65
112
  DispatcherTimer dispatcherTimer;
66
113
 
67
-
68
-
69
114
  const int iteration = 100;
70
-
71
115
  int angle;
72
-
73
116
  public MainWindow()
74
-
75
117
  {
76
-
77
118
  InitializeComponent();
78
119
 
79
-
80
-
81
- //threadingTimer = new System.Threading.Timer(new System.Threading.TimerCallback(TimerCallBack));
82
-
83
- //threadingTimer.Change(0, 1);
84
-
85
-
86
-
87
120
  dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1), };
88
-
89
121
  dispatcherTimer.Tick += Timer_Tick;
90
-
91
122
  dispatcherTimer.Start();
92
123
 
124
+ angle++;
125
+ var sin = Math.Sin(angle * (Math.PI / 180));
126
+ var cos = Math.Cos(angle * (Math.PI / 180));
127
+ canvas.Children.Clear();
128
+
129
+ foreach(var i in Enumerable.Range(0, iteration))
130
+ {
131
+ var line = new Polyline { Stroke = Brushes.Black, StrokeThickness = 50, };
132
+ line.Points.Add(new Point(cos * 100 + 200 + i * 100, sin * 100 + 200));
133
+ line.Points.Add(new Point(-cos * 100 + 200 + i * 100, -sin * 100 + 200));
134
+ line.RenderTransform = new RotateTransform
135
+ {
136
+ CenterX = 200 + i * 100,
137
+ CenterY = 200,
138
+ };
139
+ canvas.Children.Add(line);
140
+ }
93
141
  }
94
142
 
95
-
96
-
97
143
  private void Timer_Tick(object sender, EventArgs e)
98
-
99
144
  {
100
-
101
145
  angle++;
102
-
103
- var sin = Math.Sin(angle * (Math.PI / 180));
104
-
105
- var cos = Math.Cos(angle * (Math.PI / 180));
106
-
107
- canvas.Children.Clear();
108
-
109
-
110
-
111
- foreach(var i in Enumerable.Range(0, iteration))
146
+ foreach(Polyline line in canvas.Children)
112
-
113
147
  {
114
-
115
- var line = new Polyline { Stroke = Brushes.Black, StrokeThickness = 50, };
148
+ var t = line.RenderTransform as RotateTransform;
116
-
117
- line.Points.Add(new Point(cos * 100 + 200 + i * 100, sin * 100 + 200));
118
-
119
- line.Points.Add(new Point(-cos * 100 + 200 + i * 100, -sin * 100 + 200));
120
-
121
- canvas.Children.Add(line);
149
+ t.Angle = angle;
122
-
123
150
  }
124
-
125
151
  }
126
152
 
127
-
128
-
129
- void TimerCallBack(object state)
153
+ private void canvas_MouseLeftDown(object sender, MouseButtonEventArgs e)
130
-
131
154
  {
132
-
133
- angle++;
134
-
135
- var sin = Math.Sin(angle * (Math.PI / 180));
136
-
137
- var cos = Math.Cos(angle * (Math.PI / 180));
138
-
139
- Dispatcher.BeginInvoke(new Action(() =>
140
-
141
- {
142
-
143
- canvas.Children.Clear();
144
-
145
-
146
-
147
- foreach(var i in Enumerable.Range(0, iteration))
155
+ Debug.WriteLine("canvas_MouseLeftDown");
148
-
149
- {
150
-
151
- var line = new Polyline { Stroke = Brushes.Black, StrokeThickness = 50, };
152
-
153
- line.Points.Add(new Point(cos * 100 + 200 + i * 100, sin * 100 + 200));
154
-
155
- line.Points.Add(new Point(-cos * 100 + 200 + i * 100, -sin * 100 + 200));
156
-
157
- canvas.Children.Add(line);
158
-
159
- }
160
-
161
- }));
162
-
163
156
  }
164
-
165
-
166
-
167
- private void canvas_MouseLeftDown(object sender, MouseButtonEventArgs e)
168
-
169
- {
170
-
171
- Debug.WriteLine("canvas_MouseLeftDown");
172
-
173
- }
174
-
175
157
  }
176
-
177
158
  }
178
-
179
159
  ```
180
-
181
- ---
182
-
183
-
184
-
185
- hihijijiさんが言っているように、**クリアしない手法**を考える必要があります。
186
-
187
- どうするかは描画内容次第ですが、今回は`RotateTransform`を使ってみました。
188
-
189
- クリック連打時の違いを確認してください。
190
-
191
-
192
-
193
- xamlは同一
194
-
195
- ```C#
196
-
197
- using System;
198
-
199
- using System.Diagnostics;
200
-
201
- using System.Linq;
202
-
203
- using System.Windows;
204
-
205
- using System.Windows.Input;
206
-
207
- using System.Windows.Media;
208
-
209
- using System.Windows.Shapes;
210
-
211
- using System.Windows.Threading;
212
-
213
-
214
-
215
- namespace Questions269801
216
-
217
- {
218
-
219
- public partial class MainWindow : Window
220
-
221
- {
222
-
223
- DispatcherTimer dispatcherTimer;
224
-
225
-
226
-
227
- const int iteration = 100;
228
-
229
- int angle;
230
-
231
- public MainWindow()
232
-
233
- {
234
-
235
- InitializeComponent();
236
-
237
-
238
-
239
- dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1), };
240
-
241
- dispatcherTimer.Tick += Timer_Tick;
242
-
243
- dispatcherTimer.Start();
244
-
245
-
246
-
247
- angle++;
248
-
249
- var sin = Math.Sin(angle * (Math.PI / 180));
250
-
251
- var cos = Math.Cos(angle * (Math.PI / 180));
252
-
253
- canvas.Children.Clear();
254
-
255
-
256
-
257
- foreach(var i in Enumerable.Range(0, iteration))
258
-
259
- {
260
-
261
- var line = new Polyline { Stroke = Brushes.Black, StrokeThickness = 50, };
262
-
263
- line.Points.Add(new Point(cos * 100 + 200 + i * 100, sin * 100 + 200));
264
-
265
- line.Points.Add(new Point(-cos * 100 + 200 + i * 100, -sin * 100 + 200));
266
-
267
- line.RenderTransform = new RotateTransform
268
-
269
- {
270
-
271
- CenterX = 200 + i * 100,
272
-
273
- CenterY = 200,
274
-
275
- };
276
-
277
- canvas.Children.Add(line);
278
-
279
- }
280
-
281
- }
282
-
283
-
284
-
285
- private void Timer_Tick(object sender, EventArgs e)
286
-
287
- {
288
-
289
- angle++;
290
-
291
- foreach(Polyline line in canvas.Children)
292
-
293
- {
294
-
295
- var t = line.RenderTransform as RotateTransform;
296
-
297
- t.Angle = angle;
298
-
299
- }
300
-
301
- }
302
-
303
-
304
-
305
- private void canvas_MouseLeftDown(object sender, MouseButtonEventArgs e)
306
-
307
- {
308
-
309
- Debug.WriteLine("canvas_MouseLeftDown");
310
-
311
- }
312
-
313
- }
314
-
315
- }
316
-
317
- ```