-
Notifications
You must be signed in to change notification settings - Fork 0
/
exUtils.pas
225 lines (195 loc) · 6.26 KB
/
exUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
unit exUtils;
(*****************************************************************************
(c) 2006, [email protected]
*****************************************************************************)
interface
uses StdCtrls, Windows, Messages, SysUtils, ShellApi, OSCARMd5, Classes;
type
PEditBallonTip = ^TEditBallonTip;
TEditBallonTip = packed record
cbStruct: DWORD;
pszTitle: PWideChar;
pszText: PWideChar;
ttiIcon: Integer;
end;
const
ECM_FIRST = $1500;
EM_SHOWBALLOONTIP = (ECM_FIRST + 3);
TTI_NONE = 0;
TTI_INFO = 1;
TTI_WARNING = 2;
TTI_ERROR = 3;
procedure exShowWinMsg(EditCtl: HWnd; Text: string; Caption: string; Icon: Integer; Balloon: Boolean);
procedure exDisableSysMenuItem(Handle: THandle; const Item: Integer);
function exIsWinXPMin: Boolean;
procedure exUpdateSysMenu(Handle: THandle; Cmd1: Integer; const Menu: string);
function exIsValidCharacters(Value: string): Boolean;
function exScreenNameIsIcqNumber(SN: string): Boolean;
function exNormalizeScreenName(SN: string) :string;
function exNormalizeIcqNumber(SN: string) :string;
function exUpdateHandCursor: HCURSOR;
function exIsValidMD5(MD5Hex: string): Boolean;
function exNowTime: string;
function GetNewestMd5Hash(Value: string): string;
implementation
procedure exDisableSysMenuItem(Handle: THandle; const Item: Integer);
var
SysMenu: HMenu;
begin
SysMenu := GetSystemMenu(Handle, False);
EnableMenuItem(SysMenu, Item, MF_DISABLED or MF_GRAYED);
end;
{*****************************************************************************}
function StrToWChar(Source: string; var Dest: PWideChar): Integer;
begin
Result := (Length(Source) * SizeOf(WideChar)) + 1;
GetMem(Dest, Result);
Dest := StringToWideChar(Source, Dest, Result);
end;
{*****************************************************************************}
procedure exShowWinMsg(EditCtl: HWnd; Text: string; Caption: string; Icon: Integer; Balloon: Boolean);
var
ebt: TEditBallonTip;
btn: Integer;
l1, l2: Integer;
begin
if Balloon then
begin
FillChar(ebt, sizeof(ebt), 0);
l1 := StrToWChar(Caption, ebt.pszTitle);
l2 := StrToWChar(Text, ebt.pszText);
ebt.ttiIcon := Icon;
ebt.cbStruct := sizeof(ebt);
SendMessage(EditCtl, EM_SHOWBALLOONTIP, 0, LongInt(@ebt));
FreeMem(ebt.pszTitle, l1);
FreeMem(ebt.pszText, l2);
end else
begin
case Icon of
TTI_INFO: btn := MB_ICONINFORMATION;
TTI_WARNING: btn := MB_ICONWARNING;
TTI_ERROR: btn := MB_ICONERROR;
else
btn := 0;
end;
MessageBox(EditCtl, PChar(Text), PChar(Caption), btn);
end;
end;
{*****************************************************************************}
function exIsWinXPMin: Boolean;
var
oi: TOSVersionInfo;
begin
FillChar(oi, SizeOf(oi), 0);
oi.dwOSVersionInfoSize := SizeOf(oi);
GetVersionEx(oi);
Result := (oi.dwPlatformId = VER_PLATFORM_WIN32_NT) and (oi.dwMajorVersion >= 5) and (oi.dwMinorVersion >= 1);
end;
{*****************************************************************************}
procedure exUpdateSysMenu(Handle: THandle; Cmd1: Integer; const Menu: string);
begin
AppendMenu(GetSystemMenu(Handle, False), MF_SEPARATOR, 0, #0);
AppendMenu(GetSystemMenu(Handle, False), MF_BYCOMMAND and MF_GRAYED, Cmd1, PChar(Menu));
end;
{*****************************************************************************}
function exIsValidCharacters(Value: string): Boolean;
const
ValidAsciiChars = ['a'..'z', 'A'..'Z', '0'..'9',
'~', '`', '!', '@', '#', '%',
'^', '&', '*', '(', ')', '-',
'=', '_', '+', '[', ']', '{',
'}', ';', '''', ',', '.', '/',
':', '"', '<', '>', '?'];
var
i: Integer;
begin
Result := True;
for i := 1 to Length(Value) do
if not (Value[i] in ValidAsciiChars) then
begin
Result := False;
Exit;
end;
end;
{*****************************************************************************}
function exScreenNameIsIcqNumber(SN: string): Boolean;
var
I: Real;
E: Integer;
begin
Val(SN, I, E);
Result := E = 0;
E := Trunc(I);
end;
{*****************************************************************************}
function exNormalizeScreenName(SN: string) :string;
function DeleteSpaces(const Value: string): string;
var
Counter, i: integer;
begin
Counter := 0;
SetLength(Result, Length(Value));
for i := 1 to Length(Value) do
if Value[i] <> ' ' then
begin
Inc(Counter);
Result[Counter] := Value[i];
end;
SetLength(Result, Counter);
end;
begin
Result := AnsiLowerCase(DeleteSpaces(SN));
end;
{*****************************************************************************}
function exNormalizeIcqNumber(SN: string) :string;
function DeleteDashes(const Value: string): string;
var
Counter, i: integer;
begin
Counter := 0;
SetLength(Result, Length(Value));
for i := 1 to Length(Value) do
if Value[i] <> '-' then
begin
Inc(Counter);
Result[Counter] := Value[i];
end;
SetLength(Result, Counter);
end;
begin
Result := DeleteDashes(SN);
end;
{*****************************************************************************}
function exUpdateHandCursor: HCURSOR;
begin
Result := LoadCursor(0, IDC_HAND);
end;
{*****************************************************************************}
function exIsValidMD5(MD5Hex: string): Boolean;
var
i: Byte;
begin
Result := False;
if Length(MD5Hex) <> 32 then Exit;
for i := 1 to Length(MD5Hex) do
if not (MD5Hex[i] in ['0'..'f']) then Exit;
Result := True;
end;
{*****************************************************************************}
function exNowTime: string;
begin
Result := FormatDateTime('hh:mm:ss', Now);
end;
{*****************************************************************************}
function GetNewestMd5Hash(Value: string): string;
var
Md5C: MD5Context;
Md5D: MD5Digest;
begin
MD5Init(Md5C);
MD5Append(Md5C, PChar(Value), Length(Value));
MD5Final(Md5C, Md5D);
SetLength(Result, 32);
BinToHex(PChar(@Md5D), PChar(Result) , 16);
end;
end.