回答編集履歴

2

見直しキャンペーン中

2023/07/21 13:57

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,423 +1,212 @@
1
1
  `FillRectangle`相当になってしまいますが、こんなのはどうでしょう?
2
2
 
3
-
4
-
5
3
  図形の保存やヒットテスト等、面倒なことをパネルにお任せしてしまいます(図形を`GraphicsPath`にするのがすこし手間ですが)
6
4
 
7
-
8
-
9
- ```C#
5
+ ```cs
10
-
11
6
  using System;
12
-
13
7
  using System.Drawing;
14
-
15
8
  using System.Drawing.Drawing2D;
16
-
17
9
  using System.Windows.Forms;
18
10
 
19
-
20
-
21
11
  namespace Questions260581
22
-
23
12
  {
24
-
25
13
  public partial class Form1 : Form
26
-
27
14
  {
28
-
29
15
  public Form1()
30
-
31
- {
16
+ {
32
-
33
17
  InitializeComponent();
34
-
35
18
  ClientSize = new Size(1000, 600);
36
19
 
37
-
38
-
39
20
  var left = 2;
40
-
41
21
  var top = 2;
42
-
43
22
  var width = 150;
44
-
45
23
  var height = 150;
46
24
 
47
-
48
-
49
- for(var x = 1; x < 200; ++x)
25
+ for(var x = 1; x < 200; ++x)
50
-
51
- {
26
+ {
52
-
53
27
  left += 4;
54
-
55
- top += 2;
28
+ top += 2;
56
-
57
-
58
29
 
59
30
  var panel = new Panel
60
-
61
31
  {
62
-
63
32
  Top = top,
64
-
65
33
  Left = left,
66
-
67
34
  Width = width,
68
-
69
35
  Height = height,
70
-
71
36
  BackColor = Color.Blue,
72
-
73
37
  };
74
-
75
38
  panel.MouseEnter += Panel_MouseEnter;
76
-
77
39
  panel.MouseLeave += Panel_MouseLeave;
78
-
79
40
  Controls.Add(panel);
80
-
81
- }
41
+ }
82
-
83
-
84
42
 
85
43
  left = 800;
86
-
87
44
  top = 2;
88
45
 
89
-
90
-
91
46
  var path = new GraphicsPath(FillMode.Winding);
92
-
93
47
  path.AddLines(StarPoints(5, new Rectangle(0, 0, width, height)));
94
-
95
48
  var region = new Region(path);
96
49
 
97
-
98
-
99
- for(var x = 1; x < 200; ++x)
50
+ for(var x = 1; x < 200; ++x)
100
-
101
- {
51
+ {
102
-
103
52
  left -= 4;
104
-
105
- top += 2;
53
+ top += 2;
106
-
107
-
108
54
 
109
55
  var panel = new Panel
110
-
111
56
  {
112
-
113
57
  Top = top,
114
-
115
58
  Left = left,
116
-
117
59
  Width = width,
118
-
119
60
  Height = height,
120
-
121
61
  BackColor = Color.Blue,
122
-
123
62
  Region = region,
124
-
125
63
  };
126
64
 
127
-
128
-
129
65
  panel.MouseEnter += Panel_MouseEnter;
130
-
131
66
  panel.MouseLeave += Panel_MouseLeave;
132
-
133
67
  Controls.Add(panel);
134
-
135
- }
68
+ }
136
-
137
- }
69
+ }
138
-
139
-
140
70
 
141
71
  private void Panel_MouseEnter(object sender, EventArgs e)
142
-
143
- {
72
+ {
144
-
145
73
  if(sender is Panel p)
146
-
147
- {
74
+ {
148
-
149
75
  p.BackColor = Color.Red;
150
-
151
76
  p.BringToFront();
152
-
153
- }
77
+ }
154
-
155
- }
78
+ }
156
-
157
-
158
79
 
159
80
  private void Panel_MouseLeave(object sender, EventArgs e)
160
-
161
- {
81
+ {
162
-
163
82
  if(sender is Panel p)
164
-
165
- {
83
+ {
166
-
167
84
  p.BackColor = Color.Blue;
168
-
169
- }
85
+ }
170
-
171
- }
86
+ }
172
-
173
-
174
87
 
175
88
  //http://csharphelper.com/blog/2014/08/draw-a-star-in-c/
176
-
177
89
  private PointF[] StarPoints(int num_points, Rectangle bounds)
178
-
179
- {
90
+ {
180
-
181
91
  var pts = new PointF[num_points];
182
92
 
183
-
184
-
185
93
  double rx = bounds.Width / 2;
186
-
187
94
  double ry = bounds.Height / 2;
188
-
189
95
  var cx = bounds.X + rx;
190
-
191
96
  var cy = bounds.Y + ry;
192
97
 
193
-
194
-
195
98
  var theta = -Math.PI / 2;
196
-
197
99
  var dtheta = 4 * Math.PI / num_points;
198
-
199
100
  for(var i = 0; i < num_points; i++)
200
-
201
- {
101
+ {
202
-
203
102
  pts[i] = new PointF(
204
-
205
103
  (float)(cx + rx * Math.Cos(theta)),
206
-
207
104
  (float)(cy + ry * Math.Sin(theta)));
208
-
209
105
  theta += dtheta;
210
-
211
- }
106
+ }
212
-
213
-
214
107
 
215
108
  return pts;
216
-
217
- }
109
+ }
218
-
219
110
  }
220
-
221
111
  }
