質問編集履歴

2

不要なものを削除

2019/12/06 05:29

投稿

anpan___
anpan___

スコア28

test CHANGED
File without changes
test CHANGED
File without changes

1

全コードを記載

2019/12/06 05:29

投稿

anpan___
anpan___

スコア28

test CHANGED
File without changes
test CHANGED
@@ -34,13 +34,339 @@
34
34
 
35
35
 
36
36
 
37
-
38
-
39
-
40
-
41
37
  ```VB
42
38
 
39
+ lass Form1
40
+
41
+ Dim S As New Source
42
+
43
+
44
+
45
+
46
+
47
+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
48
+
49
+ Me.KeyPreview = True
50
+
51
+ End Sub
52
+
53
+
54
+
55
+ Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
56
+
57
+
58
+
59
+ S.Draw(e.Graphics)
60
+
61
+
62
+
63
+ End Sub
64
+
65
+
66
+
67
+ Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
68
+
69
+ 'マウスの座標をPictureBox1のコントロール座標に変換する。
70
+
71
+ Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position)
72
+
73
+ Dim ThisCell As Cell
74
+
75
+
76
+
77
+ ThisCell = S.CellFromPoint(Pos.X, Pos.Y)
78
+
79
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Focus()
80
+
81
+
82
+
83
+ PictureBox1.Invalidate()
84
+
85
+ End Sub
86
+
87
+
88
+
89
+ Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
90
+
91
+ Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position)
92
+
93
+ Dim ThisCell As Cell
94
+
95
+ ThisCell = S.CellFromPoint(Pos.X, Pos.Y)
96
+
97
+ Select Case e.KeyCode
98
+
99
+ Case Keys.D1, Keys.NumPad1
100
+
101
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._1
102
+
103
+ Case Keys.D2, Keys.NumPad2
104
+
105
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._2
106
+
107
+ Case Keys.D3, Keys.NumPad3
108
+
109
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._3
110
+
111
+ Case Keys.D4, Keys.NumPad4
112
+
113
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._4
114
+
115
+ Case Keys.D5, Keys.NumPad5
116
+
117
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._5
118
+
119
+ Case Keys.D6, Keys.NumPad6
120
+
121
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._6
122
+
123
+ Case Keys.D7, Keys.NumPad7
124
+
125
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._7
126
+
127
+ Case Keys.D8, Keys.NumPad8
128
+
129
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._8
130
+
131
+ Case Keys.D9, Keys.NumPad9
132
+
133
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._9
134
+
135
+ End Select
136
+
137
+ PictureBox1.Invalidate()
138
+
139
+ End Sub
140
+
141
+ End Class
142
+
143
+ ```
144
+
145
+ ```VB
146
+
147
+ Public Class Source
148
+
149
+
150
+
151
+ Public Const CellSize As Integer = 50 'セルのサイズ
152
+
153
+ Public Const XCount As Integer = 9 '盤の横方向のセル数
154
+
155
+ Public Const YCount As Integer = 9 '盤の縦方向のセル数
156
+
157
+ Dim aCell(XCount - 1, YCount - 1) As Cell '全セルを表す配列
158
+
159
+
160
+
161
+ Public Sub New()
162
+
163
+
164
+
165
+ Dim X As Integer
166
+
167
+ Dim Y As Integer
168
+
169
+
170
+
171
+ For X = 0 To XCount - 1
172
+
173
+ For Y = 0 To YCount - 1
174
+
175
+ aCell(X, Y) = New Cell(Me, New Point(X, Y))
176
+
177
+ Next
178
+
179
+ Next
180
+
181
+
182
+
183
+ End Sub
184
+
185
+
186
+
187
+ Public Sub Draw(ByVal g As Graphics)
188
+
189
+
190
+
191
+ Dim X As Integer
192
+
193
+ Dim Y As Integer
194
+
195
+ Dim aPen As New Pen(Color.Gray, 2)
196
+
197
+ Dim bPen As New Pen(Color.Black, 3)
198
+
199
+
200
+
201
+ '四角形
202
+
203
+ g.FillRectangle(Brushes.White, 0, 0, XCount * CellSize, YCount * CellSize)
204
+
205
+
206
+
207
+ '縦線
208
+
209
+ For X = 0 To XCount
210
+
211
+ If X Mod 3 = 0 Then
212
+
213
+ g.DrawLine(bPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
214
+
215
+ Else
216
+
217
+ g.DrawLine(aPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
218
+
219
+ End If
220
+
221
+
222
+
223
+ Next
224
+
225
+
226
+
227
+ '横線
228
+
229
+ For Y = 0 To YCount
230
+
231
+ If Y Mod 3 = 0 Then
232
+
233
+ g.DrawLine(bPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
234
+
235
+ Else
236
+
237
+ g.DrawLine(aPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
238
+
239
+ End If
240
+
241
+
242
+
243
+ Next
244
+
245
+
246
+
247
+ For Y = 0 To YCount - 1
248
+
249
+ For X = -0 To XCount - 1
250
+
251
+ Cells(X, Y).Draw(g)
252
+
253
+ Next
254
+
255
+ Next
256
+
257
+ End Sub
258
+
259
+
260
+
261
+ Public Property Cells(ByVal X As Integer, ByVal Y As Integer) As Cell
262
+
263
+ Get
264
+
265
+ Return aCell(X, Y)
266
+
267
+ End Get
268
+
269
+ Set(ByVal value As Cell)
270
+
271
+ aCell(X, Y) = value
272
+
273
+ End Set
274
+
275
+ End Property
276
+
277
+
278
+
279
+ Public Function CellFromPoint(ByVal X As Integer, ByVal Y As Integer) As Cell
280
+
281
+
282
+
283
+ Dim ThisCell As Cell
284
+
285
+
286
+
287
+ If X < 0 OrElse X >= XCount * CellSize Then
288
+
289
+ Return Nothing
290
+
291
+ End If
292
+
293
+
294
+
295
+ If Y < 0 OrElse Y >= YCount * CellSize Then
296
+
297
+ Return Nothing
298
+
299
+ End If
300
+
301
+
302
+
303
+ ThisCell = Cells(X \ CellSize, Y \ CellSize)
304
+
305
+
306
+
307
+ Return ThisCell
308
+
309
+
310
+
311
+ End Function
312
+
313
+ ```
314
+
315
+ ```VB
316
+
317
+ Public Class Cell
318
+
319
+ Public Status As CellStatus
320
+
321
+ Public S As Source
322
+
323
+ Public Position As Point '論理位置
324
+
325
+ Public Rectangle As Rectangle '物理位置
326
+
327
+ Public Focused As Boolean
328
+
329
+
330
+
331
+
332
+
333
+ '■コンストラクタ
334
+
335
+ Public Sub New(ByVal S As Source, ByVal Position As Point)
336
+
337
+
338
+
339
+ Me.S = S
340
+
341
+ Me.Position = Position
342
+
343
+
344
+
345
+ '物理位置を求める。
346
+
347
+ Dim Rect As New Rectangle
348
+
349
+
350
+
351
+ '論理位置から物理位置を求めます。
352
+
353
+ Rect.X = Position.X * Source.CellSize
354
+
355
+ Rect.Y = Position.Y * Source.CellSize
356
+
357
+ Rect.Width = Source.CellSize
358
+
359
+ Rect.Height = Source.CellSize
360
+
361
+
362
+
363
+ Me.Rectangle = Rect
364
+
365
+ End Sub
366
+
367
+
368
+
43
- '■Focus
369
+ '■Focus
44
370
 
45
371
  Public Sub Focus()
46
372
 
@@ -190,8 +516,6 @@
190
516
 
191
517
  End Sub
192
518
 
193
-
194
-
195
- コード
519
+ End Class
196
520
 
197
521
  ```