1 |
torben |
2682 |
unit Utils;
|
2 |
|
|
|
3 |
|
|
interface
|
4 |
|
|
uses
|
5 |
|
|
Controls,
|
6 |
|
|
Graphics //TColor
|
7 |
|
|
;
|
8 |
|
|
|
9 |
|
|
type
|
10 |
|
|
TStrArray = array of string;
|
11 |
|
|
|
12 |
|
|
TUtils = class
|
13 |
|
|
class function split(input: string; schar: Char; s: Integer): string;
|
14 |
|
|
class function Explode(var a: TStrArray; Border, S: string): Integer;
|
15 |
|
|
|
16 |
|
|
class procedure RoundCornerOf(Control: TWinControl);
|
17 |
|
|
class function BarCodeValid (ACode: string): boolean;
|
18 |
|
|
class function CheckSumModulo10(const data:string):string;
|
19 |
|
|
|
20 |
|
|
class function TColorToHex(Color : TColor) : string;
|
21 |
|
|
class function HexToTColor(sColor : string) : TColor;
|
22 |
|
|
|
23 |
torben |
2685 |
class function AdobeReaderExists(): Boolean;
|
24 |
torben |
2682 |
|
25 |
|
|
|
26 |
|
|
|
27 |
torben |
2685 |
|
28 |
torben |
2682 |
end;
|
29 |
|
|
|
30 |
|
|
implementation
|
31 |
|
|
|
32 |
|
|
uses StrUtils,
|
33 |
|
|
Types, //TRect ,
|
34 |
|
|
Windows,
|
35 |
|
|
Messages,
|
36 |
torben |
2685 |
SysUtils, //IntToStr etc
|
37 |
|
|
Registry
|
38 |
|
|
|
39 |
torben |
2682 |
;
|
40 |
|
|
|
41 |
|
|
{
|
42 |
|
|
VERY fast split function
|
43 |
|
|
this function returns part of a string based on
|
44 |
|
|
constant defineable delimiters, such as ";". So
|
45 |
|
|
SPLIT('this is a test ',' ',3) = 'is' or
|
46 |
|
|
SPLIT('data;another;yet;again;more;',';',4) = 'yet'
|
47 |
|
|
|
48 |
|
|
Split function shifts index integer by two to
|
49 |
|
|
be compatible with commonly used PD split function
|
50 |
|
|
gpl 2004 / Juhani Suhonen
|
51 |
|
|
}
|
52 |
|
|
class function TUtils.split(input: string; schar: Char; s: Integer): string;
|
53 |
|
|
var
|
54 |
|
|
c: array of Integer;
|
55 |
|
|
b, t: Integer;
|
56 |
|
|
begin
|
57 |
|
|
Dec(s, 2); // for compatibility with very old & slow split function
|
58 |
|
|
t := 0; // variable T needs to be initialized...
|
59 |
|
|
setlength(c, Length(input));
|
60 |
|
|
for b := 0 to pred(High(c)) do
|
61 |
|
|
begin
|
62 |
|
|
c[b + 1] := posex(schar, input, succ(c[b]));
|
63 |
|
|
// BREAK LOOP if posex looped (position before previous)
|
64 |
|
|
// or wanted position reached..
|
65 |
|
|
if (c[b + 1] < c[b]) or (s < t) then break
|
66 |
|
|
else
|
67 |
|
|
Inc(t);
|
68 |
|
|
end;
|
69 |
|
|
Result := Copy(input, succ(c[s]), pred(c[s + 1] - c[s]));
|
70 |
|
|
end;
|
71 |
|
|
|
72 |
|
|
class function TUtils.Explode(var a: TStrArray; Border, S: string): Integer;
|
73 |
|
|
var
|
74 |
|
|
S2: string;
|
75 |
|
|
begin
|
76 |
|
|
Result := 0;
|
77 |
|
|
S2 := S + Border;
|
78 |
|
|
repeat
|
79 |
|
|
SetLength(A, Length(A) + 1);
|
80 |
|
|
a[Result] := Copy(S2, 0,Pos(Border, S2) - 1);
|
81 |
|
|
Delete(S2, 1,Length(a[Result] + Border));
|
82 |
|
|
Inc(Result);
|
83 |
|
|
until S2 = '';
|
84 |
|
|
end;
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
class procedure TUtils.RoundCornerOf(Control: TWinControl);
|
89 |
|
|
var
|
90 |
|
|
R: TRect;
|
91 |
|
|
Rgn: HRGN;
|
92 |
|
|
begin
|
93 |
|
|
with Control do
|
94 |
|
|
begin
|
95 |
|
|
R := ClientRect;
|
96 |
|
|
// rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 20, 20) ;
|
97 |
|
|
rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 25, 25) ;
|
98 |
|
|
// rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 30, 30) ;
|
99 |
|
|
Perform(EM_GETRECT, 0, lParam(@r)) ;
|
100 |
|
|
InflateRect(r, - 4, - 4) ;
|
101 |
|
|
Perform(EM_SETRECTNP, 0, lParam(@r)) ;
|
102 |
|
|
SetWindowRgn(Handle, rgn, True) ;
|
103 |
|
|
Invalidate;
|
104 |
|
|
end;
|
105 |
|
|
end;
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
class function TUtils.BarCodeValid (ACode: string): boolean;
|
109 |
|
|
var
|
110 |
|
|
I: integer;
|
111 |
|
|
SumOdd, SumEven: integer;
|
112 |
|
|
ADigit, AChecksumDigit: integer;
|
113 |
|
|
begin
|
114 |
|
|
SumOdd := 0;
|
115 |
|
|
SumEven := 0;
|
116 |
|
|
for I := 1 to (Length (ACode) - 1) do begin
|
117 |
|
|
ADigit := StrToIntDef (ACode [I], 0);
|
118 |
|
|
if (I MOD 2 = 0) then begin
|
119 |
|
|
SumEven := SumEven + ADigit;
|
120 |
|
|
end else begin
|
121 |
|
|
SumOdd := SumOdd + ADigit;
|
122 |
|
|
end; {if}
|
123 |
|
|
end; {for}
|
124 |
|
|
AChecksumDigit := StrToIntDef (ACode [Length (ACode)], 0);
|
125 |
|
|
Result := ((SumOdd*3 + SumEven + AChecksumDigit) MOD 10 = 0);
|
126 |
|
|
end; {--BarCodeValid--}
|
127 |
|
|
|
128 |
|
|
{ used for EAN 8/13 }
|
129 |
|
|
class function TUtils.CheckSumModulo10(const data:string):string;
|
130 |
|
|
var i,fak,sum : Integer;
|
131 |
|
|
begin
|
132 |
|
|
sum := 0;
|
133 |
|
|
fak := Length(data);
|
134 |
|
|
for i:=1 to Length(data) do
|
135 |
|
|
begin
|
136 |
|
|
if (fak mod 2) = 0 then
|
137 |
|
|
sum := sum + (StrToInt(data[i])*1)
|
138 |
|
|
else
|
139 |
|
|
sum := sum + (StrToInt(data[i])*3);
|
140 |
|
|
dec(fak);
|
141 |
|
|
end;
|
142 |
|
|
if (sum mod 10) = 0 then
|
143 |
|
|
result := data+'0'
|
144 |
|
|
else
|
145 |
|
|
result := data+IntToStr(10-(sum mod 10));
|
146 |
|
|
end;
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
class function TUtils.TColorToHex(Color : TColor) : string;
|
150 |
|
|
begin
|
151 |
|
|
Result :=
|
152 |
|
|
IntToHex(GetRValue(Color), 2) +
|
153 |
|
|
IntToHex(GetGValue(Color), 2) +
|
154 |
|
|
IntToHex(GetBValue(Color), 2) ;
|
155 |
|
|
end;
|
156 |
|
|
|
157 |
|
|
class function TUtils.HexToTColor(sColor : string) : TColor;
|
158 |
|
|
begin
|
159 |
|
|
Result :=
|
160 |
|
|
RGB(
|
161 |
|
|
StrToInt('$'+Copy(sColor, 1, 2)),
|
162 |
|
|
StrToInt('$'+Copy(sColor, 3, 2)),
|
163 |
|
|
StrToInt('$'+Copy(sColor, 5, 2))
|
164 |
|
|
) ;
|
165 |
|
|
end;
|
166 |
|
|
|
167 |
|
|
|
168 |
torben |
2685 |
class function TUtils.AdobeReaderExists(): Boolean;
|
169 |
|
|
var
|
170 |
|
|
AReg: TRegistry;
|
171 |
|
|
begin
|
172 |
|
|
result:= false;
|
173 |
|
|
AReg := TRegistry.Create;
|
174 |
|
|
AReg.RootKey := HKEY_LOCAL_MACHINE;
|
175 |
|
|
if AReg.KeyExists('\SOFTWARE\Adobe\Acrobat Reader') then
|
176 |
|
|
result:= True;
|
177 |
|
|
AReg.Free;
|
178 |
|
|
end;
|
179 |
|
|
|
180 |
torben |
2682 |
end.
|