執行檔+迷宮文字檔下載 (解壓後有 Proj_RMatrix_WStringGrid.exe 與一8*8迷宮測試~.txt)
// 讀入0/1陣列
int maze [100][100];
int m, n;
void __fastcall TForm1::Button1Click(TObject *Sender)
{ FILE *fp;
AnsiString out, fname;
int i, j;
if (OpenDialog1->Execute())
{ fname = OpenDialog1->FileName;
fp = fopen(fname.c_str(), "r+"); // c_str函数的傳回值是 const char *, 即把 string 轉成 const char *
fscanf(fp, "%d %d", &m, &n); // Reda in two integers m & n
Memo1->Lines->Add("m="+IntToStr(m));
Memo1->Lines->Add("n="+IntToStr(n));
for (i=1; i<=m; i++) // Reda in m*n 0/1's into maze[][]
for (j=1; j<=n; j++)
fscanf(fp, "%d", &maze[i][j]);
for (i=1; i<=m; i++) // Print out maze[][] in Memo1
{ out = "";
for (j=1; j<=n; j++)
out += " "+IntToStr(maze[i][j]);
Memo1->Lines->Add(out);
}
// Print out maze[][] in StringGrid1
StringGrid1->RowCount = m+1;
StringGrid1->ColCount = n+1;
for (i=1; i<=m; i++)
{ for (j=1; j<=n; j++)
{ StringGrid1->Cells[j][i] = maze[i][j];
}
}
fclose(fp);
}
else Memo1->Lines->Add("Nothing happens.");
}
// 寫入 StringGrid1 中
// 設定 StringGrid2~5
void __fastcall TForm1::Button2Click(TObject *Sender)
{ int i, j;
StringGrid2->RowCount = m;
StringGrid2->ColCount = n;
StringGrid2->FixedCols=0;
StringGrid2->FixedRows=0;
for (i=0; i<m; i++)
{ StringGrid2->ColWidths[i] = 24;
StringGrid2->RowHeights[i] = 24;
}
for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
{ StringGrid2->Cells[j][i] = StringGrid1->Cells[j+1][i+1];
}
}
StringGrid3->RowCount = m;
StringGrid3->ColCount = n;
StringGrid3->FixedCols=0;
StringGrid3->FixedRows=0;
for (i=0; i<m; i++)
{ StringGrid3->ColWidths[i] = 24;
StringGrid3->RowHeights[i] = 24;
}
StringGrid4->RowCount = m;
StringGrid4->ColCount = n;
StringGrid4->FixedCols=0;
StringGrid4->FixedRows=0;
for (i=0; i<m; i++)
{ StringGrid4->ColWidths[i] = 24;
StringGrid4->RowHeights[i] = 24;
}
StringGrid5->RowCount = m;
StringGrid5->ColCount = n;
StringGrid5->FixedCols=0;
StringGrid5->FixedRows=0;
StringGrid5->GridLineWidth = 0;
for (i=0; i<m; i++)
{ StringGrid5->ColWidths[i] = 24;
StringGrid5->RowHeights[i] = 24;
}
}
// 顯示迷宮 in StringGrid2
// 以下對不同的 StringGrid 做 OnDrawCell 事件的設定,以產生不同的顯示結果!
void __fastcall TForm1::StringGrid2DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
if ( State.Contains(gdFixed) ) // 設定 column/row headers 的顏色 (這個例子沒用到)
{ StringGrid2->Canvas->Brush->Color = static_cast<TColor>(RGB(255, 155, 0));
StringGrid2->Canvas->Font->Style = TFontStyles() << fsBold;
StringGrid2->Canvas->Font->Color = static_cast<TColor>(RGB(250, 245, 135));
StringGrid2->Canvas->Rectangle(Rect);
}
else if( State.Contains(gdSelected) ) // 設定 所選定 cells 的顏色 (這個例子是 (0,0) 那個橘色 cell )
{ StringGrid2->Canvas->Brush->Color = static_cast<TColor>(RGB(255, 205, 155));
StringGrid2->Canvas->Font->Style = TFontStyles() >> fsBold;
StringGrid2->Canvas->Font->Color = clNavy;
StringGrid2->Canvas->FillRect(Rect);
}
else { StringGrid2->Canvas->Brush->Color = clWhite; // 設定其它 cells 的顏色
StringGrid2->Canvas->Font->Color = clBlue;
StringGrid2->Canvas->FillRect(Rect);
}
AnsiString text = StringGrid2->Cells[ACol][ARow];
StringGrid2->Canvas->TextRect(Rect, Rect.Left, Rect.Top, text);
}
// ----------------------------------------------------------------------
// 顯示迷宮 in StringGrid3 (0/1 + background color)
void __fastcall TForm1::StringGrid3DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{ AnsiString text = StringGrid2->Cells[ACol][ARow];
if (text == "0")
{ StringGrid3->Canvas->Brush->Color = static_cast<TColor>(RGB(250, 245, 135));
}
else { StringGrid3->Canvas->Brush->Color = static_cast<TColor>(RGB(80, 80, 80));
}
StringGrid3->Canvas->FillRect(Rect);
StringGrid3->Canvas->TextRect(Rect, Rect.Left+6, Rect.Top+8, text);
}
//---------------------------------------------------------------------------
// 顯示迷宮 in StringGrid4 (background color)
void __fastcall TForm1::StringGrid4DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
AnsiString text = StringGrid2->Cells[ACol][ARow];
if (text == "0") StringGrid4->Canvas->Brush->Color = RGB(250, 245, 135) ;
else StringGrid4->Canvas->Brush->Color = RGB(80, 80, 80); // (RGB(200, 205, 155));
StringGrid4->Canvas->FillRect(Rect);
}
//---------------------------------------------------------------------------
// 顯示迷宮 in StringGrid5 (background color without GridLine)
void __fastcall TForm1::StringGrid5DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
AnsiString text = StringGrid2->Cells[ACol][ARow];
if (text == "0") StringGrid5->Canvas->Brush->Color = RGB(200, 205, 155) ;
else StringGrid5->Canvas->Brush->Color = RGB(80, 80, 80); // (RGB(200, 205, 155));
StringGrid5->Canvas->FillRect(Rect);
}
// --------------------------------------------------------------------------------
// OnDrawCell 事件可在 StringGrid 選定後如下設定