VERSION 5.00
Begin VB.Form frmGrid 
   Caption         =   "Grid Example"
   ClientHeight    =   4935
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   5340
   LinkTopic       =   "Form1"
   ScaleHeight     =   4935
   ScaleWidth      =   5340
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdDrawGrid 
      Caption         =   "Draw Grid"
      Height          =   495
      Left            =   1770
      TabIndex        =   1
      Top             =   3960
      Width           =   1215
   End
   Begin VB.PictureBox picGrid 
      Appearance      =   0  'Flat
      AutoRedraw      =   -1  'True
      BackColor       =   &H80000005&
      BorderStyle     =   0  'None
      FillStyle       =   0  'Solid
      ForeColor       =   &H80000008&
      Height          =   3165
      Left            =   390
      ScaleHeight     =   211
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   260
      TabIndex        =   0
      Top             =   330
      Width           =   3900
   End
End
Attribute VB_Name = "frmGrid"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Const CellSize As Long = 16
Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Private SampleGrid(9, 9) As Long

Private Sub cmdDrawGrid_Click()
  Dim lngCol As Long
  Dim lngRow As Long
  Dim lngColor As Long
  Dim X1 As Long, Y1 As Long
  
  For lngRow = 0 To 9
    Y1 = lngRow * CellSize
    For lngCol = 0 To 9
      X1 = lngCol * CellSize
      SampleGrid(lngRow, lngCol) = Int(Rnd * 3)
      Select Case SampleGrid(lngRow, lngCol)
      Case 0
        lngColor = vbYellow
      Case 1
        lngColor = vbGreen
      Case 2
        lngColor = vbRed
      End Select
      picGrid.FillColor = lngColor
      Call Rectangle(picGrid.hdc, X1, Y1, X1 + CellSize + 1, Y1 + CellSize + 1)
    Next lngCol
  Next lngRow
  picGrid.Refresh
End Sub

Private Sub Form_Load()
  Randomize
  cmdDrawGrid.Value = True
End Sub

Private Sub picGrid_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Dim lngCol As Long
  Dim lngRow As Long
  Dim lngColor As Long
  Dim X1 As Long, Y1 As Long
  
  If X < 0 Or Y < 0 Then Exit Sub
  lngCol = X \ CellSize
  lngRow = Y \ CellSize
  If lngCol > 9 Or lngRow > 9 Then Exit Sub
  Y1 = lngRow * CellSize
  X1 = lngCol * CellSize
  SampleGrid(lngRow, lngCol) = (SampleGrid(lngRow, lngCol) + 1) Mod 3
  Select Case SampleGrid(lngRow, lngCol)
  Case 0
    lngColor = vbYellow
  Case 1
    lngColor = vbGreen
  Case 2
    lngColor = vbRed
  End Select
  picGrid.FillColor = lngColor
  Call Rectangle(picGrid.hdc, X1, Y1, X1 + CellSize + 1, Y1 + CellSize + 1)
  picGrid.Refresh
End Sub
