//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ int m, n, i, j;
int gridSize;
RichEdit1->Clear();
PageControl1->ActivePage = TabSheet1;
m = Edit1->Text.ToInt();
n = Edit2->Text.ToInt();
gridSize = Edit3->Text.ToInt();
StringGrid1->RowCount = m;
StringGrid1->ColCount = n;
StringGrid1->FixedCols= 2; // # of Column headers
StringGrid1->FixedRows= 2; // # of Row headers
StringGrid1->GridLineWidth = 1; StringGrid1->DefaultDrawing = false;
for (i=0; i<m; i++) StringGrid1->ColWidths[i] = gridSize;
for (i=0; i<n; i++) StringGrid1->RowHeights[i] = gridSize;
for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
{ StringGrid1->Cells[j][i] = rand() % 3;
RichEdit1->Lines->Add("Cell["+IntToStr(i)+", "+IntToStr(j)+"] = "+StringGrid1->Cells[j][i]);
if (CheckBox1->Checked)
{ Sleep(TrackBar1->Position);
StringGrid1->Refresh();
}
}
}
Button3->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{ String text = StringGrid1->Cells[ACol][ARow];
if (text == "0") StringGrid1->Canvas->Brush->Color = TColor RGB(250, 245, 135);
else if (text == "1") StringGrid1->Canvas->Brush->Color = TColor (RGB(176, 36, 132));
else if (text == "2") StringGrid1->Canvas->Brush->Color = TColor (RGB(180, 180, 80));
else StringGrid1->Canvas->Brush->Color = TColor RGB(50, 50, 50);
StringGrid1->Canvas->FillRect(Rect);
// StringGrid1->Canvas->TextRect(Rect, Rect.Left+6, Rect.Top+8, text);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{ int m, n, i, j, k;
PageControl1->ActivePage = TabSheet1;
m = Edit1->Text.ToInt();
n = Edit2->Text.ToInt();
if (CheckBox2) DoubleBuffered = true;
else DoubleBuffered = false;
// 畫面重新繪製時,會出現閃爍的狀況;在Panel或Form上設置DoubleBuffered的屬性為true即可
//Form1->DoubleBuffered = true;
for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
{ Sleep(TrackBar1->Position);
k = StringGrid1->Cells[j][i].ToInt();
StringGrid1->Cells[j][i] = 3;
StringGrid1->Refresh();
StringGrid1->Cells[j][i] = k;
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ int m, n, i, j;
RichEdit1->Clear();
PageControl1->ActivePage = TabSheet3;
m = Edit1->Text.ToInt();
n = Edit2->Text.ToInt();
StringGrid2->RowCount = m;
StringGrid2->ColCount = n;
StringGrid2->FixedCols= 0; // # of Column headers
StringGrid2->FixedRows= 0; // # of Row headers
StringGrid2->GridLineWidth = 1; StringGrid2->DefaultDrawing = false;
for (i=0; i<m; i++) StringGrid2->ColWidths[i] = 24;
for (i=0; i<n; i++) StringGrid2->RowHeights[i] = 24;
StringGrid2->Refresh();
if (CheckBox1->Checked)
{ for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
{ Sleep(TrackBar1->Position);
StringGrid2->Refresh();
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid2DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{ int rr = rand()%256, rg = rand()%256, rb = rand()%256;
RichEdit1->Lines->Add("(r, g, b) = ("+IntToStr(rr)+","+IntToStr(rg)+","+IntToStr(rb)+")");
// StringGrid2->Canvas->Brush->Color = static_cast <TColor>(RGB(rr, rg, rb));
StringGrid2->Canvas->Brush->Color = TColor (RGB(rr, rg, rb));
StringGrid2->Canvas->FillRect(Rect);
}
//---------------------------------------------------------------------------