?? delphi常用函數庫.txt
字號:
I: Integer;
S1: string;
begin
S1 := HexToStr(S);
Result := S1;
for I := 1 to Length(S1) do
begin
if char(byte(S1[I]) xor (Key shr 8)) = Chr(0) then
begin
Result[I] := S1[I];
Key := (byte(Chr(0)) + Key) * C1 + C2; //保證Key的正確性
end
else
begin
Result[I] := char(byte(S1[I]) xor (Key shr 8));
Key := (byte(S1[I]) + Key) * C1 + C2;
end;
end;
end;
///VarIIF,VarTostr為變體函數
function varIIF(aTest: Boolean; TrueValue, FalseValue: Variant): Variant;
begin
if aTest then Result := TrueValue else Result := FalseValue;
end;
function varToStr(const V: Variant): string;
begin
case TVarData(v).vType of
varSmallInt: Result := IntToStr(TVarData(v).VSmallInt);
varInteger: Result := IntToStr(TVarData(v).VInteger);
varSingle: Result := FloatToStr(TVarData(v).VSingle);
varDouble: Result := FloatToStr(TVarData(v).VDouble);
varCurrency: Result := FloatToStr(TVarData(v).VCurrency);
varDate: Result := DateToStr(TVarData(v).VDate);
varBoolean: Result := varIIf(TVarData(v).VBoolean, 'True', 'False');
varByte: Result := IntToStr(TVarData(v).VByte);
varString: Result := StrPas(TVarData(v).VString);
varEmpty,
varNull,
varVariant,
varUnknown,
varTypeMask,
varArray,
varByRef,
varDispatch,
varError: Result := '';
end;
end;
function IsDigital(Value: string): boolean;
var
i, j: integer;
str: char;
begin
result := true;
Value := trim(Value);
j := Length(Value);
if j = 0 then
begin
result := false;
exit;
end;
for i := 1 to j do
begin
str := Value[i];
if not (str in ['0'..'9']) then
begin
result := false;
exit;
end;
end;
end;
function RandomStr(aLength : Longint) : String;
var
X : Longint;
begin
if aLength <= 0 then exit;
SetLength(Result, aLength);
for X:=1 to aLength do
Result[X] := Chr(Random(26) + 65);
end;
//▎============================================================▎//
//▎==================②擴展日期時間操作函數====================▎//
//▎============================================================▎//
function GetYear(Date: TDate): Integer;
var
y, m, d: WORD;
begin
DecodeDate(Date, y, m, d);
Result := y;
end;
function GetMonth(Date: TDate): Integer;
var
y, m, d: WORD;
begin
DecodeDate(Date, y, m, d);
Result := m;
end;
function GetDay(Date: TDate): Integer;
var
y, m, d: WORD;
begin
DecodeDate(Date, y, m, d);
Result := d;
end;
function GetHour(Time: TTime): Integer;
var
h, m, s, ms: WORD;
begin
DecodeTime(Time, h, m, s, ms);
Result := h;
end;
function GetMinute(Time: TTime): Integer;
var
h, m, s, ms: WORD;
begin
DecodeTime(Time, h, m, s, ms);
Result := m;
end;
function GetSecond(Time: TTime): Integer;
var
h, m, s, ms: WORD;
begin
DecodeTime(Time, h, m, s, ms);
Result := s;
end;
function GetMSecond(Time: TTime): Integer;
var
h, m, s, ms: WORD;
begin
DecodeTime(Time, h, m, s, ms);
Result := ms;
end;
//傳入年、月,得到該月份最后一天
function GetMonthLastDay(Cs_Year,Cs_Month:string):string;
Var
V_date:Tdate;
V_year,V_month,V_day:word;
begin
V_year:=strtoint(Cs_year);
V_month:=strtoint(Cs_month);
if V_month=12 then
begin
V_month:=1;
inc(V_year);
end
else
inc(V_month);
V_date:=EncodeDate(V_year,V_month,1);
V_date:=V_date-1;
DecodeDate(V_date,V_year,V_month,V_day);
Result:=DateToStr(EncodeDate(V_year,V_month,V_day));
end;
//判斷某年是否為閏年
function IsLeapYear( nYear: Integer ): Boolean;
begin
Result := (nYear mod 4 = 0) and ((nYear mod 100 <> 0) or (nYear mod 400 = 0));
end;
//兩個日期取較大的日期
function MaxDateTime(const Values: array of TDateTime): TDateTime;
var
I: Cardinal;
begin
Result := Values[0];
for I := 0 to Low(Values) do
if Values[I] < Result then Result := Values[I];
end;
//兩個日期取較小的日期
function MinDateTime(const Values: array of TDateTime): TDateTime;
var
I: Cardinal;
begin
Result := Values[0];
for I := 0 to High(Values) do
if Values[I] < Result then Result := Values[I];
end;
//得到本月的第一一天
function dateBeginOfMonth(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, Month, 1);
end;
//得到本月的最后一天
function dateEndOfMonth(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
if Month = 12 then
begin
Inc(Year);
Month := 1;
end else
Inc(Month);
Result := EncodeDate(Year, Month, 1) - 1;
end;
//得到本年的最后一天
function dateEndOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 12, 31);
end;
//得到兩個日期相隔的天數
function DaysBetween(Date1, Date2: TDateTime): integer;
begin
Result := Trunc(Date2) - Trunc(Date1) + 1;
if Result < 0 then Result := 0;
end;
//▎============================================================▎//
//▎=====================③位操作函數===========================▎//
//▎============================================================▎//
// 設置位
procedure SetBit(var Value: Byte; Bit: TByteBit; IsSet: Boolean);
begin
if IsSet then
Value := Value or (1 shl Bit)
else
Value := Value and not (1 shl Bit);
end;
procedure SetBit(var Value: WORD; Bit: TWordBit; IsSet: Boolean);
begin
if IsSet then
Value := Value or (1 shl Bit)
else
Value := Value and not (1 shl Bit);
end;
procedure SetBit(var Value: DWORD; Bit: TDWordBit; IsSet: Boolean);
begin
if IsSet then
Value := Value or (1 shl Bit)
else
Value := Value and not (1 shl Bit);
end;
// 取位
function GetBit(Value: Byte; Bit: TByteBit): Boolean;
begin
Result := Value and (1 shl Bit) <> 0;
end;
function GetBit(Value: WORD; Bit: TWordBit): Boolean;
begin
Result := Value and (1 shl Bit) <> 0;
end;
function GetBit(Value: DWORD; Bit: TDWordBit): Boolean;
begin
Result := Value and (1 shl Bit) <> 0;
end;
//▎============================================================▎//
//▎=================④擴展的文件及目錄操作函數=================▎//
//▎============================================================▎//
// 移動文件、目錄
function MoveFile(const sName, dName: string): Boolean;
var
s1, s2: AnsiString;
lpFileOp: TSHFileOpStruct;
begin
s1 := PChar(sName) + #0#0;
s2 := PChar(dName) + #0#0;
with lpFileOp do
begin
Wnd := Application.Handle;
wFunc := FO_MOVE;
pFrom := PChar(s1);
pTo := PChar(s2);
fFlags := FOF_ALLOWUNDO;
hNameMappings := nil;
lpszProgressTitle := nil;
fAnyOperationsAborted := True;
end;
Result := SHFileOperation(lpFileOp) = 0;
end;
// 打開文件屬性窗口
procedure FileProperties(const FName: string);
var
SEI: SHELLEXECUTEINFO;
begin
with SEI do
begin
cbSize := SizeOf(SEI);
fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_INVOKEIDLIST or
SEE_MASK_FLAG_NO_UI;
Wnd := Application.Handle;
lpVerb := 'properties';
lpFile := PChar(FName);
lpParameters := nil;
lpDirectory := nil;
nShow := 0;
hInstApp := 0;
lpIDList := nil;
end;
ShellExecuteEx(@SEI);
end;
// 縮短顯示不下的長路徑名
function FormatPath(APath: string; Width: Integer): string;
var
SLen: Integer;
i, j: Integer;
TString: string;
begin
SLen := Length(APath);
if (SLen <= Width) or (Width <= 6) then
begin
Result := APath;
Exit
end
else
begin
i := SLen;
TString := APath;
for j := 1 to 2 do
begin
while (TString[i] <> '\') and (SLen - i < Width - 8) do
i := i - 1;
i := i - 1;
end;
for j := SLen - i - 1 downto 0 do
TString[Width - j] := TString[SLen - j];
for j := SLen - i to SLen - i + 2 do
TString[Width - j] := '.';
Delete(TString, Width + 1, 255);
Result := TString;
end;
end;
// 打開文件框
function OpenDialog(var FileName: string; Title: string; Filter: string;
Ext: string): Boolean;
var
OpenName: TOPENFILENAME;
TempFilename, ReturnFile: string;
begin
with OpenName do
begin
lStructSize := SizeOf(OpenName);
hWndOwner := GetModuleHandle('');
Hinstance := SysInit.Hinstance;
lpstrFilter := PChar(Filter + #0 + Ext + #0#0);
lpstrCustomFilter := '';
nMaxCustFilter := 0;
nFilterIndex := 1;
nMaxFile := MAX_PATH;
SetLength(TempFilename, nMaxFile + 2);
lpstrFile := PChar(TempFilename);
FillChar(lpstrFile^, MAX_PATH, 0);
SetLength(TempFilename, nMaxFile + 2);
nMaxFileTitle := MAX_PATH;
SetLength(ReturnFile, MAX_PATH + 2);
lpstrFileTitle := PChar(ReturnFile);
FillChar(lpstrFile^, MAX_PATH, 0);
lpstrInitialDir := '.';
lpstrTitle := PChar(Title);
Flags := OFN_HIDEREADONLY + OFN_ENABLESIZING;
nFileOffset := 0;
nFileExtension := 0;
lpstrDefExt := PChar(Ext);
lCustData := 0;
lpfnHook := nil;
lpTemplateName := '';
end;
Result := GetOpenFileName(OpenName);
if Result then
FileName := ReturnFile
else
FileName := '';
end;
// 取兩個目錄的相對路徑,注意串尾不能是'\'字符!
function GetRelativePath(Source, Dest: string): string;
// 比較兩路徑字符串頭部相同串的函數
function GetPathComp(s1, s2: string): Integer;
begin
if Length(s1) > Length(s2) then swapStr(s1, s2);
Result := Pos(s1, s2);
while (Result = 0) and (Length(s1) > 3) do
begin
if s1 = '' then Exit;
s1 := ExtractFileDir(s1);
Result := Pos(s1, s2);
end;
if Result <> 0 then Result := Length(s1);
if Result = 3 then Result := 2;
// 修正因ExtractFileDir()處理'c:\'時產生的錯誤.
end;
// 取Dest的相對根路徑的函數
function GetRoot(s: ShortString): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(s) do
if s[i] = '\' then Result := Result + '..\';
if Result = '' then Result := '.\';
// 如果不想處理成".\"的路徑格式,可去掉本行
end;
var
RelativRoot, RelativSub: string;
HeadNum: Integer;
begin
Source := UpperCase(Source);
Dest := UpperCase(Dest); // 比較兩路徑字符串頭部相同串
HeadNum := GetPathComp(Source, Dest); // 取Dest的相對根路徑
RelativRoot := GetRoot(StrRight(Dest, Length(Dest) - HeadNum));
// 取Source的相對子路徑
RelativSub := StrRight(Source, Length(Source) - HeadNum - 1);
// 返回
Result := RelativRoot + RelativSub;
end;
// 運行一個文件
procedure RunFile(const FName: string; Handle: THandle;
const Param: string);
begin
ShellExecute(Handle, nil, PChar(FName), PChar(Param), nil, SW_SHOWNORMAL);
end;
// 運行一個文件并等待其結束
function WinExecAndWait32(FileName: string; Visibility: Integer): Integer;
var
zAppName: array[0..512] of Char;
zCurDir: array[0..255] of Char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(zAppName, FileName);
GetDir(0, WorkDir);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -