[델파이]StringGrid의 셀에 색칠하기 예제
StringGrid의 셀에 색칠하기 예제
StringGrid의 1열은 녹색으로 표시하고
2행은 적색으로 표시하고 StringGrid의 1열과 2행 외의
다른 셀을 클릭하였을 때, 클릭한 셀을 청색으로 표시한다.
procedure TForm1.StringGrid1Click(Sender: TObject);
var
pntCurPos :TPoint;
iCol :integer;
iRow :integer;
begin
with StringGrid1 do
begin
pntCurPos := ScreenToClient(Mouse.CursorPos);
// 마우스가 위치가 어느 셀위에 있는지 정보얻기
MouseToCell(pntCurPos.x, pntCurPos.y, iCol, iRow);
// 셀 별 오브젝트가 설정되었는지 확인
// 오브젝트가 설정되어 있지 않을 경우 색을 오브젝트로 설정
if Assigned(Objects[iCol, iRow]) then
Objects[iCol, iRow] := nil
else
Objects[iCol, iRow] := TObject(clBlue);
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
// 셀 별 어브젝트가 설정되었는지 확인
if Assigned(Objects[ACol, ARow]) then
begin
// 색을 셀에 채우기
Canvas.Brush.Color := TColor(Objects[ACol, ARow]);
Canvas.FillRect(Rect);
end;
// 1열 녹색 채우기
if ACol = 1 then
begin
Canvas.Brush.Color := TColor(clGreen);
Canvas.FillRect(Rect);
end;
// 2행 적색 채우기
if ARow = 2 then
begin
Canvas.Brush.Color := TColor(clRed);
Canvas.FillRect(Rect);
end;
// 글자 표시
Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
end;
end;