Delphi 一些常用函数

  1. //创建快捷方式
  2. procedure CreateShellLink(const DestPath, LinkName, LinkAppPath,
  3.   LinkArgs, Description: string);
  4. const
  5.   SW_NORMAL = 1;
  6. var
  7.   aObj: IUnknown;
  8.   WFileName: WideString;
  9.   sPath: string;
  10. begin
  11.   try
  12.     OleInitialize(nil); //初始化OLE库,在使用OLE函数前必须调用初始化
  13.     aObj := CreateComObject(CLSID_ShellLink);
  14.     with aObj as IShellLink do begin
  15.      {对MS-DOS程序,一般建议使用SetShowCmd(SW_SHOWMAXIMIZED);}
  16.      //SetShowCmd(SW_NORMAL);
  17.       SetArguments(Pchar(LinkArgs));
  18.       SetPath(Pchar(LinkAppPath));
  19.       sPath := ExtractFilePath(LinkAppPath);
  20.       SetWorkingDirectory(Pchar(sPath));
  21.       SetDescription(Pchar(Description));
  22.     end;
  23.     WFileName := DestPath + '/' + LinkName;
  24.    {将一个String赋给WideString,转换过程由Delphi自动完成}
  25.     (aObj as IPersistFile).Save(PWChar(WFileName), False);
  26.   finally
  27.     OleUninitialize; //关闭OLE库,此函数必须与OleInitialize成对调用
  28.   end;
  29. end;
  30. {调用
  31.       CreateShellLink(DirStart, 'Uninstall.lnk',
  32.         GetProgramPath + MuzDir + '/'+'uninst.exe', '', '乐乐吧');
  33. }
  34. function GetProgramPath: string;
  35. var
  36.   s: string;
  37. begin
  38.   s := GetEnvironmentVariable('ProgramFiles');
  39.   Result := IncludeTrailingPathDelimiter(s);
  40. end;
  41. /复制目录
  42. function CopyDirectory(Source: string; pDirectory: string; pFilter: string): boolean;
  43. //目录拷贝source :源目录  directory:目标目录  pFilter:文件类型筛选'/*.*'或'/*.???'
  44. var
  45.   OpStruc: TSHFileOpStruct;
  46. begin
  47.   FillChar(OpStruc, SizeOf(OpStruc), 0);
  48.   with OpStruc do
  49.   begin
  50.     Wnd := 0;
  51.     wFunc := FO_COPY;
  52.     pFrom := PChar(Source + #0);
  53.     pTo := PChar(pDirectory + #0);
  54.     fFlags := FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR OR FOF_SILENT;
  55.     fAnyOperationsAborted := True;
  56.     hNameMappings := nil;
  57.     lpszProgressTitle := nil;
  58.   end;
  59.   if ShFileOperation(OpStruc) = 0 then
  60.     Result := True
  61.   else
  62.     Result := false;
  63. end;
  64. function DeleteDirAndFile(const ADir: string): Boolean;
  65. {var
  66.   OpStruc: TSHFileOpStructW;
  67. begin
  68.   FillChar(OpStruc, SizeOf(OpStruc), 0);
  69.   with OpStruc do
  70.   begin
  71.     Wnd := 0;
  72.     wFunc := FO_DELETE;
  73.     pFrom := PWideChar(ADir);
  74.     pTo := nil;
  75.     fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_NOERRORUI;
  76.     fAnyOperationsAborted := False;
  77.     hNameMappings := nil;
  78.     //lpszProgressTitle := nil;
  79.   end;
  80.   if ShFileOperationW(OpStruc) = 0 then
  81.     Result := True
  82.   else
  83.     Result := false;
  84. end; }
  85. var
  86.   fos : TSHFileOpStruct;
  87. begin
  88.   FillChar(fos, SizeOf(fos), 0);
  89.   with fos do
  90.   begin
  91.     wFunc  := FO_DELETE;
  92.     pFrom  := PChar(ADir);
  93.     fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT;
  94.   end;
  95.   if ShFileOperation(fos) = 0 then
  96.     Result := True
  97.   else
  98.     Result := false;
  99. end;
  100. //查看Windows操作系统语言
  101. function IsChnSimplifyWindows(var iType: Integer): Boolean;
  102. var
  103.   LangID: Integer;
  104. begin
  105.   LangID := GetSystemDefaultLangID;
  106.   Result := False;
  107.   iType := -1;
  108.   //ShowMessage(Languages.NameFromLocaleID[LangID]);
  109.   if LangID = ((SUBLANG_CHINESE_SIMPLIFIED shl 10) or LANG_CHINESE) then
  110.   begin
  111.     iType := 0; //简体
  112.     Result := True;
  113.     Exit;
  114.   end;
  115.   if LangID = ((SUBLANG_CHINESE_TRADITIONAL shl 10) or LANG_CHINESE) then
  116.   begin
  117.     iType := 1; //繁体
  118.     Exit;
  119.   end;
  120.   if LangID = ((SUBLANG_ENGLISH_US shl 10) or LANG_ENGLISH) then
  121.   begin
  122.     iType := 2; //English
  123.     Exit;
  124.   end;
  125. end;
  126. //Pos 的 Wide版本
  127. function WidePMatch(const M: WideString; const P: PWideChar): Boolean;
  128. var
  129.   I, L: Integer;
  130.   Q, R: PWideChar;
  131. begin
  132.   L := Length(M);
  133.   if L = 0 then
  134.   begin
  135.     Result := False;
  136.     Exit;
  137.   end;
  138.   R := Pointer(M);
  139.   Q := P;
  140.   for I := 1 to L do
  141.     if R^ <> Q^ then
  142.     begin
  143.       Result := False;
  144.       Exit;
  145.     end else
  146.     begin
  147.       Inc(R);
  148.       Inc(Q);
  149.     end;
  150.   Result := True;
  151. end;
  152. function WidePos(const F: WideString; const S: WideString; const StartIndex: Integer = 1): Integer;
  153. var
  154.   P: PWideChar;
  155.   I, L: Integer;
  156. begin
  157.   L := Length(S);
  158.   if (StartIndex > L) or (StartIndex < 1) then
  159.   begin
  160.     Result := 0;
  161.     Exit;
  162.   end;
  163.   P := Pointer(S);
  164.   Inc(P, StartIndex - 1);
  165.   for I := StartIndex to L do
  166.     if WidePMatch(F, P) then
  167.     begin
  168.       Result := I;
  169.       Exit;
  170.     end else
  171.       Inc(P);
  172.   Result := 0;
  173. end;
  174. //保存日志
  175. procedure SaveLog(AMsg: WideString);
  176. var
  177.   f: textfile;
  178.   fileName: WideString;
  179. begin
  180.   fileName := WideExtractFilePath(ParamStr(0)) + FormatDateTime('YYYYMMDD', Now) + '.log';
  181.   AssignFile(f, fileName);
  182.   if not WideFileExists(fileName) then
  183.     Rewrite(f)
  184.   else
  185.     Append(F);
  186.   Writeln(f, FormatDateTime('YYYY-MM-DD HH:NN:SS: ', Now) + AMsg);
  187.   CloseFile(f);
  188. end;
  189. function GetLocalFileSize(AFileName: WideString): DWORD;
  190. var
  191.   fs: TFileStream;
  192. begin
  193.   fs := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
  194.   try
  195.     Result := fs.Size;
  196.   finally
  197.     fs.Free;
  198.   end;
  199. end;
  200. function HexColorToColor(AColor: string): TColor;
  201. var
  202.   sR, sG, sB: string;
  203. begin
  204.   AColor := StringReplace(AColor, '#''', []);
  205.   sR := Copy(AColor, 5, 2);
  206.   sG := Copy(AColor, 3, 2);
  207.   sB := Copy(AColor, 1, 2);
  208.   Result := StrToInt64('$' + sR + sG + sB);
  209. end;
  210. function ChAnsiToWide(const StrA: AnsiString): WideString;
  211. var
  212.   nLen: integer;
  213. begin
  214.   Result := StrA;
  215.   if Result <> '' then
  216.   begin
  217.     nLen := MultiByteToWideChar(936, 1, PChar(@StrA[1]), -1, nil, 0);
  218.     SetLength(Result, nLen - 1);
  219.     if nLen > 1 then
  220.       MultiByteToWideChar(936, 1, PChar(@StrA[1]), -1, PWideChar(@Result[1]), nLen - 1);
  221.   end;
  222. end;
  223. function ChWideToAnsi(const StrW: WideString): AnsiString;
  224. var
  225.   nLen: integer;
  226. begin
  227.   Result := StrW;
  228.   if Result <> '' then
  229.   begin
  230.     nLen := WideCharToMultiByte(936, 624, @StrW[1], -1, nil, 0, nil, nil);
  231.     SetLength(Result, nLen - 1);
  232.     if nLen > 1 then
  233.       WideCharToMultiByte(936, 624, @StrW[1], -1, @Result[1], nLen - 1, nil, nil);
  234.   end;
  235. end;
  236. function GetString(sInput, sSplit: Widestring;
  237.                   var sAllLeft: Widestring): Widestring;
  238. var
  239.   SplitLen ,iSplitPos: integer;
  240.   sTmp : Widestring;
  241. begin
  242.   SplitLen := Length(sSplit);   //分隔符的长度 cyp
  243.   iSplitPos := WidePos(sSplit,sInput);  //分隔符的位置
  244.   sTmp := sInput;
  245.   if iSplitPos <= 0 then
  246.   begin
  247.     Result := sTmp;
  248.     sAllLeft := '';
  249.     Exit;
  250.   end;
  251.  //余下的所有字符
  252.   sAllleft := Copy(sTmp, iSplitPos+SplitLen, Length(sTmp) - iSplitPos );
  253.   sTmp := Copy(sTmp, 1, iSplitPos - 1 );
  254.   Result := sTmp;
  255. end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值