ここで述べたように、実際には何もペイントしないフラグを指定してDrawText関数を呼び出すことで取得できます。DT_CALCRECT
適切な長方形を計算し、それを変数に返しますR
。
procedure TForm1.Button1Click(Sender: TObject);
var
R: TRect;
S: String;
begin
R := Rect(0, 0, 20, 20);
S := 'What might be the new high of this text ?';
DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
ShowMessage('New height might be '+IntToStr(R.Bottom - R.Top)+' px');
end;
つまり、次の例を使用して2回呼び出すと、ラップされたテキストが描画されます。これは、の最初の呼び出しでDT_CALCRECT
長方形が計算され(そしてR
それを実行して変数を変更し)、2番目の呼び出しでその変更された長方形の領域にテキストが描画されるためです。
procedure TForm1.Button1Click(Sender: TObject);
var
R: TRect;
S: String;
begin
R := Rect(0, 0, 20, 20);
S := 'Some text which will be stoutly wrapped and painted :)';
DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT);
end;