1.在DrawGrid物件中, 點選OnKeyDown事件進入編輯
switch (Key)
{
case VK_LEFT: //鍵盤:左方向鍵
//ShowMessage("left");
if (mouseposCol-1>=0) { //判斷 mouse 有無超出界線
if (mark[mouseposRow][mouseposCol-1]==0) { //判斷下一步是否可以走
mark[mouseposRow][mouseposCol-1] = 3;
mark[mouseposRow][mouseposCol] = 0;
DrawGrid2->Canvas->FillRect(DrawGrid2->CellRect(mouseposCol,mouseposRow));
mouseposCol = mouseposCol-1;
DrawGrid2->Canvas->CopyRect(DrawGrid2->CellRect(mouseposCol,mouseposRow), bmp->Canvas ,R1);
}
}
break;
case VK_RIGHT: //鍵盤:右方向鍵
//ShowMessage("right");
if (mouseposCol+1<g_Width) { //判斷 mouse 有無超出界線
if (mark[mouseposRow][mouseposCol+1]==0) { //判斷下一步是否可以走
mark[mouseposRow][mouseposCol+1] = 3;
mark[mouseposRow][mouseposCol] = 0;
DrawGrid2->Canvas->FillRect(DrawGrid2->CellRect(mouseposCol,mouseposRow));
mouseposCol = mouseposCol+1;
DrawGrid2->Canvas->CopyRect(DrawGrid2->CellRect(mouseposCol,mouseposRow), bmp->Canvas ,R1);
}
}
break;
case VK_UP: //鍵盤:上方向鍵
//ShowMessage("up");
if (mark[mouseposRow-1][mouseposCol]==0) { //判斷下一步是否可以走
mark[mouseposRow-1][mouseposCol] = 3;
mark[mouseposRow][mouseposCol] = 0;
DrawGrid2->Canvas->FillRect(DrawGrid2->CellRect(mouseposCol,mouseposRow));
mouseposRow = mouseposRow-1;
DrawGrid2->Canvas->CopyRect(DrawGrid2->CellRect(mouseposCol,mouseposRow), bmp->Canvas ,R1);
}
break;
case VK_DOWN: //鍵盤:下方向鍵
//ShowMessage("down");
if (mark[mouseposRow+1][mouseposCol]==0) { //判斷下一步是否可以走
mark[mouseposRow+1][mouseposCol] = 3;
mark[mouseposRow][mouseposCol] = 0;
DrawGrid2->Canvas->FillRect(DrawGrid2->CellRect(mouseposCol,mouseposRow));
mouseposRow = mouseposRow+1;
DrawGrid2->Canvas->CopyRect(DrawGrid2->CellRect(mouseposCol,mouseposRow), bmp->Canvas ,R1);
}
break;
}