222
-
223
112
  ```
224
-
225
113
  ![アプリ画像](118e1ccebedd5cc46bee8da47f173293.png)
226
114
 
227
-
228
-
229
115
  ---
230
116
 
231
-
232
-
233
117
  追記 雑な自前描画版
234
-
235
- ```C#
118
+ ```cs
236
-
237
119
  using System.Collections.Generic;
238
-
239
120
  using System.Drawing;
240
-
241
121
  using System.Drawing.Drawing2D;
242
-
243
122
  using System.Windows.Forms;
244
123
 
245
-
246
-
247
124
  namespace Questions260581
248
-
249
125
  {
250
-
251
126
  public partial class Form1 : Form
252
-
253
127
  {
254
-
255
128
  private readonly List<GraphicsPath> paths = new List<GraphicsPath>();
256
-
257
129
  private GraphicsPath selected = new GraphicsPath(); // nullチェックが面倒なのでカラのパス
258
130
 
259
-
260
-
261
131
  public Form1()
262
-
263
- {
132
+ {
264
-
265
133
  InitializeComponent();
266
-
267
134
  ClientSize = new Size(1000, 600);
268
-
269
135
  MouseMove += Form1_MouseMove;
270
136
 
271
-
272
-
273
137
  var left = 2;
274
-
275
138
  var top = 2;
276
-
277
139
  var width = 150;
278
-
279
140
  var height = 150;
280
141
 
281
-
282
-
283
- for(var x = 1; x < 200; ++x)
142
+ for(var x = 1; x < 200; ++x)
284
-
285
- {
143
+ {
286
-
287
144
  left += 4;
288
-
289
- top += 2;
145
+ top += 2;
290
-
291
-
292
146
 
293
147
  var path = new GraphicsPath();
294
-
295
148
  path.AddRectangle(new Rectangle(left, top, width, height));
296
-
297
149
  paths.Add(path);
298
-
299
- }
150
+ }
300
-
301
-
302
151
 
303
152
  left = 800;
304
-
305
153
  top = 2;
306
-
307
154
  var lines = new PointF[] // StarPoints()が閉じてくれていないので手打ち^^;
308
-
309
- {
155
+ {
310
-
311
156
  new PointF(75f, 0f),
312
-
313
157
  new PointF(119.0839f, 135.6763f),
314
-
315
158
  new PointF(3.670761f, 51.82373f),
316
-
317
159
  new PointF(146.3292f, 51.82373f),
318
-
319
160
  new PointF(30.91611f, 135.6763f),
320
-
321
161
  new PointF(75f, 0f),
322
-
323
162
  };
324
-
325
- for(var x = 1; x < 200; ++x)
163
+ for(var x = 1; x < 200; ++x)
326
-
327
- {
164
+ {
328
-
329
165
  left -= 4;
330
-
331
- top += 2;
166
+ top += 2;
332
-
333
-
334
167
 
335
168
  var path = new GraphicsPath();
336
-
337
169
  path.AddLines(lines);
338
-
339
170
  var matrix = new Matrix();
340
-
341
171
  matrix.Translate(left, top, MatrixOrder.Append);
342
-
343
172
  path.Transform(matrix);
344
-
345
173
  paths.Add(path);
346
-
347
- }
174
+ }
348
-
349
- }
175
+ }
350
-
351
-
352
176
 
353
177
  private void Form1_MouseMove(object sender, MouseEventArgs e)
354
-
355
- {
178
+ {
356
-
357
179
  foreach(var path in paths)
358
-
359
- {
180
+ {
360
-
361
181
  if(!path.IsVisible(e.Location)) continue;
362
-
363
182
  if(path == selected) return;
364
183
 
365
-
366
-
367
184
  using(var g = CreateGraphics())
368
-
369
185
  {
370
-
371
186
  g.DrawPath(Pens.Blue, selected);
372
-
373
187
  selected = path;
374
-
375
188
  g.DrawPath(Pens.Red, selected);
376
-
377
189
  }
378
-
379
190
  return;
380
-
381
- }
191
+ }
382
-
383
-
384
192
 
385
193
  using(var g = CreateGraphics())
386
-
387
- {
194
+ {
388
-
389
195
  g.DrawPath(Pens.Blue, selected);
390
-
391
- }
196
+ }
392
-
393
-
394
197
 
395
198
  selected = new GraphicsPath(); // 選択なし nullチェックが面倒なのでカラのパス
396
-
397
- }
199
+ }
398
-
399
-
400
200
 
401
201
  protected override void OnPaint(PaintEventArgs e)
402
-
403
- {
202
+ {
404
-
405
203
  base.OnPaint(e);
406
204
 
407
-
408
-
409
205
  foreach(var path in paths)
410
-
411
- {
206
+ {
412
-
413
207
  e.Graphics.DrawPath(Pens.Blue, path);
414
-
415
- }
208
+ }
416
-
417
- }
209
+ }
418
-
419
210
  }
420
-
421
211
  }
422
-
423
212
  ```

