teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

不要なものを削除

2019/12/06 05:29

投稿

anpan___
anpan___

スコア28

title CHANGED
File without changes
body CHANGED
File without changes

1

全コードを記載

2019/12/06 05:29

投稿

anpan___
anpan___

スコア28

title CHANGED
File without changes
body CHANGED
@@ -16,10 +16,173 @@
16
16
  フォーカスのあるマス以外にもこの変化をつけようと思った場合どのようにすればよろしいでしょうか。
17
17
  ご教授いただければ幸いです。
18
18
 
19
+ ```VB
20
+ lass Form1
21
+ Dim S As New Source
19
22
 
20
23
 
24
+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
25
+ Me.KeyPreview = True
26
+ End Sub
27
+
28
+ Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
29
+
30
+ S.Draw(e.Graphics)
31
+
32
+ End Sub
33
+
34
+ Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
35
+ 'マウスの座標をPictureBox1のコントロール座標に変換する。
36
+ Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position)
37
+ Dim ThisCell As Cell
38
+
39
+ ThisCell = S.CellFromPoint(Pos.X, Pos.Y)
40
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Focus()
41
+
42
+ PictureBox1.Invalidate()
43
+ End Sub
44
+
45
+ Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
46
+ Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position)
47
+ Dim ThisCell As Cell
48
+ ThisCell = S.CellFromPoint(Pos.X, Pos.Y)
49
+ Select Case e.KeyCode
50
+ Case Keys.D1, Keys.NumPad1
51
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._1
52
+ Case Keys.D2, Keys.NumPad2
53
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._2
54
+ Case Keys.D3, Keys.NumPad3
55
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._3
56
+ Case Keys.D4, Keys.NumPad4
57
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._4
58
+ Case Keys.D5, Keys.NumPad5
59
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._5
60
+ Case Keys.D6, Keys.NumPad6
61
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._6
62
+ Case Keys.D7, Keys.NumPad7
63
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._7
64
+ Case Keys.D8, Keys.NumPad8
65
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._8
66
+ Case Keys.D9, Keys.NumPad9
67
+ S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._9
68
+ End Select
69
+ PictureBox1.Invalidate()
70
+ End Sub
71
+ End Class
72
+ ```
21
73
  ```VB
74
+ Public Class Source
75
+
76
+ Public Const CellSize As Integer = 50 'セルのサイズ
77
+ Public Const XCount As Integer = 9 '盤の横方向のセル数
78
+ Public Const YCount As Integer = 9 '盤の縦方向のセル数
79
+ Dim aCell(XCount - 1, YCount - 1) As Cell '全セルを表す配列
80
+
81
+ Public Sub New()
82
+
83
+ Dim X As Integer
84
+ Dim Y As Integer
85
+
86
+ For X = 0 To XCount - 1
87
+ For Y = 0 To YCount - 1
88
+ aCell(X, Y) = New Cell(Me, New Point(X, Y))
89
+ Next
90
+ Next
91
+
92
+ End Sub
93
+
94
+ Public Sub Draw(ByVal g As Graphics)
95
+
96
+ Dim X As Integer
97
+ Dim Y As Integer
98
+ Dim aPen As New Pen(Color.Gray, 2)
99
+ Dim bPen As New Pen(Color.Black, 3)
100
+
101
+ '四角形
102
+ g.FillRectangle(Brushes.White, 0, 0, XCount * CellSize, YCount * CellSize)
103
+
104
+ '縦線
105
+ For X = 0 To XCount
106
+ If X Mod 3 = 0 Then
107
+ g.DrawLine(bPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
108
+ Else
109
+ g.DrawLine(aPen, X * CellSize, 0, X * CellSize, YCount * CellSize)
110
+ End If
111
+
112
+ Next
113
+
114
+ '横線
115
+ For Y = 0 To YCount
116
+ If Y Mod 3 = 0 Then
117
+ g.DrawLine(bPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
118
+ Else
119
+ g.DrawLine(aPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize)
120
+ End If
121
+
122
+ Next
123
+
124
+ For Y = 0 To YCount - 1
125
+ For X = -0 To XCount - 1
126
+ Cells(X, Y).Draw(g)
127
+ Next
128
+ Next
129
+ End Sub
130
+
131
+ Public Property Cells(ByVal X As Integer, ByVal Y As Integer) As Cell
132
+ Get
133
+ Return aCell(X, Y)
134
+ End Get
135
+ Set(ByVal value As Cell)
136
+ aCell(X, Y) = value
137
+ End Set
138
+ End Property
139
+
140
+ Public Function CellFromPoint(ByVal X As Integer, ByVal Y As Integer) As Cell
141
+
142
+ Dim ThisCell As Cell
143
+
144
+ If X < 0 OrElse X >= XCount * CellSize Then
145
+ Return Nothing
146
+ End If
147
+
148
+ If Y < 0 OrElse Y >= YCount * CellSize Then
149
+ Return Nothing
150
+ End If
151
+
152
+ ThisCell = Cells(X \ CellSize, Y \ CellSize)
153
+
154
+ Return ThisCell
155
+
156
+ End Function
157
+ ```
158
+ ```VB
159
+ Public Class Cell
160
+ Public Status As CellStatus
161
+ Public S As Source
162
+ Public Position As Point '論理位置
163
+ Public Rectangle As Rectangle '物理位置
164
+ Public Focused As Boolean
165
+
166
+
167
+ '■コンストラクタ
168
+ Public Sub New(ByVal S As Source, ByVal Position As Point)
169
+
170
+ Me.S = S
171
+ Me.Position = Position
172
+
173
+ '物理位置を求める。
174
+ Dim Rect As New Rectangle
175
+
176
+ '論理位置から物理位置を求めます。
177
+ Rect.X = Position.X * Source.CellSize
178
+ Rect.Y = Position.Y * Source.CellSize
179
+ Rect.Width = Source.CellSize
180
+ Rect.Height = Source.CellSize
181
+
182
+ Me.Rectangle = Rect
183
+ End Sub
184
+
22
- '■Focus
185
+ '■Focus
23
186
  Public Sub Focus()
24
187
 
25
188
  Dim X As Integer
@@ -94,6 +257,5 @@
94
257
 
95
258
 
96
259
  End Sub
97
-
98
- コード
260
+ End Class
99
261
  ```