다음은 WordWrap기능을 지원하는 StringGrid의 소스입니다.
빈폼에, 소스를 집어넣고 컴파일 해봤는데.. 안되네요.
에잉~ O.o; 우째 쓰는기공~?
한번 붙여다 실행해보세요.
------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;
type
TWrapGrid = class(TStringGrid)
private
{Private declarations}
protected
{Protected declarations}
// DrawCell procedure는 그리드 셀의 문자를 wrap한다.
procedure DrawCell(ACol, ARow : Longint; ARect : TRect; AState :
TGridDrawState); override;
public
{Public declarations}
// Create procedure는 기본적인 DrawCell procedure를 생성한다.
constructor Create(AOwner : TComponent); override;
published
{Published declarations}
end;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor TWrapGrid.Create(AOwner : TComponent);
begin
// TStringGrid를 생성한다.
inherited Create(AOwner);
// 기본적인 DrawCell procedure를 생성한다.
DefaultDrawing := FALSE;
end;
// DrawCell procedure는 그리드 셀의 문자를 wrap한다.
procedure TWrapGrid.DrawCell(ACol, ARow : Longint; ARect : TRect;
AState : TGridDrawState);
var
Sentence, //What is left in the cell to output
CurWord : String; //The word we are currently outputting
SpacePos, //The position of the first space
CurX, //The x position of the 'cursor'
CurY : Integer; //The y position of the 'cursor'
EndOfSentence : Boolean;
// Whether or not we are done outputting the cell
begin
// Initialize the font to be the control's font
Canvas.Font := Font;
with Canvas do
begin
// If this is a fixed cell, then use the fixed color
if gdFixed in AState then
begin
Pen.Color := FixedColor;
Brush.Color := FixedColor;
end else
begin //else, use the normal color
Pen.Color := Color;
Brush.Color := Color;
end;
// Prepaint cell in cell color
Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
end;
// Start the drawing in the upper left corner of the cell
CurX := ARect.Left;
CurY := ARect.Top;
Sentence := Cells[ACol, ARow]; //Here we get the contents of the cell
// for each word in the cell
EndOfSentence := FALSE;
while (not EndOfSentence) do
begin
//to get the next word, we search for a space
SpacePos := Pos(' ', Sentence);
if SpacePos > 0 then
begin
//get the current word plus the space
CurWord := Copy(Sentence, 0, SpacePos);
//get the rest of the sentence
Sentence := Copy(Sentence, SpacePos + 1, Length(Sentence) -
SpacePos);
end else
begin
// this is the last word in the sentence
EndOfSentence := TRUE;
CurWord := Sentence;
end;
with Canvas do
begin
// if the text goes outside the boundary of the cell
if (TextWidth(CurWord) + CurX) > ARect.Right then
begin
//wrap to the next line
CurY := CurY + TextHeight(CurWord);
CurX := ARect.Left;
end;
//write out the word
TextOut(CurX, CurY, CurWord);
//increment the x position of the cursor
CurX := CurX + TextWidth(CurWord);
end;
end;
end;
var
WrapGrid1:TWrapGrid;
const
subj: array [0..3] of string[14]=('가나다라마바사', '가나다라마바사', '가나다라마바사', '가나다라마바사');
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
with WrapGrid1 do
begin
//FixedCols:=0;
//ColCount:=7;
//RowCount:=7;
for i:=0 to 3 do Cells[i, 0]:=subj[i];
end;
end;
end.