1

追記 雑な自前描画版

2020/05/12 09:19

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -223,3 +223,201 @@
223
223
  ```
224
224
 
225
225
  ![アプリ画像](118e1ccebedd5cc46bee8da47f173293.png)
226
+
227
+
228
+
229
+ ---
230
+
231
+
232
+
233
+ 追記 雑な自前描画版
234
+
235
+ ```C#
236
+
237
+ using System.Collections.Generic;
238
+
239
+ using System.Drawing;
240
+
241
+ using System.Drawing.Drawing2D;
242
+
243
+ using System.Windows.Forms;
244
+
245
+
246
+
247
+ namespace Questions260581
248
+
249
+ {
250
+
251
+ public partial class Form1 : Form
252
+
253
+ {
254
+
255
+ private readonly List<GraphicsPath> paths = new List<GraphicsPath>();
256
+
257
+ private GraphicsPath selected = new GraphicsPath(); // nullチェックが面倒なのでカラのパス
258
+
259
+
260
+
261
+ public Form1()
262
+
263
+ {
264
+
265
+ InitializeComponent();
266
+
267
+ ClientSize = new Size(1000, 600);
268
+
269
+ MouseMove += Form1_MouseMove;
270
+
271
+
272
+
273
+ var left = 2;
274
+
275
+ var top = 2;
276
+
277
+ var width = 150;
278
+
279
+ var height = 150;
280
+
281
+
282
+
283
+ for(var x = 1; x < 200; ++x)
284
+
285
+ {
286
+
287
+ left += 4;
288
+
289
+ top += 2;
290
+
291
+
292
+
293
+ var path = new GraphicsPath();
294
+
295
+ path.AddRectangle(new Rectangle(left, top, width, height));
296
+
297
+ paths.Add(path);
298
+
299
+ }
300
+
301
+
302
+
303
+ left = 800;
304
+
305
+ top = 2;
306
+
307
+ var lines = new PointF[] // StarPoints()が閉じてくれていないので手打ち^^;
308
+
309
+ {
310
+
311
+ new PointF(75f, 0f),
312
+
313
+ new PointF(119.0839f, 135.6763f),
314
+
315
+ new PointF(3.670761f, 51.82373f),
316
+
317
+ new PointF(146.3292f, 51.82373f),
318
+
319
+ new PointF(30.91611f, 135.6763f),
320
+
321
+ new PointF(75f, 0f),
322
+
323
+ };
324
+
325
+ for(var x = 1; x < 200; ++x)
326
+
327
+ {
328
+
329
+ left -= 4;
330
+
331
+ top += 2;
332
+
333
+
334
+
335
+ var path = new GraphicsPath();
336
+
337
+ path.AddLines(lines);
338
+
339
+ var matrix = new Matrix();
340
+
341
+ matrix.Translate(left, top, MatrixOrder.Append);
342
+
343
+ path.Transform(matrix);
344
+
345
+ paths.Add(path);
346
+
347
+ }
348
+
349
+ }
350
+
351
+
352
+
353
+ private void Form1_MouseMove(object sender, MouseEventArgs e)
354
+
355
+ {
356
+
357
+ foreach(var path in paths)
358
+
359
+ {
360
+
361
+ if(!path.IsVisible(e.Location)) continue;
362
+
363
+ if(path == selected) return;
364
+
365
+
366
+
367
+ using(var g = CreateGraphics())
368
+
369
+ {
370
+
371
+ g.DrawPath(Pens.Blue, selected);
372
+
373
+ selected = path;
374
+
375
+ g.DrawPath(Pens.Red, selected);
376
+
377
+ }
378
+
379
+ return;
380
+
381
+ }
382
+
383
+
384
+
385
+ using(var g = CreateGraphics())
386
+
387
+ {
388
+
389
+ g.DrawPath(Pens.Blue, selected);
390
+
391
+ }
392
+
393
+
394
+
395
+ selected = new GraphicsPath(); // 選択なし nullチェックが面倒なのでカラのパス
396
+
397
+ }
398
+
399
+
400
+
401
+ protected override void OnPaint(PaintEventArgs e)
402
+
403
+ {
404
+
405
+ base.OnPaint(e);
406
+
407
+
408
+
409
+ foreach(var path in paths)
410
+
411
+ {
412
+
413
+ e.Graphics.DrawPath(Pens.Blue, path);
414
+
415
+ }
416
+
417
+ }
418
+
419
+ }
420
+
421
+ }
422
+
423
+ ```