본문 바로가기
Delphi Tip/그리드

델파이 스트링그리드(StringGrid) 셀 정렬하기

by MonoSoft 2021. 9. 16.
728x90
반응형

델파이 스트링그리드(StringGrid) 셀 정렬하기

 

 

스트링그리드의 셀을 원하시는 데로 정렬하실 수 있습니다.

스트링그리드의 OnDrawCell 이벤트를 원하시는 정렬방식으로 코딩해 주세요.

 

// ***********************************************************************

// 왼쪽 정렬 *************************************************************

// ***********************************************************************

 

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;

Rect: TRect; State: TGridDrawState);

begin

with TStringGrid(Sender).Canvas do

begin

FillRect(Rect);

TextOut(Rect.Left + 2, Rect.Top+2, TStringGrid(Sender).Cells[ACol, ARow]);

end;

end;

 

// ***********************************************************************

// 가운데 정렬 ***********************************************************

// ***********************************************************************

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;

Rect: TRect; State: TGridDrawState);

var

LeftPos: Integer;

CellStr: string;

begin

with TStringGrid(Sender).Canvas do

begin

CellStr := TStringGrid(Sender).Cells[ACol, ARow];

LeftPos := ((Rect.Right - Rect.Left -

TStringGrid(Sender).Canvas.TextWidth(CellStr)) div 2) + Rect.Left;

FillRect(Rect);

TextOut(LeftPos, Rect.Top+2, CellStr);

end;

end;

 

// ***********************************************************************

// 오른쪽 정렬 ***********************************************************

// ***********************************************************************

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;

Rect: TRect; State: TGridDrawState);

var

LeftPos: Integer;

CellStr: string;

begin

with TStringGrid(Sender).Canvas do

begin

CellStr := TStringGrid(Sender).Cells[ACol, ARow];

LeftPos := Rect.Right - TStringGrid(Sender).Canvas.TextWidth(CellStr);

FillRect(Rect);

TextOut(LeftPos-2, Rect.Top+2, CellStr);

end;

end;

 

728x90
반응형

댓글