>> 질문 : 김성수님 (
bcbdn C++Builder Q&A 5897번 ))
"StringGrid에서 각 Column에서 문자열을 정렬할 수 있는방법 좀 갈켜주세요
다른 grid에는 ColumnAlign 프로퍼티가 있다는데..... "
>> 답변 : 박지훈.임프
임펠리테리입니다.
참 오랜만에 보는 질문이군요. 몇년전엔 잊을만하면 다시 질문을 받던, 정말 자주 질문받던 내용이거든요.
말씀하신 대로, 디비그리드에서는 각 컬럼 프로퍼티에 ColumnAlign 프로퍼티가 있어서 간단히 정렬을
구현 할 수 있습니다만 스트링그리드의 경우에는 정렬 기능을 전혀 지원하지 못하므로 OnDrawCell
이벤트의 핸들러에서 직접 그려주는 수밖에 없습니다.
다음은 스트링그리드의 모든 셀을 가운데 정렬로 나타나게 하는 예입니다.
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
unsigned oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_CENTER);
StringGrid1->Canvas->TextRect(Rect, (Rect.Right+Rect.Left)/2, Rect.Top+2,
StringGrid1->Cells[ACol][ARow]);
SetTextAlign(StringGrid1->Canvas->Handle, oldalign);
}
다음은 모든 셀을 오른쪽 정렬하는 예입니다.
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
unsigned oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_RIGHT);
StringGrid1->Canvas->TextRect(Rect, Rect.Right-2, Rect.Top+2,
StringGrid1->Cells[ACol][ARow]);
SetTextAlign(StringGrid1->Canvas->Handle, oldalign);
}
실제로 프로그램을 작성하실 때는, 아마도 셀의 컬럼에 따라서 왼쪽/가운데/오른쪽 정렬을 선택적으로
해야 하겠지요? 이런 경우에는 이벤트 핸들러로 넘어온 ACol 파라미터를 검사해서 선택적으로 그려주면
됩니다.