現在VBで数独を作成させてもらっております。
その中で、フォーカスのあるマスの色を変化させるようにしておりますが、追加して
【フォーカス中のセルと】
・同じ数字の枠を赤で囲む
・同じX/Y軸また、3×3のマスの背景色をうっすら変える
を行いたいと考えています。
今回詰まっているのは、上記の両点において、
フォーカス中の枠にだけしか、行いたいエフェクトが反映されないことです。
また、3×3のマスを取得する処理の作り方にも悩んでおります。
一つ目の問題に関しては、デバッグを行ったところ正常に分岐分には入り、DrawRectangleとFillRectangleは処理されております。
フォーカスのあるマス以外にもこの変化をつけようと思った場合どのようにすればよろしいでしょうか。
ご教授いただければ幸いです。
VB
1lass Form1 2 Dim S As New Source 3 4 5 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 6 Me.KeyPreview = True 7 End Sub 8 9 Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint 10 11 S.Draw(e.Graphics) 12 13 End Sub 14 15 Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click 16 'マウスの座標をPictureBox1のコントロール座標に変換する。 17 Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position) 18 Dim ThisCell As Cell 19 20 ThisCell = S.CellFromPoint(Pos.X, Pos.Y) 21 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Focus() 22 23 PictureBox1.Invalidate() 24 End Sub 25 26 Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 27 Dim Pos As Point = PictureBox1.PointToClient(Cursor.Position) 28 Dim ThisCell As Cell 29 ThisCell = S.CellFromPoint(Pos.X, Pos.Y) 30 Select Case e.KeyCode 31 Case Keys.D1, Keys.NumPad1 32 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._1 33 Case Keys.D2, Keys.NumPad2 34 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._2 35 Case Keys.D3, Keys.NumPad3 36 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._3 37 Case Keys.D4, Keys.NumPad4 38 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._4 39 Case Keys.D5, Keys.NumPad5 40 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._5 41 Case Keys.D6, Keys.NumPad6 42 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._6 43 Case Keys.D7, Keys.NumPad7 44 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._7 45 Case Keys.D8, Keys.NumPad8 46 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._8 47 Case Keys.D9, Keys.NumPad9 48 S.Cells(ThisCell.Position.X, ThisCell.Position.Y).Status = CellStatus._9 49 End Select 50 PictureBox1.Invalidate() 51 End Sub 52End Class
VB
1Public Class Source 2 3 Public Const CellSize As Integer = 50 'セルのサイズ 4 Public Const XCount As Integer = 9 '盤の横方向のセル数 5 Public Const YCount As Integer = 9 '盤の縦方向のセル数 6 Dim aCell(XCount - 1, YCount - 1) As Cell '全セルを表す配列 7 8 Public Sub New() 9 10 Dim X As Integer 11 Dim Y As Integer 12 13 For X = 0 To XCount - 1 14 For Y = 0 To YCount - 1 15 aCell(X, Y) = New Cell(Me, New Point(X, Y)) 16 Next 17 Next 18 19 End Sub 20 21 Public Sub Draw(ByVal g As Graphics) 22 23 Dim X As Integer 24 Dim Y As Integer 25 Dim aPen As New Pen(Color.Gray, 2) 26 Dim bPen As New Pen(Color.Black, 3) 27 28 '四角形 29 g.FillRectangle(Brushes.White, 0, 0, XCount * CellSize, YCount * CellSize) 30 31 '縦線 32 For X = 0 To XCount 33 If X Mod 3 = 0 Then 34 g.DrawLine(bPen, X * CellSize, 0, X * CellSize, YCount * CellSize) 35 Else 36 g.DrawLine(aPen, X * CellSize, 0, X * CellSize, YCount * CellSize) 37 End If 38 39 Next 40 41 '横線 42 For Y = 0 To YCount 43 If Y Mod 3 = 0 Then 44 g.DrawLine(bPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize) 45 Else 46 g.DrawLine(aPen, 0, Y * CellSize, XCount * CellSize, Y * CellSize) 47 End If 48 49 Next 50 51 For Y = 0 To YCount - 1 52 For X = -0 To XCount - 1 53 Cells(X, Y).Draw(g) 54 Next 55 Next 56 End Sub 57 58 Public Property Cells(ByVal X As Integer, ByVal Y As Integer) As Cell 59 Get 60 Return aCell(X, Y) 61 End Get 62 Set(ByVal value As Cell) 63 aCell(X, Y) = value 64 End Set 65 End Property 66 67 Public Function CellFromPoint(ByVal X As Integer, ByVal Y As Integer) As Cell 68 69 Dim ThisCell As Cell 70 71 If X < 0 OrElse X >= XCount * CellSize Then 72 Return Nothing 73 End If 74 75 If Y < 0 OrElse Y >= YCount * CellSize Then 76 Return Nothing 77 End If 78 79 ThisCell = Cells(X \ CellSize, Y \ CellSize) 80 81 Return ThisCell 82 83 End Function
VB
1Public Class Cell 2 Public Status As CellStatus 3 Public S As Source 4 Public Position As Point '論理位置 5 Public Rectangle As Rectangle '物理位置 6 Public Focused As Boolean 7 8 9 '■コンストラクタ 10 Public Sub New(ByVal S As Source, ByVal Position As Point) 11 12 Me.S = S 13 Me.Position = Position 14 15 '物理位置を求める。 16 Dim Rect As New Rectangle 17 18 '論理位置から物理位置を求めます。 19 Rect.X = Position.X * Source.CellSize 20 Rect.Y = Position.Y * Source.CellSize 21 Rect.Width = Source.CellSize 22 Rect.Height = Source.CellSize 23 24 Me.Rectangle = Rect 25 End Sub 26 27 '■Focus 28 Public Sub Focus() 29 30 Dim X As Integer 31 Dim Y As Integer 32 33 '同じグリッドに属する自分以外のセルを非アクティブにする。 34 For X = 0 To source.XCount - 1 35 For Y = 0 To source.YCount - 1 36 S.Cells(X, Y).Focused = False 37 Next 38 Next 39 40 '自分自身をアクティブにする。 41 Me.Focused = True 42 43 End Sub 44 45 Public Sub Draw(ByVal g As Graphics) 46 Dim oPen As New Pen(Color.Orange, 3) 47 Dim rPen As New Pen(Color.Red, 2) 48 Dim b = New SolidBrush(Color.AntiqueWhite) 49 Dim CellRect As Rectangle 50 51 'オレンジ枠に余裕を持たせる 52 CellRect = Me.Rectangle 53 CellRect.Inflate(-2, -2) 54 Dim fnt As New Font("MS UI Gothic", 40) 55 56 57 'フォーカスのある枠をオレンジで囲む 58 If Me.Focused Then 59 g.DrawRectangle(oPen, CellRect) 60 If S.Cells(Position.X, Position.Y).Status <> Nothing Then 61 For X = 0 To Source.XCount - 1 62 For Y = 0 To Source.YCount - 1 63 If S.Cells(Position.X, Position.Y).Status = S.Cells(X, Y).Status Then 64 g.DrawRectangle(rPen, CellRect) 65 End If 66 If Position.X = X Then 67 g.FillRectangle(b, CellRect) 68 ElseIf Position.Y = Y Then 69 g.DrawRectangle(rPen, CellRect) 70 End If 71 Next 72 Next 73 End If 74 End If 75 76 'CellStatusによって表示を変更 77 Select Case Me.Status 78 Case CellStatus._1 79 g.DrawString(1, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 80 Case CellStatus._2 81 g.DrawString(2, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 82 Case CellStatus._3 83 g.DrawString(3, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 84 Case CellStatus._4 85 g.DrawString(4, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 86 Case CellStatus._5 87 g.DrawString(5, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 88 Case CellStatus._6 89 g.DrawString(6, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 90 Case CellStatus._7 91 g.DrawString(7, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 92 Case CellStatus._8 93 g.DrawString(8, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 94 Case CellStatus._9 95 g.DrawString(9, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 96 Case CellStatus.Nothing 97 g.DrawString(Nothing, fnt, Brushes.Black, Rectangle.X, Rectangle.Y) 98 End Select 99 100 101 End Sub 102End Class
回答1件
あなたの回答
tips
プレビュー