📄 gettextextentexpointu.pas
字号:
unit GetTextExtentExPointU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Label1: TLabel;
procedure PaintBox1Paint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
TheString: PChar; // holds the output string
StrPointer: PChar; // a pointer within the output string
DisplayString: PChar; // holds the actual displayed string
MaxChars: Integer; // receives the maximum displayable characters
StringSize: TSize; // receives the string dimensions
LineNum: Integer; // a line number counter
ExtraSpace: Integer; // holds the extra space to add
NumBreaks: Integer; // holds the number of spaces in a string
Count: Integer; // a general loop control variable
begin
{erase the image on the paintbox canvas}
with PaintBox1.Canvas do
begin
Brush.Color := clWhite;
FillRect(ClipRect);
end;
{in itialize the original string}
TheString:='Delphi is the most awesome Windows development environment ever!';
{initialize the line number and the string pointer}
LineNum := 0;
StrPointer := TheString;
{retrieve enough memory for the displayed string}
GetMem(DisplayString, Length(TheString));
{loop through the string until the entire string is displayed}
while Length(StrPointer)>0 do
begin
{retrieve the maximum number of characters that can fit on
one line within the small paintbox}
GetTextExtentExPoint(PaintBox1.Canvas.Handle, TheString,
Length(TheString), PaintBox1.Width, @MaxChars,
nil, StringSize);
{if the remaining string is longer than what can be displayed on one line,
and the last character to be displayed is not a space, continue
decreasing the maximum displayable characters until we hit a space}
while (Length(StrPointer)>MaxChars) and (StrPointer[MaxChars]<>' ') do
Inc(MaxChars, -1);
{copy only the computed amount of characters into the displayable string.
this new string should fit within the paintbox without breaking any words}
StrLCopy(DisplayString, StrPointer, MaxChars);
{if the remaining string is longer that what can be displayed, move
the string pointer beyond the end of the displayed string; otherwise,
point the string pointer to an empty string}
if Length(StrPointer)>MaxChars then
StrPointer := @StrPointer[MaxChars+1]
else
StrPointer := #0;
{retrieve the width and height of the string}
GetTextExtentPoint32(PaintBox1.Canvas.Handle, DisplayString,
Length(DisplayString), StringSize);
{to justify the text so that it fills the entire line, compute the amount
of space left between the size of the string and the width of the
paintbox}
ExtraSpace := PaintBox1.Width - StringSize.cx;
{count the number of break characters in the displayed string. note that
this assumes that the break character is a space (' ')}
NumBreaks := 0;
for Count := 0 to Length(DisplayString)-1 do
if DisplayString[Count] = ' ' then
Inc(NumBreaks);
{if there is at least one space, set the text justification. this will add
the computed amount of extra space evenly among all of the spaces in the
line, thus performing a full justification when the string is drawn to
the device context}
if NumBreaks>0 then
SetTextJustification(PaintBox1.Canvas.Handle, ExtraSpace, NumBreaks);
{draw the fully justified string to the paint box device context}
TextOut(PaintBox1.Canvas.Handle, 0, LineNum*Stringsize.cy, DisplayString,
Length(DisplayString));
{reset the text justification to its original value for the next pass}
SetTextJustification(PaintBox1.Canvas.Handle, 0, 0);
{track the current text line number}
Inc(LineNum);
end;
{free the display string memory}
FreeMem(DisplayString, Length(TheString));
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -