I want to know the width and height of an image file before opening that file.
So, how can I do that?
This refers to JPEG, BMP, PNG and GIF types of image files.
If by 'image file' you mean those raster image files recognised by the VCL's graphics system, and by 'before opening' you mean 'before the user is likely to notice that the file is opened', then you can do this very easily:
var
pict: TPicture;
begin
with TOpenDialog.Create(nil) do
try
if Execute then
begin
pict := TPicture.Create;
try
pict.LoadFromFile(FileName);
Caption := Format('%d×%d', [pict.Width, pict.Height])
finally
pict.Free;
end;
end;
finally
Free;
end;
Of course, the file is opened, and this requires a lot of memory if the image is big. However, if you need to obtain metatada (like dimensions) without loading the file, I believe you need a more 'complicated' solution.
You can try this page. I have not tested it, but it seems pretty reasonable that it will work.
Also, different file types have different ways of getting the width and height.
One of the page answers:
unit ImgSize;
interface
uses Classes;
procedure GetJPGSize(const sFile: string; var wWidth, wHeight: word);
procedure GetPNGSize(const sFile: string; var wWidth, wHeight: word);
procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: word);
implementation
uses SysUtils;
function ReadMWord(f: TFileStream): word;
type
TMotorolaWord = record
case byte of
0: (Value: word);
1: (Byte1, Byte2: byte);
end;
var
MW: TMotorolaWord;
begin
// It would probably be better to just read these two bytes in normally and
// then do a small ASM routine to swap them. But we aren't talking about
// reading entire files, so I doubt the performance gain would be worth the trouble.
f.Read(MW.Byte2, SizeOf(Byte));
f.Read(MW.Byte1, SizeOf(Byte));
Result := MW.Value;
end;
procedure GetJPGSize(const sFile: string; var wWidth, wHeight: word);
const
ValidSig : array[0..1] of byte = ($FF, $D8);
Parameterless = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];
var
Sig: array[0..1] of byte;
f: TFileStream;
x: integer;
Seg: byte;
Dummy: array[0..15] of byte;
Len: word;
ReadLen: LongInt;
begin
FillChar(Sig, SizeOf(Sig), #0);
f := TFileStream.Create(sFile, fmOpenRead);
try
ReadLen := f.Read(Sig[0], SizeOf(Sig));
for x := Low(Sig) to High(Sig) do
if Sig[x] <> ValidSig[x] then
ReadLen := 0;
if ReadLen > 0 then
begin
ReadLen := f.Read(Seg, 1);
while (Seg = $FF) and (ReadLen > 0) do
begin
ReadLen := f.Read(Seg, 1);
if Seg <> $FF then
begin
if (Seg = $C0) or (Seg = $C1) then
begin
ReadLen := f.Read(Dummy[0], 3); // don't need these bytes
wHeight := ReadMWord(f);
wWidth := ReadMWord(f);
end
else
begin
if not (Seg in Parameterless) then
begin
Len := ReadMWord(f);
f.Seek(Len - 2, 1);
f.Read(Seg, 1);
end
else
Seg := $FF; // Fake it to keep looping.
end;
end;
end;
end;
finally
f.Free;
end;
end;
procedure GetPNGSize(const sFile: string; var wWidth, wHeight: word);
type
TPNGSig = array[0..7] of byte;
const
ValidSig: TPNGSig = (137, 80, 78, 71, 13, 10, 26, 10);
var
Sig: TPNGSig;
f: tFileStream;
x: integer;
begin
FillChar(Sig, SizeOf(Sig), #0);
f := TFileStream.Create(sFile, fmOpenRead);
try
f.Read(Sig[0], SizeOf(Sig));
for x := Low(Sig) to High(Sig) do
if Sig[x] <> ValidSig[x] then
exit;
f.Seek(18, 0);
wWidth := ReadMWord(f);
f.Seek(22, 0);
wHeight := ReadMWord(f);
finally
f.Free;
end;
end;
procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: word);
type
TGIFHeader = record
Sig: array[0..5] of char;
ScreenWidth, ScreenHeight: word;
Flags, Background, Aspect: byte;
end;
TGIFImageBlock = record
Left, Top, Width, Height: word;
Flags: byte;
end;
var
f: file;
Header: TGifHeader;
ImageBlock: TGifImageBlock;
nResult: integer;
x: integer;
c: char;
DimensionsFound: boolean;
begin
wWidth := 0;
wHeight := 0;
if sGifFile = '' then
exit;
{$I-}
FileMode := 0; // read-only
AssignFile(f, sGifFile);
reset(f, 1);
if IOResult <> 0 then
// Could not open file
exit;
// Read header and ensure valid file
BlockRead(f, Header, SizeOf(TGifHeader), nResult);
if (nResult <> SizeOf(TGifHeader)) or (IOResult <> 0)
or (StrLComp('GIF', Header.Sig, 3) <> 0) then
begin
// Image file invalid
close(f);
exit;
end;
// Skip color map, if there is one
if (Header.Flags and $80) > 0 then
begin
x := 3 * (1 SHL ((Header.Flags and 7) + 1));
Seek(f, x);
if IOResult <> 0 then
begin
// Color map thrashed
close(f);
exit;
end;
end;
DimensionsFound := False;
FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);
// Step through blocks
BlockRead(f, c, 1, nResult);
while (not EOF(f)) and (not DimensionsFound) do
begin
case c of
',': // Found image
begin
BlockRead(f, ImageBlock, SizeOf(TGIFImageBlock), nResult);
if nResult <> SizeOf(TGIFImageBlock) then
begin
// Invalid image block encountered
close(f);
exit;
end;
wWidth := ImageBlock.Width;
wHeight := ImageBlock.Height;
DimensionsFound := True;
end;
',' : // Skip
begin
// NOP
end;
// nothing else, just ignore
end;
BlockRead(f, c, 1, nResult);
end;
close(f);
{$I+}
end;
end.
And for BMP (also found at the page I mentioned):
function FetchBitmapHeader(PictFileName: String; Var wd, ht: Word): Boolean;
// similar routine is in "BitmapRegion" routine
label ErrExit;
const
ValidSig: array[0..1] of byte = ($FF, $D8);
Parameterless = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];
BmpSig = $4d42;
var
// Err : Boolean;
fh: HFile;
// tof : TOFSTRUCT;
bf: TBITMAPFILEHEADER;
bh: TBITMAPINFOHEADER;
// JpgImg : TJPEGImage;
Itype: Smallint;
Sig: array[0..1] of byte;
x: integer;
Seg: byte;
Dummy: array[0..15] of byte;
skipLen: word;
OkBmp, Readgood: Boolean;
begin
// Open the file and get a handle to it's BITMAPINFO
OkBmp := False;
Itype := ImageType(PictFileName);
fh := CreateFile(PChar(PictFileName), GENERIC_READ, FILE_SHARE_READ, Nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (fh = INVALID_HANDLE_VALUE) then
goto ErrExit;
if Itype = 1 then
begin
// read the BITMAPFILEHEADER
if not GoodFileRead(fh, #bf, sizeof(bf)) then
goto ErrExit;
if (bf.bfType <> BmpSig) then // 'BM'
goto ErrExit;
if not GoodFileRead(fh, #bh, sizeof(bh)) then
goto ErrExit;
// for now, don't even deal with CORE headers
if (bh.biSize = sizeof(TBITMAPCOREHEADER)) then
goto ErrExit;
wd := bh.biWidth;
ht := bh.biheight;
OkBmp := True;
end
else
if (Itype = 2) then
begin
FillChar(Sig, SizeOf(Sig), #0);
if not GoodFileRead(fh, #Sig[0], sizeof(Sig)) then
goto ErrExit;
for x := Low(Sig) to High(Sig) do
if Sig[x] <> ValidSig[x] then
goto ErrExit;
Readgood := GoodFileRead(fh, #Seg, sizeof(Seg));
while (Seg = $FF) and Readgood do
begin
Readgood := GoodFileRead(fh, #Seg, sizeof(Seg));
if Seg <> $FF then
begin
if (Seg = $C0) or (Seg = $C1) or (Seg = $C2) then
begin
Readgood := GoodFileRead(fh, #Dummy[0],3); // don't need these bytes
if ReadMWord(fh, ht) and ReadMWord(fh, wd) then
OkBmp := True;
end
else
begin
if not (Seg in Parameterless) then
begin
ReadMWord(fh,skipLen);
SetFilePointer(fh, skipLen - 2, nil, FILE_CURRENT);
GoodFileRead(fh, #Seg, sizeof(Seg));
end
else
Seg := $FF; // Fake it to keep looping
end;
end;
end;
end;
ErrExit: CloseHandle(fh);
Result := OkBmp;
end;
As a complement to Rafael's answer, I believe that this much shorter procedure can detect BMP dimensions:
function GetBitmapDimensions(const FileName: string; out Width,
Height: integer): boolean;
const
BMP_MAGIC_WORD = ord('M') shl 8 or ord('B');
var
f: TFileStream;
header: TBitmapFileHeader;
info: TBitmapInfoHeader;
begin
result := false;
f := TFileStream.Create(FileName, fmOpenRead);
try
if f.Read(header, sizeof(header)) <> sizeof(header) then Exit;
if header.bfType <> BMP_MAGIC_WORD then Exit;
if f.Read(info, sizeof(info)) <> sizeof(info) then Exit;
Width := info.biWidth;
Height := abs(info.biHeight);
result := true;
finally
f.Free;
end;
end;
If anyone yet interested in retrieving TIFF image dimensions without loading the graphic, there is a proven method that works perfectly for me in all environments. I also found another solution for that, but it returned wrong values from Illustrator-generated TIFFs. But there is a fantastic graphic library, called GraphicEx by Mike Lischke (TVirtualStringTree's very talented developer). There are implementations of many popular image formats and all of them descend from the base class TGraphicExGraphic, that implements ReadImageProperties virtual method. It is stream-based and only reads the fileheader in all implementations. So it is lightning-fast... :-)
So, here is a sample code, that retrieves a TIFF's dimensions (the method is the same for all graphic implementation, PNG,PCD,TGA,GIF,PCX,etc):
Uses ..., GraphicEx,...,...;
Procedure ReadTifSize (FN:String; Var iWidth,iHeight:Integer);
Var FS:TFileStream;
TIFF:TTIFFGraphic;
Begin
iWidth:=0;iHeight:=0;
TIFF:=TTIFFGraphic.Create;
FS:=TFileStream.Create(FN,OF_READ);
Try
TIFF.ReadImageProperties(FS,0);
iWidth:=TIFF.ImageProperties.Width;
iHeight:=TIFF.ImageProperties.Height;
Finally
TIFF.Destroy;
FS.Free;
End;
End;
That's all... :-) And this is the same for all the graphic implementations in the unit.
I don't like Rafael's solution for JPEG files too much because his algorithm parses every single byte until it hits FFC0. It doesn't make use of the fact that almost all markers (except FFD8, FFD9 and FFFE) are followed by two length bytes, allowing to skip from marker to marker. So I suggest the following procedure (which I condensed even a little more by stuffing checking for a marker and retrieving a value into the same function):
procedure GetJPGSize(const Filename: string; var ImgWidth, ImgHeight: word);
const
SigJPG : TBytes = [$FF, $D8];
SigC01 : TBytes = [$FF, $C0];
SigC02 : TBytes = [$FF, $C1];
var
FStream: TFileStream;
Buf: array[0..1] of Byte;
Offset,CheckMarker : Word;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
function SameValue(Sig:TBytes):Boolean;
begin
Result := CompareMem(#Sig[0], #Buf[0], Length(Sig));
end;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
function CheckMarkerOrVal(var Value:Word):Boolean;
begin
FStream.ReadData(Buf, Length(Buf));
Value := Swap(PWord(#Buf[0])^);
Result := (Buf[0] = $FF);
end;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
begin
FStream := TFileStream.Create(Filename, fmOpenRead);
Try
// First two bytes in a JPG file MUST be $FFD8, followed by the next marker
If not (CheckMarkerOrVal(CheckMarker) and SameValue(SigJPG))
then exit;
Repeat
If not CheckMarkerOrVal(CheckMarker)
then exit;
If SameValue(SigC01) or SameValue(SigC02) then begin
FStream.Position := FStream.Position + 3;
CheckMarkerOrVal(ImgHeight);
CheckMarkerOrVal(ImgWidth);
exit;
end;
CheckMarkerOrVal(Offset);
FStream.Position := FStream.Position + Offset - 2;
until FStream.Position > FStream.Size div 2;
Finally
FStream.Free;
end;
end;
Since GetGIFSize in Rafael's answer is broken and utterly complicated, here is my personal version of it:
function GetGifSize(var Stream: TMemoryStream; var Width: Word; var Height: Word): Boolean;
var
HeaderStr: AnsiString;
begin
Result := False;
Width := 0;
Height := 0;
//GIF header is 13 bytes in length
if Stream.Size > 13 then
begin
SetString(HeaderStr, PAnsiChar(Stream.Memory), 6);
if (HeaderStr = 'GIF89a') or (HeaderStr = 'GIF87a') then
begin
Stream.Seek(6, soFromBeginning);
Stream.Read(Width, 2); //Width is located at bytes 7-8
Stream.Read(Height, 2); //Height is located at bytes 9-10
Result := True;
end;
end;
end;
I found it by reading the RFC.
I need to draw text in the center of a box. The text must be drawn horizontally aligned with one char on each line.
I have attached an example with the string, "class".
I can draw the single char but I hope there is a faster way to do it.
procedure DrawVert(Canvas: TCanvas; Box: TRect; const Text: string);
var
i: Integer;
s: string;
R: TRect;
begin
s := '';
for i := 1 to Length(Text) do
s := s + Text[i] + ' ';
R := Rect(0, 0, 1, 0);
Canvas.TextRect(R, s, [tfCalcRect, tfNoClip, tfWordBreak]);
Box.Left := Box.Left + (Box.Right - Box.Left - R.Right) div 2;
Box.Top := Box.Top + (Box.Bottom - Box.Top - R.Bottom) div 2;
Box.Right := Box.Left + R.Right;
Box.Bottom := Box.Top + R.Bottom;
Canvas.TextRect(Box, s, [tfWordBreak]);
end;
For testing, let PaintBox1 is the box we're painting on
procedure TForm1.Button1Click(Sender: TObject);
begin
DrawVert(PaintBox1.Canvas, PaintBox1.Canvas.ClipRect, 'CLASS TEST');
end;
Currently using canvas.textout in a TGraphicControl Component, to show some text but i need the text to stay inside an area. Is there any property like word wrap i could use.. or a way to set the textout area? like so
var
r: TRect
s: string
begin
s := 'some long text that takes up about 3-4 lines';
r.Left := 10;
r.Top := 10;
r.Right := 20;
r.bottom := 50;
textout(r,s);
end;
You can use the DrawText function for this:
procedure TForm1.FormPaint(Sender: TObject);
const
S = 'This is some sample text. It is very long. Very long, indeed.' +
'Very, very, long.';
var
R: TRect;
begin
R := Rect(100, 100, 200, 200);
DrawText(Canvas.Handle, S, length(S), R, DT_WORDBREAK);
end;
code sample
procedure TForm1.Button1Click(Sender: TObject);
var
r: Trect;
s: String;
begin
R := Rect(0,0, 300, 100);
s := 'WordWrapTextOut(TargetCanvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);';
DrawText(Canvas.Handle, PChar(s), length(s), R, DT_WORDBREAK or DT_LEFT);
end;
I want to wrap the text in 300px width but how can I get the new Height? Is there a way or any solution?
The height of the drawn text is the returned value of DrawText.
HeightOfText := DrawText(...
If you want to update your rectangle before drawing the text you could use DT_CALCRECT. DrawText will then modify your rectangle to the new height (and width if necessary). If you only need the height though use the return value as Andreas Rejbrand showed.
Here's a sample of this:
procedure TForm1.Button1Click(Sender: TObject);
var
r: Trect;
s: String;
begin
R := Rect(0,0, 300, 100);
s := 'WordWrapTextOut(TargetCanvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);';
if DrawText(Canvas.Handle, PChar(s), length(s), R, DT_CALCRECT or DT_WORDBREAK or DT_LEFT) <> 0 then
begin
DrawText(Canvas.Handle, PChar(s), length(s), R, DT_WORDBREAK or DT_LEFT);
r.Top := r.Bottom;
r.Bottom := r.Bottom * 2;
DrawText(Canvas.Handle, PChar(s), length(s), R, DT_WORDBREAK or DT_LEFT);
end;
end;
I would recommend reading the docs for more details:
http://msdn.microsoft.com/en-us/library/dd162498(v=vs.85).aspx
As was mentioned here you can get it by calling DrawText function with DT_CALCRECT flag specified what actually won't paint anything; it just calculates appropriate rectangle and returns it to variable 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;
What means if you call it twice using the following example, you'll get drawn the wrapped text. It's because the first call with DT_CALCRECT calculates the rectangle (and modify R variable by doing it) and the second call draws the text in that modified rectangle area.
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;
I want to have one fixed row as a header, but the texts are rather long, so I'd like to increase the row height and insert CR/LF into the cell text.
Googling shows this as a solution (and it's the first thing I thought of before googling), but it doesn't seem to work. Any ideas?
Grid.Cells[2,3] := 'This is a sample test' + #13#10 + 'This is the second line';
What happens is that the cell contains This is a sample testThis is the second line
I am using Delphi 7 if it makes any difference.
[Bounty] "My bad. I actually awarded this an answer two years ago without checking and now find that the answer did not work. Apologies to anyone who was misled. This is a frequently asked, often wrongly answered question."
I presume that we are looking to use OnDrawCell, but imagine that we would also have to increase the height of the string grid row which contains the cell.
I will award the answer for either code or a FOSS VCL component.
[Update] must work with Delphi XE2 Starter edition
TStringGrid uses Canvas.TextRect, which uses ExtTextOut, which in turn does not support drawing of multiline text.
You have to draw this yourself in an OnDrawCell event handler with WinAPI's DrawText routine. See for example this answer on how to use DrawText for multiline text, and this recent answer on how to implement custom drawing in OnDrawCell:
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
procedure FillWithRandomText(AGrid: TStringGrid);
procedure UpdateRowHeights(AGrid: TStringGrid);
end;
procedure TForm1.FillWithRandomText(AGrid: TStringGrid);
const
S = 'This is a sample'#13#10'text that contains'#13#10'multiple lines.';
var
X: Integer;
Y: Integer;
begin
for X := AGrid.FixedCols to AGrid.ColCount - 1 do
for Y := AGrid.FixedRows to AGrid.RowCount - 1 do
AGrid.Cells[X, Y] := Copy(S, 1, 8 + Random(Length(S) - 8));
UpdateRowHeights(AGrid);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FillWithRandomText(StringGrid1);
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with TStringGrid(Sender) do
if Pos(#13#10, Cells[ACol, ARow]) > 0 then
begin
Canvas.FillRect(Rect);
Inc(Rect.Left, 2);
Inc(Rect.Top, 2);
DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]), -1, Rect,
DT_NOPREFIX or DT_WORDBREAK);
end;
end;
procedure TForm1.UpdateRowHeights(AGrid: TStringGrid);
var
Y: Integer;
MaxHeight: Integer;
X: Integer;
R: TRect;
TxtHeight: Integer;
begin
for Y := AGrid.FixedRows to AGrid.RowCount - 1 do
begin
MaxHeight := AGrid.DefaultRowHeight - 4;
for X := AGrid.FixedCols to AGrid.ColCount - 1 do
begin
R := Rect(0, 0, AGrid.ColWidths[X] - 4, 0);
TxtHeight := DrawText(AGrid.Canvas.Handle, PChar(AGrid.Cells[X, Y]), -1,
R, DT_WORDBREAK or DT_CALCRECT);
if TxtHeight > MaxHeight then
MaxHeight := TxtHeight;
end;
AGrid.RowHeights[Y] := MaxHeight + 4;
end;
end;
There are also other StringGrid components able of drawing multiline text. For instance, this one which I wrote myself (download source: NLDStringGrid) with possibly this result:
var
R: TRect;
begin
NLDStringGrid1.Columns.Add;
NLDStringGrid1.Columns.Add;
NLDStringGrid1.Cells[1, 1] := 'Sample test'#13#10'Second line';
NLDStringGrid1.Columns[1].MultiLine := True;
NLDStringGrid1.AutoRowHeights := True;
SetRect(R, 2, 2, 3, 3);
NLDStringGrid1.MergeCells(TGridRect(R), True, True);
NLDStringGrid1.ColWidths[2] := 40;
NLDStringGrid1.Cells[2, 2] := 'Sample test'#13#10'Second line';
end;
The TStringGrid's default renderer don't support multiple lines. By setting the TStringGrid in OwnerDraw mode (by invoking the OnDrawCell event) you can render each cell by your own liking.
Have a look at this for an example that helped a previous user.
Linked reference code inserted:
procedure DrawSGCell(Sender : TObject; C, R : integer; Rect : TRect;
Style : TFontStyles; Wrap : boolean; Just : TAlignment;
CanEdit : boolean);
{ draws formatted contents in string grid cell at col C, row R;
Style is a set of fsBold, fsItalic, fsUnderline and fsStrikeOut;
Wrap invokes word wrap for the cell's text; Just is taLeftJustify,
taRightJustify or taCenter; if CanEdit false, cell will be given
the background color of fixed cells; call this routine from
grid's DrawCell event }
var
S : string;
DrawRect : TRect;
begin
with (Sender as tStringGrid), Canvas do begin
{ erase earlier contents from default drawing }
if (R >= FixedRows) and (C >= FixedCols) and CanEdit then
Brush.Color:= Color
else
Brush.Color:= FixedColor;
FillRect(Rect);
{ get cell contents }
S:= Cells[C, R];
if length(S) > 0 then begin
case Just of
taLeftJustify : S:= ' ' + S;
taRightJustify : S:= S + ' ';
end;
{ set font style }
Font.Style:= Style;
{ copy of cell rectangle for text sizing }
DrawRect:= Rect;
if Wrap then begin
{ get size of text rectangle in DrawRect, with word wrap }
DrawText(Handle, PChar(S), length(S), DrawRect,
dt_calcrect or dt_wordbreak or dt_center);
if (DrawRect.Bottom - DrawRect.Top) > RowHeights[R] then begin
{ cell word-wraps; increase row height }
RowHeights[R]:= DrawRect.Bottom - DrawRect.Top;
SetGridHeight(Sender as tStringGrid);
end
else begin
{ cell doesn't word-wrap }
DrawRect.Right:= Rect.Right;
FillRect(DrawRect);
case Just of
taLeftJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_left);
taCenter : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_center);
taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_right);
end;
end
end
else
{ no word wrap }
case Just of
taLeftJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_left);
taCenter : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_center);
taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_right);
end;
{ restore no font styles }
Font.Style:= [];
end;
end;
end;
I think this will work fine for you...