2 {*****************************************************************************}
4 { Tnt Delphi Unicode Controls }
5 { http://www.tntware.com/delphicontrols/unicode/ }
8 { Copyright (c) 2002-2007, Troy Wolbrink (troy.wolbrink@tntware.com) }
10 {*****************************************************************************}
14 {$INCLUDE TntCompilers.inc}
22 procedure WideCanvasTextRect(Canvas
: TCanvas
; Rect
: TRect
; X
, Y
: Integer; const Text: WideString
);
24 procedure WideCanvasTextOut(Canvas
: TCanvas
; X
, Y
: Integer; const Text: WideString
);
26 function WideCanvasTextExtent(Canvas
: TCanvas
; const Text: WideString
): TSize
;
27 function WideDCTextExtent(hDC
: THandle
; const Text: WideString
): TSize
;
29 function WideCanvasTextWidth(Canvas
: TCanvas
; const Text: WideString
): Integer;
31 function WideCanvasTextHeight(Canvas
: TCanvas
; const Text: WideString
): Integer;
35 TTntPicture
= class(TPicture
{TNT-ALLOW TPicture})
37 procedure LoadFromFile(const Filename
: WideString
);
38 procedure SaveToFile(const Filename
: WideString
);
44 SysUtils
, TntSysUtils
;
47 TAccessCanvas
= class(TCanvas
);
49 procedure WideCanvasTextRect(Canvas
: TCanvas
; Rect
: TRect
; X
, Y
: Integer; const Text: WideString
);
53 with TAccessCanvas(Canvas
) do begin
55 RequiredState([csHandleValid
, csFontValid
, csBrushValid
]);
56 Options
:= ETO_CLIPPED
or TextFlags
;
57 if Brush
.Style
<> bsClear
then
58 Options
:= Options
or ETO_OPAQUE
;
59 if ((TextFlags
and ETO_RTLREADING
) <> 0) and
60 (CanvasOrientation
= coRightToLeft
) then Inc(X
, WideCanvasTextWidth(Canvas
, Text) + 1);
61 Windows
.ExtTextOutW(Handle
, X
, Y
, Options
, @Rect
, PWideChar(Text),
67 procedure WideCanvasTextOut(Canvas
: TCanvas
; X
, Y
: Integer; const Text: WideString
);
69 with TAccessCanvas(Canvas
) do begin
71 RequiredState([csHandleValid
, csFontValid
, csBrushValid
]);
72 if CanvasOrientation
= coRightToLeft
then Inc(X
, WideCanvasTextWidth(Canvas
, Text) + 1);
73 Windows
.ExtTextOutW(Handle
, X
, Y
, TextFlags
, nil, PWideChar(Text),
75 MoveTo(X
+ WideCanvasTextWidth(Canvas
, Text), Y
);
80 function WideDCTextExtent(hDC
: THandle
; const Text: WideString
): TSize
;
84 Windows
.GetTextExtentPoint32W(hDC
, PWideChar(Text), Length(Text), Result
);
87 function WideCanvasTextExtent(Canvas
: TCanvas
; const Text: WideString
): TSize
;
89 with TAccessCanvas(Canvas
) do begin
90 RequiredState([csHandleValid
, csFontValid
]);
91 Result
:= WideDCTextExtent(Handle
, Text);
95 function WideCanvasTextWidth(Canvas
: TCanvas
; const Text: WideString
): Integer;
97 Result
:= WideCanvasTextExtent(Canvas
, Text).cX
;
100 function WideCanvasTextHeight(Canvas
: TCanvas
; const Text: WideString
): Integer;
102 Result
:= WideCanvasTextExtent(Canvas
, Text).cY
;
107 procedure TTntPicture
.LoadFromFile(const Filename
: WideString
);
109 ShortName
: WideString
;
111 ShortName
:= WideExtractShortPathName(Filename
);
112 if WideSameText(WideExtractFileExt(FileName
), '.jpeg') // the short name ends with ".JPE"!
113 or (ShortName
= '') then // GetShortPathName failed
114 inherited LoadFromFile(FileName
)
116 inherited LoadFromFile(WideExtractShortPathName(Filename
));
119 procedure TTntPicture
.SaveToFile(const Filename
: WideString
);
121 TempFile
: WideString
;
123 if Graphic
<> nil then begin
124 // create to temp file (ansi safe file name)
126 TempFile
:= WideExtractFilePath(Filename
) + IntToStr(Random(MaxInt
)) + WideExtractFileExt(Filename
);
127 until not WideFileExists(TempFile
);
128 CloseHandle(WideFileCreate(TempFile
)); // make it a real file so that it has a temp
131 Graphic
.SaveToFile(WideExtractShortPathName(TempFile
));
133 WideDeleteFile(Filename
);
134 if not WideRenameFile(TempFile
, FileName
) then
137 WideDeleteFile(TempFile
);