UtilMethods.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. unit UtilMethods;
  2. interface
  3. uses
  4. Controls, ZhAPI, ActnList, ZjIDTree, DB, ZjGridDBA, ZjGrid, Windows, Messages,
  5. sdDB, VCLZip, VCLUnZip, Dialogs, Forms, ShlObj, Classes, StrUtils, Math, ADODB,
  6. IdGlobal;
  7. type
  8. TRoundMode = (rmNearest, rmUp, rmDown);
  9. TBookmarkRefreshEvent = procedure (AExpandFrame: Boolean) of object;
  10. {RoundTo}
  11. function GetCompareDigitValue(ADigit: Integer): Double;
  12. function QuantityRoundTo(AValue: Double): Double;
  13. function PriceRoundTo(AValue: Double): Double;
  14. function TotalPriceRoundTo(AValue: Double): Double;
  15. function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
  16. {Interface Control}
  17. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  18. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  19. {DataBase Rela}
  20. function GetsdDataSetNewID(ADataSet: TsdDataSet; const AIndex: string): Integer;
  21. procedure ExecuteSql(AConnection: TADOConnection; const ASql: string);
  22. function QueryData(AConnection: TADOConnection; const ASql: string): TADOQuery;
  23. {Message}
  24. procedure WarningMessage(const AMsg: string; AHandle: THandle = 0);
  25. procedure ErrorMessage(const AMsg: string; AHandle: THandle = 0);
  26. function QuestMessage(const AMsg: string; AHandle: THandle = 0): Boolean;
  27. function QuestMessageYesNo(const AMsg: string; AHandle: THandle = 0): Boolean;
  28. procedure TipMessage(const AMsg: string; AHandle: THandle = 0);
  29. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  30. {Get Common Path}
  31. function GetAppFilePath: string;
  32. function GetMyProjectsFilePath: string;
  33. function GetEmptyDataBaseFileName: string;
  34. function GetTemplateBillsFileName: string;
  35. function GetTemplateXlsFileName: string;
  36. function GetBackupFilePath: string;
  37. function GetReportTemplatePath: string;
  38. function GetAppTempPath: string;
  39. {Select & Save File Choose}
  40. function GetFilter(AExt: string): string;
  41. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  42. function SelectFiles(AFiles: TStrings; const AExt: string): Boolean;
  43. function SaveFile(var FileName: string; const AExt: string): Boolean;
  44. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  45. AParentHandle: THandle = 0; AHasNewFolderBtn: Boolean = True): Boolean;
  46. function FixPathByAppPath(AFileName: string): string;
  47. function BrowseFolder(var APath: string; const ATitle: string;
  48. AParentHandle: THandle; AHasNewFolderBtn: Boolean = True): Boolean;
  49. {CheckStrings}
  50. function CheckPeg(const AStr: string): Boolean;
  51. function CheckValidPassword(APassword: string): Boolean;
  52. function ValidInteger(var AKey: Char): Boolean;
  53. {MergeStrings}
  54. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  55. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  56. {CodeTransform}
  57. function B_CodeToIndexCode(const AB_Code: string): string;
  58. function ChinessNum(const ADigitNum: Integer): string;
  59. function Num2Peg(ANum: Double): string;
  60. {Compare Code}
  61. //function CompareCodeWithChar(const ACode1, ACode2: string): Integer;
  62. {Tree Relative}
  63. function CreateTree: TZjIDTree;
  64. procedure DisConnectTree(ATree: TZjIDTree);
  65. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  66. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  67. {Generate Name/Directory/Path}
  68. function GetTempFileDir: string;
  69. function GetTempFilePath: string;
  70. function GetTempName(ALength: Integer = 8): string;
  71. function GetTempFileName: string; overload;
  72. function GetTempFileName(const APath, AExt: string): string; overload;
  73. function GenerateTempFolder(AGeneratePath: string): string;
  74. function GetNewGUIDFileName(const AGeneratePath: string): string;
  75. {Progress bar Control}
  76. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  77. procedure UpdateProgress(APosition: Integer; const AHint: string);
  78. procedure DisableSysProgress;
  79. procedure EnableSysProgress;
  80. {Interface RePaint Control}
  81. procedure BeginUpdateWindow(AHandle: THandle);
  82. procedure EndUpdateWindow(AHandle: THandle);
  83. {Zip}
  84. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  85. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  86. {Copy By Stream}
  87. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  88. procedure AppendTestLog(const AFileName, ALog: string);
  89. // Add by chenshilong, 2014-04-11
  90. function GetVersion(APartCount: Integer = 4): string;
  91. function ExtractFileNameWithoutExt(const AFileName: string): string;
  92. function ShortText(AText: string; AWidth: Integer): string; // 文本缩略显示
  93. function CustomWidthText(AText: string; AWidth: Integer): string;
  94. function ReplaceCharsForJson(AText: string): string; // 替换Json文本中的特殊字符
  95. function RecoverCharsFromJson(AText: string): string; // 下载得到的Json文本恢复成用户需要的文本
  96. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  97. function DeleteFolder(const FolderStr: string): Boolean;
  98. function HasExt(const AFileName: string): Boolean;
  99. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  100. function FileCount(APath: string): Integer;
  101. implementation
  102. uses
  103. SysUtils, dxBar, MainFrm, ConstUnit, Globals, StdCtrls, ShellAPI;
  104. var
  105. SysProgressDisabled: Boolean;
  106. {RoundTo}
  107. function InnerRoundTo(const AValue: Extended; const ADigit: Integer; RoundMode: TRoundMode): Extended;
  108. var
  109. LFactor, Offset, HFactor: Extended;
  110. begin
  111. LFactor := IntPower(10, ADigit);
  112. HFactor := IntPower(10, abs(ADigit));
  113. Result := AValue;
  114. case RoundMode of
  115. rmNearest:
  116. begin
  117. if AValue >= 0 then
  118. Offset := 0.5
  119. else
  120. Offset := -0.5;
  121. Result := Trunc((AValue * HFactor) + Offset) * LFactor;
  122. end;
  123. rmUP:
  124. begin
  125. if Frac(AValue / LFactor) > 0 then
  126. Result := Trunc(AValue * HFactor + 1) * LFactor
  127. else
  128. Result := Trunc(AValue * HFactor) * LFactor;
  129. end;
  130. rmDown:
  131. begin
  132. Result := Trunc(AValue * HFactor) * LFactor;
  133. end;
  134. end;
  135. end;
  136. function GetCompareDigitValue(ADigit: Integer): Double;
  137. begin
  138. if ADigit < 0 then
  139. Result := IntPower(10, ADigit - 1)
  140. else
  141. Result := 0.1;
  142. end;
  143. function QuantityRoundTo(AValue: Double): Double;
  144. begin
  145. if Assigned(OpenProjectManager.CurProjectData) then
  146. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.QuantityDigit)
  147. else
  148. Result := CommonRoundTo(AValue, iQuantityDigit);
  149. end;
  150. function PriceRoundTo(AValue: Double): Double;
  151. begin
  152. if Assigned(OpenProjectManager.CurProjectData) then
  153. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.PriceDigit)
  154. else
  155. Result := CommonRoundTo(AValue, iPriceDigit);
  156. end;
  157. function TotalPriceRoundTo(AValue: Double): Double;
  158. begin
  159. if Assigned(OpenProjectManager.CurProjectData) then
  160. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.TotalPriceDigit)
  161. else
  162. Result := CommonRoundTo(AValue, iTotalPriceDigit);
  163. end;
  164. function GetTrueDigit(AValue: Double): Integer;
  165. const
  166. sFormat12 = '0.############';
  167. var
  168. sValue, sDigitValue: string;
  169. iPointPos: Integer;
  170. begin
  171. sValue := FormatFloat(sFormat12, AValue);
  172. iPointPos := Pos('.', sValue);
  173. Result := Min(0, -(Length(sValue) - iPointPos));
  174. end;
  175. function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
  176. var
  177. X: Double;
  178. P: Pointer;
  179. I: Integer;
  180. Buf: array [0..7] of Byte;
  181. begin
  182. P := @AValue;
  183. CopyMemory(@Buf[0], P, SizeOf(AValue));
  184. // 进位处理,从后往前,如果当前byte为$FF,则往前一byte进1
  185. // 注意到尾数开始的位置停止
  186. // 这里说的前后是二进制串,实际存储前后相反
  187. for I := 0 to 6 do
  188. if (I < 6) and (Buf[I] = $FF) then
  189. Buf[I] := 0
  190. else
  191. begin
  192. Buf[I] := Buf[I] + 1;
  193. Break;
  194. end;
  195. P := @X;
  196. CopyMemory(P, @Buf[0], SizeOf(X));
  197. if (ADigit < 0) and (ADigit < GetTrueDigit(AValue)) then
  198. Result := AValue
  199. else
  200. Result := InnerRoundTo(X, ADigit, RoundMode);
  201. end;
  202. {Interface Control}
  203. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  204. begin
  205. if Assigned(AControl) then
  206. begin
  207. if Assigned(AControl.Parent) then
  208. AControl.Parent.RemoveControl(AControl);
  209. AControl.Parent := AParent;
  210. AControl.Align := AAlign;
  211. end;
  212. end;
  213. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  214. begin
  215. if Assigned(ADxBtn) then
  216. if ADxBtn is TdxBarButton then
  217. TdxBarButton(ADxBtn).Action := AAction;
  218. end;
  219. {DataBase Rela}
  220. function GetsdDataSetNewID(ADataSet: TsdDataSet; const AIndex: string): Integer;
  221. var
  222. idx: TsdIndex;
  223. begin
  224. idx := ADataSet.FindIndex(AIndex);
  225. if idx.RecordCount > 0 then
  226. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  227. else
  228. Result := 1;
  229. end;
  230. procedure ExecuteSql(AConnection: TADOConnection; const ASql: string);
  231. var
  232. vQuery: TADOQuery;
  233. begin
  234. vQuery := TADOQuery.Create(nil);
  235. try
  236. vQuery.Connection := AConnection;
  237. vQuery.SQL.Add(ASql);
  238. vQuery.ExecSQL;
  239. finally
  240. vQuery.Free;
  241. end;
  242. end;
  243. function QueryData(AConnection: TADOConnection; const ASql: string): TADOQuery;
  244. begin
  245. Result := TADOQuery.Create(nil);
  246. Result.Connection := AConnection;
  247. Result.SQL.Clear;
  248. Result.SQL.Add(ASql);
  249. Result.Open;
  250. end;
  251. {Message}
  252. function GetValidHandle(AHandle: THandle = 0): THandle;
  253. begin
  254. if AHandle = 0 then
  255. begin
  256. if (Screen.ActiveForm <> nil) and (Screen.ActiveForm.HandleAllocated) then
  257. Result := Screen.ActiveForm.Handle
  258. else
  259. Result := Application.Handle;
  260. end
  261. else
  262. Result := AHandle;
  263. end;
  264. procedure WarningMessage(const AMsg: string; AHandle: THandle = 0);
  265. begin
  266. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('警告'), MB_OK or MB_ICONWARNING);
  267. end;
  268. procedure ErrorMessage(const AMsg: string; AHandle: THandle = 0);
  269. begin
  270. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('错误'), MB_OK or MB_ICONERROR);
  271. end;
  272. function QuestMessage(const AMsg: string; AHandle: THandle = 0): Boolean;
  273. begin
  274. Result := MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('询问'), MB_OKCANCEL or MB_ICONQUESTION) = IDOK;
  275. end;
  276. function QuestMessageYesNo(const AMsg: string; AHandle: THandle = 0): Boolean;
  277. begin
  278. Result := MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('询问'), MB_YESNO or MB_ICONQUESTION) = IDYes;
  279. end;
  280. procedure TipMessage(const AMsg: string; AHandle: THandle = 0);
  281. begin
  282. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('提示'), MB_OK or MB_ICONINFORMATION);
  283. end;
  284. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  285. begin
  286. Allow := False;
  287. ErrorMessage(AMsg);
  288. end;
  289. {Get Common Path}
  290. function GetAppFilePath: string;
  291. begin
  292. Result := ExtractFilePath(ParamStr(0));
  293. end;
  294. function GetMyProjectsFilePath: string;
  295. begin
  296. Result := GetAppFilePath + '我的项目\';
  297. end;
  298. function GetEmptyDataBaseFileName: string;
  299. begin
  300. Result := GetAppFilePath + 'Data\Base.dat';
  301. end;
  302. function GetTemplateBillsFileName: string;
  303. begin
  304. Result := GetAppFilePath + 'Data\Template.xls';
  305. end;
  306. function GetTemplateXlsFileName: string;
  307. begin
  308. Result := GetAppFilePath + 'Data\Basic.xls';
  309. end;
  310. function GetBackupFilePath: string;
  311. begin
  312. Result := GetAppFilePath + 'TenderBackup\';
  313. end;
  314. function GetReportTemplatePath: string;
  315. begin
  316. Result := GetAppFilePath + 'ReportTemplates\';
  317. end;
  318. function GetAppTempPath: string;
  319. begin
  320. Result := GetAppFilePath + 'Temp\';
  321. if DirectoryExists(Result) then
  322. CreateDirectoryInDeep(Result);
  323. end;
  324. {Select & Save File Choose}
  325. function GetFilter(AExt: string): string;
  326. begin
  327. if AExt = '' then
  328. Result := '所有文件(*.*)|*.*'
  329. else
  330. begin
  331. if AExt[1] <> '.' then
  332. AExt := '.' + AExt;
  333. Result := Format('(*%s)|*%s', [AExt, AExt]);
  334. end;
  335. end;
  336. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  337. var
  338. odFile: TOpenDialog;
  339. begin
  340. odFile := TOpenDialog.Create(nil);
  341. try
  342. odFile.Filter := GetFilter(AExt);
  343. if odFile.Execute then
  344. begin
  345. Application.ProcessMessages;
  346. AFileName := odFile.FileName;
  347. Result := True;
  348. end
  349. else
  350. Result := False;
  351. finally
  352. odFile.Free;
  353. end;
  354. end;
  355. function SelectFiles(AFiles: TStrings; const AExt: string): Boolean;
  356. var
  357. odFile: TOpenDialog;
  358. begin
  359. odFile := TOpenDialog.Create(nil);
  360. try
  361. odFile.Options := odFile.Options + [ofAllowMultiSelect];
  362. odFile.Filter := GetFilter(AExt);
  363. if odFile.Execute then
  364. begin
  365. Application.ProcessMessages;
  366. AFiles.Assign(odFile.Files);
  367. Result := True;
  368. end
  369. else
  370. Result := False;
  371. finally
  372. odFile.Free;
  373. end;
  374. end;
  375. function SaveFile(var FileName: string; const AExt: string): Boolean;
  376. var
  377. sdFile: TSaveDialog;
  378. begin
  379. sdFile := TSaveDialog.Create(nil);
  380. try
  381. sdFile.FileName := FileName;
  382. sdFile.DefaultExt := AExt;
  383. sdFile.Filter := GetFilter(AExt);
  384. Result := sdFile.Execute;
  385. if Result then
  386. FileName := sdFile.FileName;
  387. Application.ProcessMessages;
  388. finally
  389. sdFile.Free;
  390. end;
  391. end;
  392. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  393. AParentHandle: THandle; AHasNewFolderBtn: Boolean): Boolean;
  394. var
  395. pID: PItemIDList;
  396. bInfo: TBrowseInfo;
  397. AHandle: THandle;
  398. PStr: array[0..1023] of Char;
  399. sPath: string;
  400. function BFCallBack(Hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
  401. begin
  402. if uMsg = BFFM_INITIALIZED then
  403. begin
  404. SendMessage(Hwnd, BFFM_SETSELECTION, 1, lpData);
  405. end;
  406. Result := 0;
  407. end;
  408. begin
  409. Result := False;
  410. if AParentHandle = 0 then
  411. AHandle := Screen.ActiveForm.Handle
  412. else
  413. AHandle := AParentHandle;
  414. if ADirectory = '' then
  415. sPath := GetAppFilePath
  416. else
  417. sPath := ADirectory;
  418. with bInfo do
  419. begin
  420. hwndOwner := AParentHandle;
  421. iImage := 0;
  422. lParam := Integer(PChar(sPath));
  423. lpszTitle := PChar(ATitle);
  424. pidlRoot := nil;
  425. pszDisplayName := nil;
  426. if AHasNewFolderBtn then
  427. ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI
  428. else
  429. ulFlags := BIF_RETURNONLYFSDIRS;
  430. lpfn := @BFCallBack;
  431. end;
  432. pID := SHBrowseForFolder(bInfo);
  433. if pID <> nil then
  434. begin
  435. SHGetPathFromIDList(pID, PStr);
  436. ADirectory := StrPas(PStr);
  437. if (ADirectory <> '') and (ADirectory[Length(ADirectory)] <> '\') then
  438. ADirectory := ADirectory + '\';
  439. Result := True;
  440. end;
  441. end;
  442. function FixPathByAppPath(AFileName: string): string;
  443. begin
  444. Result := AFileName;
  445. if AnsiPos(':\', Result) = 0 then
  446. begin
  447. if (Result <> '') and (Result[1] = '\') then
  448. Delete(Result, 1, 1);
  449. Result := ExtractFilePath(Application.ExeName) + Result;
  450. end;
  451. end;
  452. var
  453. PathStr: string;
  454. function BrowseFolder(var APath: string; const ATitle: string; AParentHandle: THandle;
  455. AHasNewFolderBtn: Boolean): Boolean;
  456. function BFCallBackFunc(hwnd: HWND; uMsg: UINT; lParam: LPARAM; lpData: LPARAM): Integer; stdcall;
  457. begin
  458. Result := 0;
  459. case uMsg of
  460. BFFM_INITIALIZED : // 初始化设置目录。
  461. begin
  462. SendMessage(Hwnd,BFFM_SETSELECTION,-1,Integer(PathStr));
  463. end;
  464. end;
  465. end;
  466. var
  467. pID: PItemIDList;
  468. bInfo: TBrowseInfo;
  469. PStr: array[0..1023] of Char;
  470. sPath: string;
  471. Hdl: THandle;
  472. begin
  473. Result := False;
  474. PathStr := '';
  475. if AParentHandle = 0 then
  476. Hdl := Screen.ActiveForm.Handle
  477. else
  478. Hdl := AParentHandle;
  479. sPath := FixPathByAppPath(APath);
  480. if DirectoryExists(sPath) then
  481. PathStr := sPath;
  482. bInfo.hwndOwner := Hdl;
  483. bInfo.iImage := 0;
  484. bInfo.lParam := 0;
  485. bInfo.lpszTitle := PChar(ATitle);
  486. bInfo.pidlRoot := nil;
  487. bInfo.pszDisplayName := nil;
  488. if (not AHasNewFolderBtn) then
  489. bInfo.ulFlags := BIF_RETURNONLYFSDIRS
  490. else
  491. bInfo.ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI;
  492. bInfo.lpfn := @BFCallBackFunc;
  493. pID := SHBrowseForFolder(bInfo);
  494. if pID <> nil then
  495. begin
  496. SHGetPathFromIDList(pID,pStr);
  497. APath := StrPas(pStr);
  498. if (APath <> '') and (APath[Length(APath)] <> '\') then
  499. APath := APath + '\';
  500. Result := True;
  501. end;
  502. end;
  503. {CheckStrings}
  504. function CheckPeg(const AStr: string): Boolean;
  505. function GetPosition(const AName, AStr, AStrSpare: string): Integer;
  506. begin
  507. Result := Pos(AStr, AName);
  508. if Result = 0 then
  509. Result := Pos(AStrSpare, AName);
  510. end;
  511. var
  512. iPosK, iPosPlus: Integer;
  513. fNum: Double;
  514. begin
  515. Result := False;
  516. iPosK := GetPosition(AStr, 'K', 'k');
  517. iPosPlus := GetPosition(AStr, '+', '+');
  518. if (iPosK = 0) or (iPosPlus = 0) or (iPosPlus < iPosK) then Exit;
  519. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosPlus - iPosK - 1), fNum);
  520. end;
  521. function CheckValidPassword(APassword: string): Boolean;
  522. var
  523. iIndex, iLength: Integer;
  524. begin
  525. Result := True;
  526. if APassword = '' then Exit;
  527. iIndex := 1;
  528. iLength := Length(APassword);
  529. Result := (iLength >= 6) and (iLength <= 16);
  530. while Result and (iIndex < iLength) do
  531. begin
  532. Result := APassword[iIndex] in ['A'..'Z', 'a'..'z', '0'..'9'];
  533. Inc(iIndex);
  534. end;
  535. end;
  536. function ValidInteger(var AKey: Char): Boolean;
  537. begin
  538. if AKey in ['+', '-', '0'..'9', #8, #13, #35, #36,
  539. #37, #38, #39, #40, #46] then
  540. begin
  541. Result := True;
  542. end
  543. else
  544. begin
  545. AKey := #0;
  546. Result := False;
  547. end;
  548. end;
  549. {MergeStrings}
  550. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  551. var
  552. sgs1, sgs2: TStrings;
  553. i1, i2: Integer;
  554. bExist: Boolean;
  555. begin
  556. Result := ABGLCode1;
  557. sgs1 := TStringList.Create;
  558. sgs2 := TStringList.Create;
  559. try
  560. sgs1.Delimiter := ';';
  561. sgs2.Delimiter := ';';
  562. sgs1.DelimitedText := ABGLCode1;
  563. sgs2.DelimitedText := ABGLCode2;
  564. for i2 := 0 to sgs2.Count - 1 do
  565. begin
  566. bExist := False;
  567. for i1 := 0 to sgs1.Count - 1 do
  568. if SameText(sgs2[i2], sgs1[i1]) then
  569. begin
  570. bExist := True;
  571. Break;
  572. end;
  573. if not bExist then
  574. sgs1.Add(sgs2[i2]);
  575. end;
  576. Result := sgs1.DelimitedText;
  577. finally
  578. sgs1.Free;
  579. sgs2.Free;
  580. end;
  581. end;
  582. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  583. var
  584. sgsC1, sgsC2, sgsN1, sgsN2: TStrings;
  585. i1, i2: Integer;
  586. bExist: Boolean;
  587. begin
  588. sgsC1 := TStringList.Create;
  589. sgsC2 := TStringList.Create;
  590. sgsN1 := TStringList.Create;
  591. sgsN2 := TStringList.Create;
  592. try
  593. sgsC1.Delimiter := ';';
  594. sgsC2.Delimiter := ';';
  595. sgsC1.DelimitedText := ABGLCode1;
  596. sgsC2.DelimitedText := ABGLCode2;
  597. sgsN1.Delimiter := ';';
  598. sgsN2.Delimiter := ';';
  599. sgsN1.DelimitedText := ABGLNum1;
  600. sgsN2.DelimitedText := ABGLNum2;
  601. for i2 := 0 to sgsC2.Count - 1 do
  602. begin
  603. bExist := False;
  604. for i1 := 0 to sgsC1.Count - 1 do
  605. if SameText(sgsC2[i2], sgsC1[i1]) then
  606. begin
  607. bExist := True;
  608. Break;
  609. end;
  610. if bExist then
  611. begin
  612. sgsN1[i1] := FloatToStr(StrToFloatDef(sgsN1[i1], 0) + StrToFloatDef(sgsN2[i2], 0))
  613. end
  614. else
  615. begin
  616. sgsC1.Add(sgsC2[i2]);
  617. sgsN1.Add(sgsN2[i2]);
  618. end;
  619. end;
  620. ABGLCode1 := sgsC1.DelimitedText;
  621. ABGLNum1 := sgsN1.DelimitedText;
  622. finally
  623. sgsC1.Free;
  624. sgsC2.Free;
  625. sgsN1.Free;
  626. sgsN2.Free;
  627. end;
  628. end;
  629. function B_CodeToIndexCode(const AB_Code: string): string;
  630. var
  631. sgs: TStrings;
  632. i, iNum, iError: Integer;
  633. sError: string;
  634. begin
  635. sgs := TStringList.Create;
  636. try
  637. Result := '';
  638. sgs.Delimiter := '-';
  639. sgs.DelimitedText := AB_Code;
  640. for i := 0 to sgs.Count - 1 do
  641. begin
  642. Val(sgs[i], iNum, iError);
  643. if iError = 0 then
  644. sError := ''
  645. else
  646. sError := Copy(sgs[i], iError, Length(sgs[i])-iError+1);
  647. if iError = 0 then
  648. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError])
  649. else if iNum = 0 then
  650. Result := Result + '9999' + Format('%-4s', [sError])
  651. else
  652. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError]);
  653. end;
  654. finally
  655. sgs.Free;
  656. end;
  657. end;
  658. function ChinessNum(const ADigitNum: Integer): string;
  659. function TransChineseNum(const ANum, AZeroNum: Integer): string;
  660. begin
  661. Result := '';
  662. case ANum of
  663. 1: if AZeroNum <> 1 then Result := '一';
  664. 2: Result := '二';
  665. 3: Result := '三';
  666. 4: Result := '四';
  667. 5: Result := '五';
  668. 6: Result := '六';
  669. 7: Result := '七';
  670. 8: Result := '八';
  671. 9: Result := '九';
  672. end;
  673. if (Result = '') and ((AZeroNum <> 1) or (ANum = 0)) then Exit;
  674. case AZeroNum of
  675. 0: Result := Result;
  676. 1: Result := Result + '十';
  677. 2: Result := Result + '百';
  678. 3: Result := Result + '千';
  679. 4: Result := Result + '万';
  680. end;
  681. end;
  682. var
  683. iBai, iShi, iGe: Integer;
  684. begin
  685. Result := '';
  686. if (ADigitNum < 0) and (ADigitNum > 10000) then Exit;
  687. iBai := ADigitNum div 100;
  688. iShi := (ADigitNum mod 100) div 10;
  689. iGe := (ADigitNum mod 100) mod 10;
  690. Result := TransChineseNum(iBai, 2) + TransChineseNum(iShi, 1) + TransChineseNum(iGe, 0);
  691. end;
  692. function CheckDigit(ANum: Double): Integer;
  693. begin
  694. if abs(ANum - advRoundTo(ANum, -2)) > 0.0001 then
  695. Result := 3
  696. else if abs(ANum - advRoundTo(ANum, -1)) > 0.001 then
  697. Result := 2
  698. else if abs(ANum - AdvRoundTo(ANum, 0)) > 0.01 then
  699. Result := 1
  700. else
  701. Result := 0;
  702. end;
  703. function Num2Peg(ANum: Double): string;
  704. var
  705. fMod: Double;
  706. iK, iDigit: Integer;
  707. sDigit: string;
  708. begin
  709. iK := Trunc(ANum/1000);
  710. fMod := Frac(ANum/1000)*1000;
  711. iDigit := CheckDigit(fMod);
  712. case iDigit of
  713. 3: sDigit := FormatFloat('000.000', AdvRoundTo(fMod, -3));
  714. 2: sDigit := FormatFloat('000.00', AdvRoundTo(fMod, -2));
  715. 1: sDigit := FormatFloat('000.0', AdvRoundTo(fMod, -1));
  716. 0: sDigit := FormatFloat('000', Trunc(AdvRoundTo(fMod, 0)));
  717. end;
  718. { case iDigit of
  719. 3: Result := Format('K%d+%3.3f', [iK, ]);
  720. 2: Result := Format('K%d+%3.2f', [iK, AdvRoundTo(fMod, -2)]);
  721. 1: Result := Format('K%d+%3.1f', [iK, AdvRoundTo(fMod, -1)]);
  722. 0: Result := Format('K%d+%s', [iK, FormatFloat('000', [])]);
  723. end; }
  724. Result := Format('K%d+%s', [iK, sDigit]);
  725. end;
  726. {Tree Relative}
  727. function CreateTree: TZjIDTree;
  728. begin
  729. Result := TZjIDTree.Create;
  730. Result.KeyFieldName := 'ID';
  731. Result.ParentFieldName := 'ParentID';
  732. Result.NextSiblingFieldName := 'NextSiblingID';
  733. Result.AutoCreateKeyID := True;
  734. Result.AutoExpand := True;
  735. end;
  736. procedure DisConnectTree(ATree: TZjIDTree);
  737. begin
  738. ATree.DataSet := nil;
  739. ATree.Active := False;
  740. end;
  741. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  742. begin
  743. ATree.DataSet := ADataset;
  744. ATree.Active := True;
  745. end;
  746. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  747. var
  748. I: Integer;
  749. begin
  750. for I := 0 to AGridDBA.Columns.Count - 1 do
  751. AGrid.Cells[I + 1, 0].Text := AGridDBA.Columns[I].Title.Caption;
  752. end;
  753. {Generate Name/Path}
  754. function GetTempFileDir: string;
  755. var
  756. TempPath: string;
  757. begin
  758. TempPath := GetEnvironmentVariable('TMP');
  759. if TempPath = '' then
  760. TempPath := GetEnvironmentVariable('TEMP');
  761. if TempPath = '' then
  762. begin
  763. if not DirectoryExists(ExtractFileDir(ParamStr(0)) + '\Temp') then
  764. CreateDir(ExtractFileDir(ParamStr(0)) + '\Temp');
  765. TempPath := ExtractFileDir(ParamStr(0)) + '\Temp';
  766. end;
  767. Result := TempPath;
  768. end;
  769. function GetTempFilePath: string;
  770. begin
  771. Result := GetTempFileDir + '\';
  772. end;
  773. function GetTempName(ALength: Integer): string;
  774. const
  775. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  776. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  777. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  778. 'W', 'X', 'Y', 'Z');
  779. var
  780. Temp: string;
  781. Num: Integer;
  782. begin
  783. Randomize;
  784. Temp := '';
  785. while Length(Temp) < ALength do
  786. begin
  787. Num := Random(37);
  788. If Num <> 36 Then
  789. Temp := Temp + CodedBuf[Num]; // 0..35
  790. end;
  791. Result := Temp;
  792. end;
  793. function GetTempFileName: string;
  794. var
  795. TempExt: string;
  796. begin
  797. Result := GetTempFilePath + GetTempName;
  798. TempExt := '.' + GetTempName(3);
  799. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  800. SameText(TempExt, '.ldb') do
  801. TempExt := '.' + GetTempName(3);
  802. Result := Result + TempExt;
  803. while FileExists(Result) do
  804. begin
  805. Result := GetTempFilePath + GetTempName;
  806. TempExt := '.' + GetTempName(3);
  807. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  808. SameText(TempExt, '.ldb') do
  809. TempExt := '.' + GetTempName(3);
  810. Result := Result + TempExt;
  811. end;
  812. end;
  813. function GetTempFileName(const APath, AExt: string): string;
  814. begin
  815. Result := APath + GetTempName + AExt;
  816. while FileExists(Result) do
  817. Result := APath + GetTempName + AExt;
  818. end;
  819. function GenerateTempFolder(AGeneratePath: string): string;
  820. begin
  821. Result := AGeneratePath + GetTempName;
  822. while DirectoryExists(Result) do
  823. Result := AGeneratePath + GetTempName;
  824. CreateDirectoryInDeep(Result);
  825. end;
  826. function GetNewGUIDFileName(const AGeneratePath: string): string;
  827. var
  828. gFile: TGUID;
  829. begin
  830. repeat
  831. CreateGUID(gFile);
  832. Result := AGeneratePath + GUIDToString(gFile);
  833. until not FileExists(Result);
  834. end;
  835. {Progress bar Control}
  836. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  837. begin
  838. if not SysProgressDisabled then
  839. UpdateProgress(APosition, AHint);
  840. end;
  841. procedure UpdateProgress(APosition: Integer; const AHint: string);
  842. begin
  843. MainForm.UpdateProgress(APosition, AHint);
  844. end;
  845. procedure DisableSysProgress;
  846. begin
  847. SysProgressDisabled := True;
  848. end;
  849. procedure EnableSysProgress;
  850. begin
  851. SysProgressDisabled := False;
  852. end;
  853. {Interface RePaint Control}
  854. procedure BeginUpdateWindow(AHandle: THandle);
  855. begin
  856. SendMessage(AHandle, WM_SETREDRAW, 0, 0);
  857. end;
  858. procedure EndUpdateWindow(AHandle: THandle);
  859. begin
  860. SendMessage(AHandle, WM_SETREDRAW, 1, 0);
  861. end;
  862. {Zip}
  863. function ZipComplete(const AFileName: string; ACheckMode: Integer): Boolean;
  864. const
  865. i4k = 4096;
  866. var
  867. iSize1, iSize2: Integer;
  868. begin
  869. iSize1 := FileSizeByName(AFileName);
  870. Sleep(300);
  871. iSize2 := FileSizeByName(AFileName);
  872. Result := (iSize1 = iSize2);
  873. if ACheckMode = 0 then
  874. Result := Result and (iSize1 > i4k);
  875. end;
  876. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  877. procedure AppendLog(const ALog: string);
  878. begin
  879. if SupportManager.ConfigInfo.IsLog then
  880. MeasureLog.AppendLogTo(ALog);
  881. end;
  882. var
  883. sTempFile: string;
  884. vZip: TVCLZip;
  885. iCount: Integer;
  886. begin
  887. AppendLog('Zip');
  888. sTempFile := GetTempFileName;
  889. vZip := TVCLZip.Create(nil);
  890. iCount := 0;
  891. try
  892. vZip.FilesList.Clear;
  893. vZip.ZipName := sTempFile;
  894. vzip.RootDir := AFileFolder;
  895. vZip.OverwriteMode := Always;
  896. //vZip.AddDirEntriesOnRecurse:=True;
  897. vZip.RelativePaths := True;
  898. //vZip.RecreateDirs := True;
  899. vZip.FilesList.Add(AFileFolder + '\*.*');
  900. vZip.Zip;
  901. AppendLog(Format('Check Zip File, FileSize - %d', [FileSizeByName(sTempFile)]));
  902. while not ZipComplete(sTempFile, ACheckMode) and (iCount < 10) do
  903. begin
  904. Sleep(200);
  905. Inc(iCount);
  906. AppendLog(Format('Check Zip File - %d, FileSize - %d', [iCount + 1, FileSizeByName(sTempFile)]));
  907. end;
  908. AppendLog('Check Zip File --> Pass');
  909. CopyFileOrFolder(sTempFile, AFileName);
  910. AppendLog('Zip --> Pass');
  911. finally
  912. vZip.Free;
  913. if FileExists(sTempFile) then
  914. DeleteFile(sTempFile);
  915. end;
  916. end;
  917. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  918. var
  919. vUnZip: TVCLZip;
  920. begin
  921. Result := True;
  922. vUnZip := TVCLZip.Create(nil);
  923. try
  924. vUnZip.FilesList.Clear;
  925. vUnZip.ZipName := AFileName;
  926. vUnZip.ReadZip;
  927. vUnZip.DestDir := AFileFolder;
  928. vUnZip.OverwriteMode := Always;
  929. vUnZip.RecreateDirs := True;
  930. vUnZip.RelativePaths := True;
  931. vUnZip.DoAll := True;
  932. vUnZip.FilesList.Add('*.*');
  933. try
  934. vUnZip.UnZip;
  935. except
  936. Result := False;
  937. end;
  938. finally
  939. vUnZip.Free;
  940. end;
  941. end;
  942. {Copy By Stream}
  943. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  944. var
  945. MS: TMemoryStream;
  946. begin
  947. MS := TMemoryStream.Create;
  948. try
  949. MS.LoadFromFile(ASourceFile);
  950. if FileExists(ADestFile) then
  951. DeleteFile(ADestFile);
  952. MS.SaveToFile(ADestFile);
  953. finally
  954. MS.Free;
  955. end;
  956. end;
  957. procedure AppendTestLog(const AFileName, ALog: string);
  958. var
  959. f: TextFile;
  960. begin
  961. try
  962. if FileExists(AFileName) then
  963. begin
  964. AssignFile(f, AFileName);
  965. Append(f);
  966. Writeln(f, ALog);
  967. end
  968. else
  969. begin
  970. AssignFile(f, AFileName);
  971. Rewrite(f);
  972. Writeln(f, ALog);
  973. end;
  974. finally
  975. CloseFile(f);
  976. end;
  977. end;
  978. function GetVersion(APartCount: Integer = 4): string;
  979. var
  980. V1, V2, V3, V4: Word;
  981. FInfoSize, FF: Cardinal;
  982. FInfo: Pointer;
  983. FFixed: PVSFIXEDFILEINFO;
  984. begin
  985. FInfoSize := GetFileVersionInfoSize(PChar(Application.ExeName), FF);
  986. FInfo := AllocMem(FInfoSize);
  987. try
  988. GetFileVersionInfo(PChar(Application.ExeName), FF, FInfoSize, FInfo);
  989. VerQueryValue(FInfo, '\', Pointer(FFixed), FInfoSize);
  990. V1 := FFixed.dwFileVersionMS shr 16;
  991. V2 := FFixed.dwFileVersionMS and $FFFF;
  992. V3 := FFixed.dwFileVersionLS shr 16;
  993. V4 := FFixed.dwFileVersionLS and $FFFF;
  994. finally
  995. Dispose(FInfo);
  996. end;
  997. Result := '';
  998. case APartCount of
  999. 1:
  1000. Result := Format('%d', [V1]);
  1001. 2:
  1002. Result := Format('%d.%d', [V1, V2]);
  1003. 3:
  1004. Result := Format('%d.%d.%d', [V1, V2, V3]);
  1005. 4:
  1006. Result := Format('%d.%d.%d.%d', [V1, V2, V3, V4]);
  1007. end;
  1008. end;
  1009. function ExtractFileNameWithoutExt(const AFileName: string): string;
  1010. var
  1011. sFileName, Ext: string;
  1012. begin
  1013. Result := '';
  1014. if AFileName = '' then
  1015. Exit;
  1016. sFileName := AFileName;
  1017. if sFileName[Length(sFileName)] = '\' then
  1018. Delete(sFileName, Length(sFileName), 1);
  1019. Result := ExtractFileName(sFileName);
  1020. Ext := ExtractFileExt(sFileName);
  1021. Delete(Result, Length(Result) - Length(Ext) + 1, Length(Ext));
  1022. end;
  1023. function ShortText(AText: string; AWidth: Integer): string;
  1024. var vMM: TMemo;
  1025. begin
  1026. if Trim(AText) = '' then
  1027. begin
  1028. Result := '';
  1029. Exit;
  1030. end;
  1031. vMM := TMemo.Create(nil);
  1032. try
  1033. vMM.Visible := False;
  1034. vMM.WordWrap := True;
  1035. vMM.parent := Application.MainForm;
  1036. vMM.Width := AWidth - 10; // 10像素留给3个小点
  1037. vMM.Text := AText;
  1038. if vMM.Lines.Count > 1 then
  1039. Result := vMM.Lines[0] + '...'
  1040. else
  1041. Result := AText;
  1042. finally
  1043. vMM.Free;
  1044. end;
  1045. end;
  1046. function CustomWidthText(AText: string; AWidth: Integer): string;
  1047. var vMM: TMemo;
  1048. i: Integer;
  1049. begin
  1050. if Trim(AText) = '' then
  1051. begin
  1052. Result := '';
  1053. Exit;
  1054. end;
  1055. vMM := TMemo.Create(nil);
  1056. try
  1057. vMM.Visible := False;
  1058. vMM.WordWrap := True;
  1059. vMM.parent := Application.MainForm;
  1060. vMM.Width := AWidth;
  1061. vMM.Text := AText;
  1062. for i := 0 to vMM.Lines.Count - 1 do
  1063. begin
  1064. if i = 0 then
  1065. Result := vMM.Lines[i]
  1066. else
  1067. Result := Result + #10#13 + vMM.Lines[i];
  1068. end;
  1069. finally
  1070. vMM.Free;
  1071. end;
  1072. end;
  1073. function ReplaceCharsForJson(AText: string): string;
  1074. const
  1075. BefChar: array [0..7] of Char = ('{', '}', ',', ':', '"', '[', ']', '%');
  1076. AftChar: array [0..7] of string = ('{', '}', ',', ':', '"', '【', '】', '♂');
  1077. var I: Integer;
  1078. begin
  1079. AText := Trim(AText);
  1080. Result := AText;
  1081. if AText = '' then Exit;
  1082. for I := low(BefChar) to High(BefChar) do
  1083. begin
  1084. if Pos(BefChar[I], AText) > 0 then
  1085. AText := StringReplace(AText, BefChar[I], AftChar[I], [rfReplaceAll]);
  1086. end;
  1087. Result := AText;
  1088. end;
  1089. function RecoverCharsFromJson(AText: string): string;
  1090. const
  1091. BefStr: array [0..3] of string = ('♂', '\r\n', '\r', '\n');
  1092. AftStr: array [0..3] of string = ('%', '', '', '');
  1093. var I: Integer;
  1094. begin
  1095. AText := Trim(AText);
  1096. Result := AText;
  1097. if AText = '' then Exit;
  1098. for I := low(BefStr) to High(BefStr) do
  1099. begin
  1100. if Pos(BefStr[I], AText) > 0 then
  1101. AText := StringReplace(AText, BefStr[I], AftStr[I], [rfReplaceAll]);
  1102. end;
  1103. Result := AText;
  1104. end;
  1105. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  1106. var
  1107. SRec: TSearchRec;
  1108. retval: Integer;
  1109. begin
  1110. AFileList.Clear;
  1111. retval := FindFirst(APath + AExtName, faAnyFile, sRec);
  1112. try
  1113. while retval = 0 do
  1114. begin
  1115. if (SRec.Attr and faDirectory) = 0 then
  1116. AFileList.Add(Srec.Name);
  1117. retval := FindNext(SRec);
  1118. end;
  1119. finally
  1120. FindClose(SRec);
  1121. end;
  1122. end;
  1123. function DeleteFolder(const FolderStr: string): Boolean;
  1124. var
  1125. fos: SHFILEOPSTRUCT;
  1126. begin
  1127. ZeroMemory(@fos, SizeOf(fos));
  1128. fos.Wnd := HWND_DESKTOP;
  1129. fos.wFunc := FO_DELETE;
  1130. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO OR FOF_NOCONFIRMATION;
  1131. fos.pFrom := PChar(FolderStr + #0);
  1132. Result := SHFileOperation(fos) <> 0;
  1133. end;
  1134. function HasExt(const AFileName: string): Boolean;
  1135. begin
  1136. Result := ExtractFileExt(AFileName) <> '';
  1137. end;
  1138. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  1139. var
  1140. fos: SHFILEOPSTRUCT;
  1141. begin
  1142. ZeroMemory(@fos, SizeOf(fos));
  1143. fos.Wnd := HWND_DESKTOP;
  1144. fos.wFunc := FO_COPY;
  1145. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO;
  1146. fos.pFrom := PChar(SrcFolder + #0);
  1147. fos.pTo := PChar(DstFolder + #0);
  1148. Result := SHFileOperation(fos) <> 0;
  1149. end;
  1150. function FileCount(APath: string): Integer;
  1151. var
  1152. vSR: TSearchRec;
  1153. iRetval: Integer;
  1154. vSL: TStringList;
  1155. begin
  1156. vSL := TStringList.Create;
  1157. iRetval := FindFirst(APath + '*.*', faAnyFile, vSR);
  1158. try
  1159. while iRetval = 0 do
  1160. begin
  1161. if (vSR.Attr and faDirectory) = 0 then
  1162. vSL.Add(vSR.Name);
  1163. iRetval := FindNext(vSR);
  1164. end;
  1165. Result := vSL.Count;
  1166. finally
  1167. FindClose(vSR);
  1168. vSL.Free;
  1169. end;
  1170. end;
  1171. end.