質問編集履歴

2

できるだけコンパクトに。不要なタグを削除。

2019/12/11 06:41

投稿

anpan___
anpan___

スコア28

test CHANGED
File without changes
test CHANGED
@@ -76,540 +76,386 @@
76
76
 
77
77
 
78
78
 
79
- また現在自分が作成しているコードを下記に添付させていただきます。
79
+ また**参考までに**現在自分が作成しているコードを下記に添付させていただきます。
80
-
80
+
81
+
82
+
81
- ```VB
83
+ ```vb
82
-
84
+
83
- Public Class Form1
85
+ Public Class Cell
86
+
84
-
87
+ Public Status As CellStatus
88
+
85
- Dim S As New Source
89
+ Public S As Source
90
+
86
-
91
+ Public Position As Point '論理位置
92
+
87
-
93
+ Public Rectangle As Rectangle '物理位置
94
+
88
-
95
+ Public Focused As Boolean
96
+
97
+
98
+
99
+
100
+
89
-
101
+ '■コンストラクタ
90
-
102
+
91
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
103
+ Public Sub New(ByVal S As Source, ByVal Position As Point)
104
+
105
+
106
+
92
-
107
+ Me.S = S
108
+
109
+ Me.Position = Position
110
+
111
+
112
+
113
+ '物理位置を求める。
114
+
115
+ Dim Rect As New Rectangle
116
+
117
+
118
+
119
+ '論理位置から物理位置を求めます。
120
+
121
+ Rect.X = Position.X * Source.CellSize
122
+
123
+ Rect.Y = Position.Y * Source.CellSize
124
+
125
+ Rect.Width = Source.CellSize
126
+
127
+ Rect.Height = Source.CellSize
128
+
129
+
130
+
93
- Me.KeyPreview = True
131
+ Me.Rectangle = Rect
94
132
 
95
133
  End Sub
96
134
 
97
135
 
98
136
 
99
- Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
137
+ '■Focus
100
-
138
+
101
- S.Draw(e.Graphics)
139
+ Public Sub Focus()
140
+
141
+
142
+
143
+ Dim X As Integer
144
+
145
+ Dim Y As Integer
146
+
147
+
148
+
149
+ '同じグリッドに属する自分以外のセルを非アクティブにする。
150
+
151
+ For X = 0 To source.XCount - 1
152
+
153
+ For Y = 0 To source.YCount - 1
154
+
155
+ S.Cells(X, Y).Focused = False
156
+
157
+ Next
158
+
159
+ Next
160
+
161
+
162
+
163
+ '自分自身をアクティブにする。
164
+
165
+ Me.Focused = True
166
+
167
+
102
168
 
103
169
  End Sub
104
170
 
