在网格板上绘图

时间:2014-10-14 17:41:05

标签: vb.net

我正在做康威的生命游戏计划。我的电路板是二维数组,充满了被称为活着的物体。我画板:

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim cellSize As Size = New Size(10, 10)

    For x As Integer = 1 To board_width - 1
        For y As Integer = 1 To board_height - 1

            Dim cellLocation As Point = New Point(x * cellSize.Width - 10, y * cellSize.Height - 10)

            Dim cell As Rectangle = New Rectangle(cellLocation, cellSize)

            Using cellBrush As SolidBrush = If(board(x, y).alive, New SolidBrush(Color.FromArgb(0, 0, 0)), New SolidBrush(Color.FromArgb(255, 255, 255)))
                e.Graphics.FillRectangle(cellBrush, cell)
            End Using
        Next
    Next
End Sub

通过点击登录来改变一个单元格的状态。

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    pic_pos.X = ((e.X - 5) / 10) + 1
    pic_pos.Y = ((e.Y - 5) / 10) + 1
    board(pic_pos.X, pic_pos.Y).alive = Not board(pic_pos.X, pic_pos.Y).alive
    PictureBox1.Refresh()
End Sub

现在我想通过单击并按住鼠标按钮(如绘画中的绘图)来不断更改单元格状态,我完全不知道如何操作。任何帮助或建议? 抱歉英语不好

1 个答案:

答案 0 :(得分:1)

尝试使用MouseMove事件,检查按下了哪个按钮,并确保您的点仍然在数组的范围内:

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
  PictureBox1_MouseMove(sender, e)
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
  If e.Button = MouseButtons.Left Then
    pic_pos.X = ((e.X - 5) / 10) + 1
    pic_pos.Y = ((e.Y - 5) / 10) + 1
    If pic_pos.X >= 0 And pic_pos.X < board_width AndAlso _
       pic_pos.Y >= 0 And pic_pos.Y < board_width Then
      board(pic_pos.X, pic_pos.Y).alive = True
      PictureBox1.Invalidate()
    End If
  End If
End Sub