171
+
172
+
173
+ Public Sub Draw(ByVal g As Graphics)
174
+
175
+ Dim oPen As New Pen(Color.Orange, 3)
176
+
177
+ Dim rPen As New Pen(Color.Red, 2)
178
+
179
+ Dim blue = New SolidBrush(Color.AntiqueWhite)
180
+
181
+ Dim pink = New SolidBrush(Color.Pink)
182
+
183
+ Dim CellRect As Rectangle
184
+
185
+
186
+
187
+ 'オレンジ枠に余裕を持たせる
188
+
189
+ CellRect = Me.Rectangle
190
+
191
+ CellRect.Inflate(-2, -2)
192
+
193
+ Dim fnt As New Font("MS UI Gothic", 40)
194
+
195
+ Dim CellRectNotFocus As Rectangle ' フォーカス外のマス
196
+
197
+
198
+
199
+
200
+
201
+ 'フォーカスのある枠をオレンジで囲む
202
+
203
+ If Me.Focused Then
204
+
205
+ g.DrawRectangle(oPen, CellRect) 'オレンジ色の枠描画
206
+
207
+ If S.Cells(Position.X, Position.Y).Status <> Nothing Then
208
+
209
+ For X = 0 To Source.XCount - 1
210
+
211
+ For Y = 0 To Source.YCount - 1
212
+
213
+ CellRectNotFocus = S.Cells(X, Y).Rectangle
214
+
215
+ CellRectNotFocus.Inflate(-3, -3)
216
+
217
+
218
+
219
+ If Position.X = X And Position.Y = Y Then 'フォーカスセルの場合(条件追加)
220
+
221
+ g.FillRectangle(pink, CellRectNotFocus) 'マスをピンクで塗る
222
+
223
+ ElseIf Position.X = X Then
224
+
225
+ g.FillRectangle(blue, CellRectNotFocus)
226
+
227
+ Call InputNumber(g, X, Y) '数字入力
228
+
229
+ ElseIf Position.Y = Y Then
230
+
231
+ g.FillRectangle(blue, CellRectNotFocus)
232
+
233
+ Call InputNumber(g, X, Y) '数字入力
234
+
235
+ End If
236
+
237
+ If S.Cells(Position.X, Position.Y).Status = S.Cells(X, Y).Status Then
238
+
239
+ g.DrawRectangle(rPen, CellRectNotFocus) '赤色の枠描画
240
+
241
+ End If
242
+
243
+ Next
244
+
245
+ Next
246
+
247
+ End If
248
+
249
+ End If
250
+
251
+
252
+
253
+ 'CellStatusによって表示を変更
254
+
255
+ Select Case Me.Status
256
+
257
+ ____________中略___________
258
+
259
+
260
+
261
+ 'cell(X,Y)のマスに数字を描画する
262
+
105
- Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
263
+ Public Sub InputNumber(ByVal g As Graphics, ByVal X As Integer, ByVal Y As Integer)
106
-
107
- 'マウスの座標をPictureBox1のコントロール座標に変換する。
264
+
108
-
109
- Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position)
265
+ Dim fnt As New Font("MS UI Gothic", 40)
266
+
267
+
268
+
269
+ Dim CellX As Integer = S.Cells(X, Y).Rectangle.X
270
+
271
+ Dim CellY As Integer = S.Cells(X, Y).Rectangle.Y
272
+
273
+ 'CellStatusによって表示を変更
274
+
275
+ Select Case S.Cells(X, Y).Status
276
+
277
+
278
+
279
+ __________中略____________
280
+
281
+
282
+
283
+ End Select
284
+
285
+ End Sub
286
+
287
+ End Class
288
+
289
+ ```
290
+
291
+ ```vb
292
+
293
+ Public Class Source
294
+
295
+
296
+
297
+ Public Const CellSize As Integer = 50 'セルのサイズ
298
+
299
+ Public Const XCount As Integer = 9 '盤の横方向のセル数
300
+
301
+ Public Const YCount As Integer = 9 '盤の縦方向のセル数
302
+
303
+ Dim aCell(XCount - 1, YCount - 1) As Cell '全セルを表す配列
304
+
305
+
306
+
307
+ Public Sub New()
308
+
309
+
310
+
311
+ Dim X As Integer
312
+
313
+ Dim Y As Integer
314
+
315
+
316
+
317
+ For X = 0 To XCount - 1
318
+
319
+ For Y = 0 To YCount - 1
320
+
321
+ aCell(X, Y) = New Cell(Me, New Point(X, Y))
322
+
323
+ Next
324
+
325
+ Next
326
+
327
+
328
+
329
+ End Sub
330
+
331
+
332
+
333
+ Public Sub Draw(ByVal g As Graphics)
334
+
335
+
336
+
337
+ Dim X As Integer
338
+
339
+ Dim Y As Integer
340
+
341
+ Dim aPen As New Pen(Color.Gray, 2)
342
+
343
+ Dim bPen As New Pen(Color.Black, 3)
344
+
345
+
346
+
347
+ '四角形
348
+
349
+ g.FillRectangle(Brushes.White, 0, 0, XCount * CellSize, YCount * CellSize)
350
+
351
+
352
+
353
+ '縦線
354
+
355
+ For X = 0 To XCount
356
+
357
+ If X Mod 3 = 0 Then
358
+
359
+ g.DrawLine(bPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
360
+
361
+ Else
362
+
363
+ g.DrawLine(aPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
364
+
365
+ End If
366
+
367
+
368
+
369
+ Next
370
+
371
+
372
+
373
+ '横線
374
+
375
+ For Y = 0 To YCount
376
+
377
+ If Y Mod 3 = 0 Then
378
+
379
+ g.DrawLine(bPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
380
+
381
+ Else
382
+
383
+ g.DrawLine(aPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
384
+
385
+ End If
386
+
387
+
388
+
389
+ Next
390
+
391
+
392
+
393
+ For Y = 0 To YCount - 1
394
+
395
+ For X = -0 To XCount - 1
396
+
397
+ Cells(X, Y).Draw(g)
398
+
399
+ Next
400
+
401
+ Next
402
+
403
+ End Sub
404
+
405
+
406
+
407
+ Public Property Cells(ByVal X As Integer, ByVal Y As Integer) As Cell
408
+
409
+ Get
410
+
411
+ Return aCell(X, Y)
412
+
413
+ End Get
414
+
415
+ Set(ByVal value As Cell)
416
+
417
+ aCell(X, Y) = value
418
+
419
+ End Set
420
+
421
+ End Property
422
+
423
+
424
+
425
+ Public Function CellFromPoint(ByVal X As Integer, ByVal Y As Integer) As Cell
426
+
427
+
110
428
 
111
429
  Dim ThisCell As Cell
112
430
 
113
431
 
114
432
 
115
- ThisCell = S.CellFromPoint(Pos.X, Pos.Y)
433
+ If X < 0 OrElse X >= XCount * CellSize Then
116
-
117
- S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Focus()
434
+
118
-
119
-
120
-
121
- PictureBox1.Invalidate()
435
+ Return Nothing
122
-
436
+
123
- End Sub
437
+ End If
124
-
125
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
438
+
126
-
127
- Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position)
439
+
128
-
129
- Dim ThisCell As Cell
440
+
130
-
131
- ThisCell = S.CellFromPoint(Pos.X, Pos.Y)
441
+ If Y < 0 OrElse Y >= YCount * CellSize Then
132
-
442
+
133
- Select Case e.KeyCode
443
+ Return Nothing
134
-
135
- Case Keys.D1, Keys.NumPad1
444
+
136
-
137
- __________中略____________
138
-
139
- End Select
140
-
141
- PictureBox1.Invalidate()
142
-
143
- End Sub
445
+ End If
144
-
446
+
447
+
448
+
145
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
449
+ ThisCell = Cells(X \ CellSize, Y \ CellSize)
146
-
147
- S.Reset()
450
+
148
-
149
- S.Easy()
451
+
150
-
452
+
151
- PictureBox1.Invalidate()
453
+ Return ThisCell
152
-
454
+
455
+
456
+
153
- End Sub
457
+ End Function
154
-
155
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
156
-
157
- S.Reset()
158
-
159
- S.Normal()
160
-
161
- PictureBox1.Invalidate()
162
-
163
- End Sub
164
-
165
-
166
-
167
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
168
-
169
- S.Reset()
170
-
171
- S.Hard()
172
-
173
- PictureBox1.Invalidate()
174
-
175
- End Sub
176
-
177
- Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
178
-
179
- S.Reset()
180
-
181
- PictureBox1.Invalidate()
182
-
183
-
184
-
185
- End Sub
186
-
187
-
188
-
189
-
190
458
 
191
459
  End Class
192
460
 
193
-
194
-
195
461
  ```
196
-
197
-
198
-
199
- ```vb
200
-
201
- Public Class Cell
202
-
203
- Public Status As CellStatus
204
-
205
- Public S As Source
206
-
207
- Public Position As Point '論理位置
208
-
209
- Public Rectangle As Rectangle '物理位置
210
-
211
- Public Focused As Boolean
212
-
213
-
214
-
215
-
216
-
217
- '■コンストラクタ
218
-
219
- Public Sub New(ByVal S As Source, ByVal Position As Point)
220
-
221
-
222
-
223
- Me.S = S
224
-
225
- Me.Position = Position
226
-
227
-
228
-
229
- '物理位置を求める。
230
-
231
- Dim Rect As New Rectangle
232
-
233
-
234
-
235
- '論理位置から物理位置を求めます。
236
-
237
- Rect.X = Position.X * Source.CellSize
238
-
239
- Rect.Y = Position.Y * Source.CellSize
240
-
241
- Rect.Width = Source.CellSize
242
-
243
- Rect.Height = Source.CellSize
244
-
245
-
246
-
247
- Me.Rectangle = Rect
248
-
249
- End Sub
250
-
251
-
252
-
253
- '■Focus
254
-
255
- Public Sub Focus()
256
-
257
-
258
-
259
- Dim X As Integer
260
-
261
- Dim Y As Integer
262
-
263
-
264
-
265
- '同じグリッドに属する自分以外のセルを非アクティブにする。
266
-
267
- For X = 0 To source.XCount - 1
268
-
269
- For Y = 0 To source.YCount - 1
270
-
271
- S.Cells(X, Y).Focused = False
272
-
273
- Next
274
-
275
- Next
276
-
277
-
278
-
279
- '自分自身をアクティブにする。
280
-
281
- Me.Focused = True
282
-
283
-
284
-
285
- End Sub
286
-
287
-
288
-
289
- Public Sub Draw(ByVal g As Graphics)
290
-
291
- Dim oPen As New Pen(Color.Orange, 3)
292
-
293
- Dim rPen As New Pen(Color.Red, 2)
294
-
295
- Dim blue = New SolidBrush(Color.AntiqueWhite)
296
-
297
- Dim pink = New SolidBrush(Color.Pink)
298
-
299
- Dim CellRect As Rectangle
300
-
301
-
302
-
303
- 'オレンジ枠に余裕を持たせる
304
-
305
- CellRect = Me.Rectangle
306
-
307
- CellRect.Inflate(-2, -2)
308
-
309
- Dim fnt As New Font("MS UI Gothic", 40)
310
-
311
- Dim CellRectNotFocus As Rectangle ' フォーカス外のマス
312
-
313
-
314
-
315
-
316
-
317
- 'フォーカスのある枠をオレンジで囲む
318
-
319
- If Me.Focused Then
320
-
321
- g.DrawRectangle(oPen, CellRect) 'オレンジ色の枠描画
322
-
323
- If S.Cells(Position.X, Position.Y).Status <> Nothing Then
324
-
325
- For X = 0 To Source.XCount - 1
326
-
327
- For Y = 0 To Source.YCount - 1
328
-
329
- CellRectNotFocus = S.Cells(X, Y).Rectangle
330
-
331
- CellRectNotFocus.Inflate(-3, -3)
332
-
333
-
334
-
335
- If Position.X = X And Position.Y = Y Then 'フォーカスセルの場合(条件追加)
336
-
337
- g.FillRectangle(pink, CellRectNotFocus) 'マスをピンクで塗る
338
-
339
- ElseIf Position.X = X Then
340
-
341
- g.FillRectangle(blue, CellRectNotFocus)
342
-
343
- Call InputNumber(g, X, Y) '数字入力
344
-
345
- ElseIf Position.Y = Y Then
346
-
347
- g.FillRectangle(blue, CellRectNotFocus)
348
-
349
- Call InputNumber(g, X, Y) '数字入力
350
-
351
- End If
352
-
353
- If S.Cells(Position.X, Position.Y).Status = S.Cells(X, Y).Status Then
354
-
355
- g.DrawRectangle(rPen, CellRectNotFocus) '赤色の枠描画
356
-
357
- End If
358
-
359
- Next
360
-
361
- Next
362
-
363
- End If
364
-
365
- End If
366
-
367
-
368
-
369
- 'CellStatusによって表示を変更
370
-
371
- Select Case Me.Status
372
-
373
- Case CellStatus._1
374
-
375
- g.DrawString(1, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
376
-
377
- Case CellStatus._2
378
-
379
- g.DrawString(2, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
380
-
381
- Case CellStatus._3
382
-
383
- g.DrawString(3, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
384
-
385
- Case CellStatus._4
386
-
387
- g.DrawString(4, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
388
-
389
- Case CellStatus._5
390
-
391
- g.DrawString(5, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
392
-
393
- Case CellStatus._6
394
-
395
- g.DrawString(6, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
396
-
397
- Case CellStatus._7
398
-
399
- g.DrawString(7, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
400
-
401
- Case CellStatus._8
402
-
403
- g.DrawString(8, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
404
-
405
- Case CellStatus._9
406
-
407
- g.DrawString(9, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
408
-
409
- Case CellStatus.Nothing
410
-
411
- g.DrawString(Nothing, fnt, Brushes.Black, Rectangle.X, Rectangle.Y)
412
-
413
- End Select
414
-
415
- End Sub
416
-
417
-
418
-
419
- 'cell(X,Y)のマスに数字を描画する
420
-
421
- Public Sub InputNumber(ByVal g As Graphics, ByVal X As Integer, ByVal Y As Integer)
422
-
423
- Dim fnt As New Font("MS UI Gothic", 40)
424
-
425
-
426
-
427
- Dim CellX As Integer = S.Cells(X, Y).Rectangle.X
428
-
429
- Dim CellY As Integer = S.Cells(X, Y).Rectangle.Y
430
-
431
- 'CellStatusによって表示を変更
432
-
433
- Select Case S.Cells(X, Y).Status
434
-
435
- __________中略____________
436
-
437
- End Select
438
-
439
- End Sub
440
-
441
- End Class
442
-
443
- ```
444
-
445
- ```vb
446
-
447
- Public Class Source
448
-
449
-
450
-
451
- Public Const CellSize As Integer = 50 'セルのサイズ
452
-
453
- Public Const XCount As Integer = 9 '盤の横方向のセル数
454
-
455
- Public Const YCount As Integer = 9 '盤の縦方向のセル数
456
-
457
- Dim aCell(XCount - 1, YCount - 1) As Cell '全セルを表す配列
458
-
459
-
460
-
461
- Public Sub New()
462
-
463
-
464
-
465
- Dim X As Integer
466
-
467
- Dim Y As Integer
468
-
469
-
470
-
471
- For X = 0 To XCount - 1
472
-
473
- For Y = 0 To YCount - 1
474
-
475
- aCell(X, Y) = New Cell(Me, New Point(X, Y))
476
-
477
- Next
478
-
479
- Next
480
-
481
-
482
-
483
- End Sub
484
-
485
-
486
-
487
- Public Sub Draw(ByVal g As Graphics)
488
-
489
-
490
-
491
- Dim X As Integer
492
-
493
- Dim Y As Integer
494
-
495
- Dim aPen As New Pen(Color.Gray, 2)
496
-
497
- Dim bPen As New Pen(Color.Black, 3)
498
-
499
-
500
-
501
- '四角形
502
-
503
- g.FillRectangle(Brushes.White, 0, 0, XCount * CellSize, YCount * CellSize)
504
-
505
-
506
-
507
- '縦線
508
-
509
- For X = 0 To XCount
510
-
511
- If X Mod 3 = 0 Then
512
-
513
- g.DrawLine(bPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
514
-
515
- Else
516
-
517
- g.DrawLine(aPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
518
-
519
- End If
520
-
521
-
522
-
523
- Next
524
-
525
-
526
-
527
- '横線
528
-
529
- For Y = 0 To YCount
530
-
531
- If Y Mod 3 = 0 Then
532
-
533
- g.DrawLine(bPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
534
-
535
- Else
536
-
537
- g.DrawLine(aPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
538
-
539
- End If
540
-
541
-
542
-
543
- Next
544
-
545
-
546
-
547
- For Y = 0 To YCount - 1
548
-
549
- For X = -0 To XCount - 1
550
-
551
- Cells(X, Y).Draw(g)
552
-
553
- Next
554
-
555
- Next
556
-
557
- End Sub
558
-
559
-
560
-
561
- Public Property Cells(ByVal X As Integer, ByVal Y As Integer) As Cell
562
-
563
- Get
564
-
565
- Return aCell(X, Y)
566
-
567
- End Get
568
-
569
- Set(ByVal value As Cell)
570
-
571
- aCell(X, Y) = value
572
-
573
- End Set
574
-
575
- End Property
576
-
577
-
578
-
579
- Public Function CellFromPoint(ByVal X As Integer, ByVal Y As Integer) As Cell
580
-
581
-
582
-
583
- Dim ThisCell As Cell
584
-
585
-
586
-
587
- If X < 0 OrElse X >= XCount * CellSize Then
588
-
589
- Return Nothing
590
-
591
- End If
592
-
593
-
594
-
595
- If Y < 0 OrElse Y >= YCount * CellSize Then
596
-
597
- Return Nothing
598
-
599
- End If
600
-
601
-
602
-
603
- ThisCell = Cells(X \ CellSize, Y \ CellSize)
604
-
605
-
606
-
607
- Return ThisCell
608
-
609
-
610
-
611
- End Function
612
-
613
- End Class
614
-
615
- ```

1

誤りを修正

2019/12/11 06:41

投稿

anpan___
anpan___

スコア28

test CHANGED
File without changes
test CHANGED
File without changes